LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-17-2016, 11:04 AM   #1
Scraph
LQ Newbie
 
Registered: Jan 2016
Posts: 6

Rep: Reputation: Disabled
Question Preventing socket from `splitting' into several packets?


I am working with Linux sockets, TCP and/or UDP streams, and I'm trying to figure out how to prevent what I write() from being (seemingly) arbitrarily split into several smaller packets. The most I will ever transmit in one `chunk' is 4096 bytes.

When I write() the entire 4096 byte buffer in one go, it appears that I receive 2 packets of 2896 bytes and 1200 bytes separately on the other end ... So, I try to split it up into 2 2048 byte calls to write(). I appear to get on the other end 2048 bytes, 1448 bytes, and 600 bytes. If I split it into 4 1024 byte calls to write() then I get 1024, 1024, 1024, 848, and 176 bytes ... only very rarely have I seen it make it through as 4 equally sized packets.

What is insisting on splitting the last packet into 2 oddly sized packets? How can I enforce that a chunk of data is actually maintained in a single packet?

Ideally, I need the 4096 bytes that I transmit to arrive at the other end as a single 4096 byte packet, every single time.
 
Old 01-17-2016, 11:56 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
There's no pockets in TCP, it is a byte-stream. If you want data-units, you have do it yourself, for example: before the actual data-unit send the length of the data-unit in 4 bytes (say, network byte-order).
 
Old 01-17-2016, 12:17 PM   #3
Scraph
LQ Newbie
 
Registered: Jan 2016
Posts: 6

Original Poster
Rep: Reputation: Disabled
TCP abstracts to `byte-stream' as far as the end-user is concerned ... but TCP still transmits in segments which are packaged up into IP packets... so TCP segmentation certainly exists.

Are you trying to tell me there is no programmatic way to control/influence TCP segmentation?

Last edited by Scraph; 01-17-2016 at 12:22 PM.
 
Old 01-17-2016, 12:24 PM   #4
teapottwo
Member
 
Registered: Sep 2013
Posts: 52

Rep: Reputation: Disabled
According my book (old now), the max size of IPV4 datagram is 65535 bytes (65575 bytes for IPV6) (*both including headers), however the MTU of Ethernet is 1500 bytes, but it depends on the route taken across a network since the protocol will use the lowest MTU of all the hardware it crosses, known as the "path MTU".

There is the DF bit flag, "Don't Fragment", but if the "path MTU" is lower then you'll get the "destination unreachable" error.
Supposedly IPV6 doesn't fragment, but again due to path may produce the "packet too big error".


Setting of the DF flag will depend on what library / level you are using...



MTU = Maximum Transmission Unit
 
Old 01-18-2016, 03:58 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Term 'byte stream' means byte-stream: bytes sent together might received separately; and vice versa.
Mind you, no byte will be lost, and the order of bytes won't change. (Of course connection-loss is possible).
 
Old 01-18-2016, 08:30 AM   #6
Scraph
LQ Newbie
 
Registered: Jan 2016
Posts: 6

Original Poster
Rep: Reputation: Disabled
Yeah, the MTU of Ethernet is lower than TCP/IP allowed segments/packets ... but the IP protocol itself would have to handle fragmentation of its own datagrams, if the physical/hardware layers were to cause it. After all, UDP datagrams still guarantee that a single write() command will always be received on the other end by a single read() command (if received at all, of course).

In reading the socket man page and other articles on the subject, I find that I'm really just wishing that `SOCK_SEQPACKET' was supported on TCP/IP. That's pretty much what I've found I was asking for ... sequenced, reliable, two-way, connection-based _datagrams_.

On further thought, my application can stand a few lost or out-of-order packets so I'll just go to SOCK_DGRAM.
 
Old 01-18-2016, 10:10 AM   #7
teapottwo
Member
 
Registered: Sep 2013
Posts: 52

Rep: Reputation: Disabled
Quote:
Originally Posted by Scraph View Post
After all, UDP datagrams still guarantee that a single write() command will always be received on the other end by a single read() command (if received at all, of course).
Fragmentation rules still apply to UDP.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Socket does not respond to broadcasted packets ! ArshDeep Linux - Kernel 3 01-22-2014 09:13 AM
how to preventing sharing persistent socket with two different tabs of same browser golden_boy615 Programming 4 03-29-2012 05:44 PM
Wireshark UDP Packets Socket not set robtard Programming 3 10-27-2010 05:45 PM
QOS for packets received on a server socket scottbiker Programming 1 06-19-2008 02:55 AM
Preventing Socket Floods felosi Linux - Security 1 05-17-2006 04:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:55 PM.

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