×

Discussion Board

Results 1 to 13 of 13
  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    36

    [moved] sending large file over TCP/IP

    Hi All,

    Actually I want to transfer a larger file (500 kb - 1MB) over TCP/IP using carbide c++.

    At the reciever end I am having a C# code that listens the port and save the data.

    I am doing the following steps at the sender end :

    1. creating a socket connection and connecting to the port.
    2. reading the file in 8 kb chuncks in a loop.
    3. send the chunck over TCP/IP using send method.

    Now the problem is that from the sender end data is being send but at the reciever end I am getting the listen event only for the first send. This problem is solved if at the sender end I close and connect the socket within the loop then I am getting the data at the reciever end.

    but Now if at this point I try to append my data file at recieving end , the final output is not properly formed.

    Has someone ever tried with this kind of approch before or is there any other way to send the large file over TCP/IP.

    Please Guide.

    Regards

  2. #2
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,697

    Re: sending large file over TCP/IP

    Steps 1-2-3 sound fine to me. You may consider showing some code. That Close should not be necessary inside the loop.

  3. #3
    Nokia Developer Expert
    Join Date
    Jun 2008
    Posts
    333

    Re: sending large file over TCP/IP

    your step should be OK. your can try to sniffer the communication to check the detail problem.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    36

    Re: sending large file over TCP/IP

    the code is like this

    RFs fs ;
    User::LeaveIfError(fs.Connect());
    RFile file ;
    _LIT(KDesName,"c:\\System\\Apps\\testfolder\\my.jpg");
    TBool running = ETrue;
    TInt cnt =0;
    TInetAddr addr;
    addr.Input(_L("10.10.10.120"));
    addr.SetPort(8081);

    RSocketServ socketServ;
    RSocket blank;
    RSocket sender;

    socketServ.Connect();
    CleanupClosePushL(socketServ);

    TRequestStatus status;
    TRequestStatus fr;
    TSockXfrLength dummyLength;

    while(1)
    {
    User::LeaveIfError(sender.Open(socketServ, KAfInet,KSockStream, KProtocolInetTcp));
    sender.Connect(addr,status);

    HBufC8* buffer = HBufC8::NewL( 8192 );
    TPtr8 ptr = buffer->Des();
    if(cnt == 0)
    {
    CleanupClosePushL(file);
    TInt offset = 0;
    file.Open(fs,KDesName,EFileRead);
    file.Read(ptr,8192);
    file.Close();
    CleanupStack::PopAndDestroy(1);
    }
    else
    {
    TInt32 pos = cnt*8192;
    TInt offset = 0;
    CleanupClosePushL(file);
    file.Open(fs,KDesName,EFileRead);
    file.Read(pos,ptr,8192,fr);
    file.Close();
    CleanupStack::PopAndDestroy(1);

    }

    sender.Send(ptr,0,status);

    cnt++;
    sender.Close();
    }

    ///
    if I put the Bold code outside the loop,the listener listens only once.

  5. #5
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,697

    Re: sending large file over TCP/IP

    Oh, that nice cnt==0 code again...
    However that works. The main problem is when you re-issue the asynchronous request without checking for the completion of the previous one...
    And that while(1) is also really nice.

    How about trying something shorter?
    Code:
    RFs fs ;
    User::LeaveIfError(fs.Connect());
    CleanupClosePushL(fs);
    
    RFile file ;
    _LIT(KFileName,"c:\\System\\Apps\\testfolder\\my.jpg");
    User::LeaveIfError(file.Open(fs,KFileName,0));
    CleanupClosePushL(file);
    
    TInetAddr addr; 
    addr.Input(_L("10.10.10.120")); 
    addr.SetPort(8081); 
    
    RSocketServ socketServ; 
    RSocket sender; 
    
    User::LeaveIfError(socketServ.Connect());
    CleanupClosePushL(socketServ);
    
    TRequestStatus status;
    
    User::LeaveIfError(sender.Open(socketServ, KAfInet,KSockStream, KProtocolInetTcp)); 
    CleanupClosePushL(sender);
    sender.Connect(addr,status);
    User::WaitForRequest(status);
    User::LeaveIfError(status.Int());
    
    HBufC8* buffer = HBufC8::NewLC( 8192 ); 
    TPtr8 ptr = buffer->Des();
    
    User::LeaveIfError(file.Read(ptr));
    while(ptr.Length()>0)
    {
        sender.Write(ptr,status);
        User::WaitForRequest(status);
        User::LeaveIfError(status.Int());
        User::LeaveIfError(file.Read(ptr));
    }
    
    CleanupStack::PopAndDestroy(5); // buffer, sender, socketServ, file, fs
    this code is still evil unfortunately, since User::WaitForRequest looks bad, especially in a GUI code. However if you are testing in a console executable, it should work, and it may also work with a GUI app.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    36

    Re: sending large file over TCP/IP

    Hey thx for the nice code....but the output is same

  7. #7
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,697

    Re: sending large file over TCP/IP

    What do you receive?

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    36

    Re: sending large file over TCP/IP

    Thanks a lot for ur help.....the problem was with the listener code.
    Now I am able to transfer the image.

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    338

    Re: sending large file over TCP/IP

    hi wizard hu,
    I have seen your code snippet. Could you make me unterstood the following part of your code:
    Code:
    while(ptr.Length()>0)
    {
        sender.Write(ptr,status);
        User::WaitForRequest(status);
        User::LeaveIfError(status.Int());
        User::LeaveIfError(file.Read(ptr));
    }
    My question is,
    1) how can the sender be sure that the chunk which is sent is actually received by the receiver? is it a handshaking protocol?
    2) while reading the file, is the 'ptr' changing automatically and updating the offset ? If it is that then, it is very nice... how can i know what methods actually gives this facility to update the offset as the 'ptr'?
    3) what User::leaveIferror(...) do? can it break the while loop ?

  10. #10
    Registered User
    Join Date
    Jan 2005
    Location
    Italy
    Posts
    639

    Re: sending large file over TCP/IP

    Quote Originally Posted by Md. Kayesh View Post
    My question is,
    1) how can the sender be sure that the chunk which is sent is actually received by the receiver? is it a handshaking protocol?
    2) while reading the file, is the 'ptr' changing automatically and updating the offset ? If it is that then, it is very nice... how can i know what methods actually gives this facility to update the offset as the 'ptr'?
    3) what User::leaveIferror(...) do? can it break the while loop ?
    Hello,
    i'm not wizard :-) but if you are in a hurry:

    1) It's based on TCP protocol, as long as the connection isn't broken or closed, layers "under" it will take care of retransmission; also, since it's TCP and it's stream, packets sent are received in the correct order
    2) basically ptr points to the head of buffer, so every ReadL reads or try to read from the file at current position up to 8192 bytes in buffer, destroying previous content; 8192 is a sort of magic number, it's usually the length of network buffers, so you read and send your file in one or more trunks
    3) calls into LeaveIfError return an TInt, usually, so if the return value is KErrNone, everything is fine and the next line is executed, otherwise the call leaves, and not only the loop is broken, but you app crashes if the leave isn't catched somewhere "above"

    hope it helps,
    regards
    pg
    Last edited by pavarang; 2012-01-22 at 21:32.

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    338

    Re: sending large file over TCP/IP

    thank you pavarang.. for a nice reply

    I had one more query.. that is if i have a file of 8192 *2 = 16384 bytes then the while loop will run for 2 times. Here i guess the 'ptr' jumps 8192 bytes after each call to Read(..) function. Am i right ? If i am wrong would you tell me what is really the while loop doing here ?

  12. #12
    Registered User
    Join Date
    Jan 2005
    Location
    Italy
    Posts
    639

    Re: sending large file over TCP/IP

    Quote Originally Posted by Md. Kayesh View Post
    thank you pavarang.. for a nice reply

    I had one more query.. that is if i have a file of 8192 *2 = 16384 bytes then the while loop will run for 2 times. Here i guess the 'ptr' jumps 8192 bytes after each call to Read(..) function. Am i right ? If i am wrong would you tell me what is really the while loop doing here ?
    Hello,
    ptr into ReadL is the pointer to the buffer that must be filled with file data, not the pointer of the data inside the file.
    So, ptr "points" to the HBufC8 buffer. This is a trick, because you can't directly modify HBufC8 (where you should notice the C suffix)... so you create a ptr to it.
    Now, if you look at docs about RFile::ReadL() you should see one of them, the one used into this snippet of code, that read from the file at current position, where the important statement is "at current position"; so every time ReadL is called, it starts to read from the file at current position, not at the beginning.
    Hope it helps, i'm not so good in detailed explanations :-)
    Regards,
    pg

  13. #13
    Registered User
    Join Date
    Feb 2011
    Posts
    338

    Re: sending large file over TCP/IP

    Thank you so much..

    You said.
    i'm not so good in detailed explanations :-)
    The explanation was not good but better..

Similar Threads

  1. How to create large file fast?
    By tyrael in forum Mobile Java General
    Replies: 1
    Last Post: 2007-07-25, 15:23
  2. sending file over tcp/ip to symbian emulator
    By yakoc in forum Symbian Networking & Messaging (Closed)
    Replies: 1
    Last Post: 2005-12-26, 12:24
  3. Sending XML file from server to mobile
    By bvsbh in forum Mobile Java General
    Replies: 1
    Last Post: 2005-11-30, 14:21
  4. Replies: 1
    Last Post: 2005-05-26, 14:22
  5. Replies: 0
    Last Post: 2002-06-10, 12:24

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×