473,398 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Socket Send - Seperate Packets for Each buffer

Dear Readers,

I am trying to learn C# network programming and I have discovered that
Windows TCPIP often joins small packets and send them as one to the
remote server. This appears to be a default behaviour of TCPIP?

I am attempting to send 1000 TCPIP packets to a remote sever with only
a sequence number (and soon a variable length message (only a few
characters)) in them.

packet 0 0
packet 1 1
packet 2 2
....
packet 100 100
....
packet 999 999

So at my remote server I wish to receive 1000 packets each with a
single number in each packet.

However at my server I get packets that include all the number to 100
(for example) joined together.

How can I force the C# socket send to send just the buffer I want in
each packet?

Thanks

Tanaka

private void send_Click(object sender, System.EventArgs e)
{
int port = 5000;
Socket tcpsocket;

tcpsocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);

IPEndPoint remoteServerEndPoint = new IPEndPoint(IPAddress.Parse(
"10.1.1.1" ), port );

tcpsocket.Connect(remoteServerEndPoint);

for (int i= 0; i < 1000 ; i++)
{
string count = i.ToString();
Byte[] socketbuffer = new byte[count.Length];
for (int j = 0; j < count.Length; j++)
socketbuffer[j] = System.Convert.ToByte(count[j]);

tcpsocket.Send(socketbuffer, 0, socketbuffer.Length,
SocketFlags.None);
}
tcpsocket.Close();
}
Nov 16 '05 #1
3 4548
See SocketOptionName.NoDelay

"Disables the Nagle algorithm for send coalescing."
"Hiroyuki Tanaka" <Hi**************@excite.co.jp> wrote in message
news:68**************************@posting.google.c om...
Dear Readers,

I am trying to learn C# network programming and I have discovered that
Windows TCPIP often joins small packets and send them as one to the
remote server. This appears to be a default behaviour of TCPIP?

I am attempting to send 1000 TCPIP packets to a remote sever with only
a sequence number (and soon a variable length message (only a few
characters)) in them.

packet 0 0
packet 1 1
packet 2 2
...
packet 100 100
...
packet 999 999

So at my remote server I wish to receive 1000 packets each with a
single number in each packet.

However at my server I get packets that include all the number to 100
(for example) joined together.

How can I force the C# socket send to send just the buffer I want in
each packet?

Thanks

Tanaka

private void send_Click(object sender, System.EventArgs e)
{
int port = 5000;
Socket tcpsocket;

tcpsocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);

IPEndPoint remoteServerEndPoint = new IPEndPoint(IPAddress.Parse(
"10.1.1.1" ), port );

tcpsocket.Connect(remoteServerEndPoint);

for (int i= 0; i < 1000 ; i++)
{
string count = i.ToString();
Byte[] socketbuffer = new byte[count.Length];
for (int j = 0; j < count.Length; j++)
socketbuffer[j] = System.Convert.ToByte(count[j]);

tcpsocket.Send(socketbuffer, 0, socketbuffer.Length,
SocketFlags.None);
}
tcpsocket.Close();
}

Nov 16 '05 #2
Hi Tanaka,

TCP does not respect packet boundaries. Switching the Nagle algoritm off
does not garantie eather. You can use UDP, but it is very unreable, with UDP,
packet boundaries are respected, but not the order of sent, and not unique
delivery, a packet can get lost or you can receive same packet many times
(depending on routing in the network).

However I have seen in help that there is a Socket option to respect
boundaries. However you can easy do the same in your receiving application.
Eather you send first the length of the packet as leading byte/word/dword, or
work with a packet terminating character.

For the former you receive until you have gathered a byte/word/dword and see
the length, then you receive until you got length bytes. If there is more in
buffer then itis begin of next packet etc..

for the latter you just have to check the packet terminating character. But
this character may never be in the data itself. For ascii data is no problem
of course, but for binary data, you have to escape this byte.

hope this helps a little. I'm very new to NET but know a lot of winsock ;)

rgds, Wilfried
http://www.mestdagh.biz
Nov 16 '05 #3
> See SocketOptionName.NoDelay
"Disables the Nagle algorithm for send coalescing."


This will not guarantie that packet boundaries will be respected, becasue
winsock can concatenate the packets at receiving side as well, and if packet
is not yet transmitted at sending side (due to TCP handshaking), it is still
possible it is concatenated at sending side.

Switchin Nagle of will send faster because winsock will not wait a while for
an eventually next packet to be send. Also if it is a handshaking protocol
then Nagle has to be switched of at both sides.

rgds, Wilfried
http://www.mestdagh.biz
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Stephan Steiner | last post by:
Hi I have a networking application that periodically needs to go into sleep mode (from an application point of view, I'm simply suspending the receiver thread until it's time to start listening...
3
by: Stephan Steiner | last post by:
Hi I have a small program listening to UDP broadcast datagrams that are periodically sent out. It will stop listening for a certain period if either a sufficient number of packets has been...
4
by: Brian Rice | last post by:
I have a socket application that is sending and receiving packets asynchronously. It works great except, when I receive packets that are larger than my receive buffer which then generate several...
4
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed...
9
by: Macca | last post by:
Hi, I have a synchronous socket server which my app uses to read data from clients. To test this I have a simulated client that sends 100 byte packets. I have set up the socket server so...
0
by: Macca | last post by:
Hi, I am writing an asychronous socket server to handle 20+ simulataneous connections. I have used the example in MSDN as a base. The code is shown at end of question. Each connection has a...
1
by: ssherm01 | last post by:
I'm using MS VS C++ .NET. I've written a multicast test utility that can send IGMP packets for a user specified group address. As well, it can send and receive UDP packets for a specified...
2
by: manasap | last post by:
Hi all! I've written a server and a client application using asynchronous sockets.The client sends data packets for every 7 seconds.The server receives the packets. This process proceeds...
4
by: Zytan | last post by:
This may be the dumbest question of all time, but... When I set the packet size, does it mean ALL packets are that size, no matter what? Let's say the packet size is 8KB, and I send a 5 byte...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.