LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 09-04-2008, 10:58 AM   #1
guru_stpetebeach
LQ Newbie
 
Registered: Mar 2008
Location: St Petersburg, Florida
Distribution: ubuntu, gentoo & debian
Posts: 28

Rep: Reputation: 17
tcp/ip sockets, aren't transmitted buffers gaurenteed to arrive entirely and intact?


Hi,

My networking experience is quite extensive, I thought I knew the answer to this, but my program has me doubting myself.

I am writing a protocol (application level) that transmits one or more C like structures from client to server and vice versa using tcp/ip sockets. I assumed that when I transmit a buffer of a specific size, it arrives at it's destination unfragmented and intact; this is the responsibility of the socket layer. Regardless, of the ip fragmentation that happens, the socket layer on the server or client should reconstruct the original buffer in it's entirity before calling the application's recieve callback.

Am I wrong?

I am, on the rare occasion, getting what looks to be like fragmented buffers coming in.

I want to confirm my assumption above with an experienced networking guru.

Thank you,
Colin
 
Old 09-05-2008, 04:16 AM   #2
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
It is a PACKET which is guaranteed (in TCP/IP) to arrive in its entirety, in sequence, and error-free. If you send an array (such as a buffer) you have to read very carefully about how the library calls treat the data - do they copy the data into their own buffers, do they keep referring to the pointer you passed to send parts of the data, and so on. There is also the issue of the ENDIAN - if you pass a structure on a little-end machine and it is received by a big-end machine, don't expect anything except for 'char' to come out looking right.
 
Old 09-05-2008, 05:03 AM   #3
BotKeeper
LQ Newbie
 
Registered: Aug 2006
Posts: 17

Rep: Reputation: 0
TCP ensures all data transmitted is received (unless the connection is broken). However, you may need to do more than one call to read(2) to get the data. If one read is short, read again until you have the data. Here is the general idea:

Code:
int readSocket (int fd, char * buf, int expected)
{
  int bytesIn = 0;
  int rc;

  while ( bytesIn < expected ) {
    rc = read ( rd, buf + bytesIn, expected - bytesIn );
    if ( rc <=0 ) {
      // Error: socket might be closed;
      bytesIn = rc;
      break;
    }
    bytesIn += rc;
  }
  return bytesIn;
}
 
Old 09-08-2008, 10:00 AM   #4
guru_stpetebeach
LQ Newbie
 
Registered: Mar 2008
Location: St Petersburg, Florida
Distribution: ubuntu, gentoo & debian
Posts: 28

Original Poster
Rep: Reputation: 17
Thanks guys. I am using C#, and using a class from CodeProject that simplifies socket calls. I had a sneaky feeling that the problem would be found in there. One problem is he used a static sized buffer to recieve of 8k. I will move to direct socket calls instead, get rid of that limitation, and change my protocol header to add the length field, and read until all data is recieved.

I am a hard core C++ programmer, new to C# but I'm loving it so far. Most of my apps run in linux without an issue! I also got Mono compiling too.

Thanks.

Last edited by guru_stpetebeach; 09-08-2008 at 10:02 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Program to forward tcp sockets to unix domain sockets mikepol Linux - Networking 0 09-27-2007 09:49 AM
C++: Packing buffers for TCP carcassonne Programming 1 07-13-2006 02:41 AM
TCP Network Sockets Rakoon Red Hat 1 08-04-2005 12:36 AM
High I/O Wait times and TCP buffers? oswald21 Linux - General 0 08-26-2003 10:22 AM
Maximum TCP-Sockets Quis Linux - Networking 2 01-17-2003 03:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 03:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration