473,595 Members | 2,589 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Protocol over TCP

Hi,

I have been writing a Communications protocol component in C# that will
basically be used in client and server applications. The sole purpose of the
component is to allow applications to be able to RELIABLY send
structures/objects across a network (LAN) to other applications that also use
the component. Objects are encapsulated into a packet which has a header to
identify to the receiving end what type of packet it is.

The way the component has been implemented at this point is explained below:

- On sending, the component serializes the object in wants to send into a
stream of bytes (packet) first and then sends it by calling the Socket
object's synchronous Send() method. Before returning, this send process needs
to make sure that the other end received the packet (this is where the
reliability part comes into the picture). To know that a packet was received
correctly, the send process waits 200ms (RTO - ReTransmissionT imeout) for an
ACK packet to come back (the reception of this ACK packet is explained below
in the receive section**). If it doesn't receive an ACK by then, it resends
the same packet. It continues to resend the packet every 200ms for 30 secs.
If it still hasn't received it by then, it disconnects the connection to the
remote endpoint.

- On receiving, bytes are received asynchronously by calling the Socket
object's BeginReceive() and EndReceive() methods. The receiving process
blocks until bytes become available on the receiving port. So when bytes do
become available on the receiving port, they are read and deserialized back
to the original object. In order not to miss any further incoming bytes, the
currently received object is passed to another thread for processing. Hence
the receiving process can return and call BeginReceive() again and wait for
more bytes to come in.
Now, the new thread that just got spawned will process the packet.
Depending on the header of the packet, this thread will have different ways
in dealing with the packet. Say we have 2 types of packets - DATA and ACK. If
the receiving end received a DATA packet, then it sends back an ACK to the
sender to signify correct reception.** But if the packet was an ACK, then it
sets a synchronize flag to let the sending process know that an ACK was
received for a particular packet.

This component is now being tested with a TestServerAppli cation and a
TestClientAppli cation. The server simply waits for clients to connect. When
the client connects, the server accepts it and waits. The system was set up
such that the client app sends a 2KB packet to the server and the server
basically echoes it back. When the client receives the echo, it also echoes
that back and so they play "packet tennis". This is simply to test the
component. The size of an ACK packet is 44 bytes. So this is what happens:

- client sends 2KB to server
- server receives and sends a 44-byte ACK, and then the echo 2KB packet to
the client
- client receives the ACK (which ends the first send), and then also
receives the echoed packet... client then sends an ACK to the server, as well
as the echo packet
- and the process repeats...

Everytime the client app receives an packet, it prints out the contents on a
RichTextBox and then clears it for the next packet. So visually, the client's
RichTextBox blinks with the contents of received packets. When i have this in
the code, it works fine. However, if i comment out the section that prints
the packet contents (so the RichTextBox will always be blank), something
bizarre occurs:

For some reason, at certain points in time, the asychronous Receive calls
(at the client end) receive bytes that are of inconsistent size and it always
seems to be decreasing by 44 bytes - the size of an ACK packet. So at first,
the component may consistently receive 2048 bytes, but then later on it
receives 2004, then 1960, then 1916, and so on .. this will obviously
register as incomplete packets on the component and cause errors. This
behaviour only happens when the code which prints out the packet contents on
the client app is not included. But if it is, the thing works fine and the
packet sizes being received are all 2KB. What is going on here??

Please excuse the lengthy post. I needed to explain how my component worked
for easier analysis on your behalf. Thanks in advance.

Michael--J.
Jul 21 '05 #1
4 1523


Michael--J wrote:
I have been writing a Communications protocol component in C# that will
Instead of implementing your own solution, you could use remoting.
- On receiving, bytes are received asynchronously by calling the Socket
object's BeginReceive() and EndReceive() methods. The receiving process
Avoid async-IO unless absolutely forced to do it. It is *very*
complicated to get right -- really!
Everytime the client app receives an packet, it prints out the contents on a
RichTextBox and then clears it for the next packet. So visually, the client's
RichTextBox blinks with the contents of received packets. When i have this in
the code, it works fine. However, if i comment out the section that prints
the packet contents (so the RichTextBox will always be blank), something
bizarre occurs:
Timing influences the way multi-threaded and asynchroneus programs work.
Try making a *very* small example program and do your tests on that,
so you can understand what's going on.

You can try insert some Thread.Sleep(0) in order to try and provoke some
timing errors by having the threads rescheduled.
packet sizes being received are all 2KB. What is going on here??
Could easily be a synchronization bug in your async-IO. It's *very* hard
to get async I/O right.

Are you allowed to read and write to the Socket from multiple treads at
once? I suspect not. Perhaps writing the ACK to the socket moves a
pointer in the Socket's buffer -- which would explain the behaviour you
are experiencing.
Please excuse the lengthy post. I needed to explain how my component worked
for easier analysis on your behalf. Thanks in advance.


I would guess that most of the content of the post is unrelated to your
problem.

BTW1: I don't really understand why you are using async IO for reading
the input. you could just as well use sync, with exactly the same result.

BTW2: Are you certain you will always receive 1 full packet per read of
the Socket?

--
Helge Jensen
mailto:he****** ****@slog.dk
sip:he********* *@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Jul 21 '05 #2
Hello Helge,

I appreciate the reply and you taking your time to read my post. Please read
inline:

"Helge Jensen" wrote:


Michael--J wrote:
I have been writing a Communications protocol component in C# that will
Instead of implementing your own solution, you could use remoting.


Yes you are correct, I could have used remoting. I would really love to use
that technology because it would definitely take a huge burden off my
shoulders. Our component however, is being designed so that it can be used in
PC applications that run under the .NET Framework (Full) environment, as well
as embedded applications (that run on embedded devices with Windows CE 5.0)
that only run in the .NET Compact Framework environment. And since the
Compact Framework does not support .NET Remoting, I could not go down that
path.
- On receiving, bytes are received asynchronously by calling the Socket
object's BeginReceive() and EndReceive() methods. The receiving process


Avoid async-IO unless absolutely forced to do it. It is *very*
complicated to get right -- really!


Yeah right?!! Is Async-IO really that difficult? If I only knew that
earlier, I may have resorted to sych methods only. :(
Everytime the client app receives an packet, it prints out the contents on a
RichTextBox and then clears it for the next packet. So visually, the client's
RichTextBox blinks with the contents of received packets. When i have this in
the code, it works fine. However, if i comment out the section that prints
the packet contents (so the RichTextBox will always be blank), something
bizarre occurs:


Timing influences the way multi-threaded and asynchroneus programs work.
Try making a *very* small example program and do your tests on that,
so you can understand what's going on.

You can try insert some Thread.Sleep(0) in order to try and provoke some
timing errors by having the threads rescheduled.


Yes you are right, timing does play a great deal in this component. I
noticed that putting a Thread.Sleep(80 ) in place of the code that prints out
packet contents on the client app’s RichTextBox, made system run fine.
However, putting a Thread.Sleep(20 ) did not. If I change my implementation to
a synch-IO approach, will that help make things run better? Guaranteed? Or
will I still get timing issues?

The example programs i have written, i thought, seem small. They are
basically a server and client that play "packet tennis". I need to be able to
stress test the server with about 500 connected clients all playing packet
tennis. When you said *very* small program, what type of program do you mean?
packet sizes being received are all 2KB. What is going on here??


Could easily be a synchronization bug in your async-IO. It's *very* hard
to get async I/O right.


That is really a shame. Thanks for the heads up. I think another issue that
may cause problems is the resending. When i have about 100 clients connected
the server maybe struggling to keep up with them, and each client resends
packets at an interval of 200ms (RTO). Maybe the high influx of packets
corrupts the streams or something?? I was thinking of increasing the RTO to
about 1 sec and see how that goes. I might give that a try.
Are you allowed to read and write to the Socket from multiple treads at
once? I suspect not. Perhaps writing the ACK to the socket moves a
pointer in the Socket's buffer -- which would explain the behaviour you
are experiencing.

Yes i think you are allowed to read and write to the Socket from multiple
threads. I mean, that's exactly what i'm doing. The sending process is
totally separate from the receiving process but they both access the same
Socket. Maybe that observation could be the key... hmm... Maybe that is why i
am experiencing the behaviour i mentioned... you could be on the right track
Helge...
Please excuse the lengthy post. I needed to explain how my component worked
for easier analysis on your behalf. Thanks in advance.


I would guess that most of the content of the post is unrelated to your
problem.

BTW1: I don't really understand why you are using async IO for reading
the input. you could just as well use sync, with exactly the same result.


The only reason why i used asych IO was simply because it made things
"easier" and there was no blocking taking place. Using sych IO will give
exactly the same result? Including the timing issues? I hope not - i may give
it a try...
BTW2: Are you certain you will always receive 1 full packet per read of
the Socket?

No. As a matter of fact there are instances where i only receive a portion
of the packet at a time. I have actually taken this into account in my code
and tested it with dummy packets as well. The behaviour i was telling you
about where the packets sizes decrease (and hence show up as incomplete
packets) get processed in that portion of the code - but the thing is i dont
know whether their remaining bytes get received or not.
--
Helge Jensen
mailto:he****** ****@slog.dk
sip:he********* *@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-

Jul 21 '05 #3


Michael--J wrote:
that only run in the .NET Compact Framework environment. And since the
Compact Framework does not support .NET Remoting, I could not go down that
path.
That's understandable.
Avoid async-IO unless absolutely forced to do it. It is *very*
complicated to get right -- really!


Yeah right?!! Is Async-IO really that difficult? If I only knew that
earlier, I may have resorted to sych methods only. :(


With async-io there really isn't much of a "program-flow", which makes
it very hard to use the usual ways of understanding/arguing how a
program works.
You can try insert some Thread.Sleep(0) in order to try and provoke some
timing errors by having the threads rescheduled. Yes you are right, timing does play a great deal in this component. I
noticed that putting a Thread.Sleep(80 ) in place of the code that prints out
packet contents on the client app’s RichTextBox, made system run fine.
The "Thread.Sleep(0 )" is an old hack to try and provoke synchronization
errors by littering the code with thread-switches. Thread.Sleep(0) (in
win32 atleast, not sure about .NET) yields the rest of a proccess'
timeslice to another thread.

Maybe an instrumenter that puts sleep-calls inbetween the original .NET
code would be nice to have for testing multi-threaded programs..... hmmmm...
However, putting a Thread.Sleep(20 ) did not. If I change my implementation to
a synch-IO approach, will that help make things run better? Guaranteed? Or
will I still get timing issues?
The problem will propbably not go away, but you will have an improved
chance of finding out whats going on.

If writing to the textbox/console/... is too slow to have the program
exhibit incorrect behaviour you can make an array of logs, something
like this:

public class CyclicLog {
public object[] Logs;
int current;
public CyclicLog(int count) { this.Logs = new object[count]; }
public void Log(object o) {
lock ( this ) {
Logs[current] = o;
current = (current + 1) % Logs.Length;
}
}
}

So you can have a kind of program-flow trace while still getting the
incorrect behaviour.
Could easily be a synchronization bug in your async-IO. It's *very* hard
to get async I/O right. That is really a shame. Thanks for the heads up. I think another issue that
may cause problems is the resending. When i have about 100 clients connected
the server maybe struggling to keep up with them, and each client resends
packets at an interval of 200ms (RTO). Maybe the high influx of packets
corrupts the streams or something?? I was thinking of increasing the RTO to
about 1 sec and see how that goes. I might give that a try.
It may help, but only indirectly -- by reducing the amount of concurrency.
BTW1: I don't really understand why you are using async IO for reading
the input. you could just as well use sync, with exactly the same result. The only reason why i used asych IO was simply because it made things
"easier" and there was no blocking taking place. Using sych IO will give
exactly the same result? Including the timing issues? I hope not - i may give
it a try...
Instead of the pattern:

ReadHandler(Asy ncResult r) {
...
s.BeginRead(... , new AsyncCallBack(R eadHandler));
}
void Main() {
s.BeginRead(... , new AsyncCallBack(R eadHandler));
}

you can do:

ReadHandler() {
while(...) {
int read = s.Read(...);
...
}
}
void Main() {
new Thread(new ThreadStart(Rea dHandler));
}

Don't expect it to have any influence on the behaviour (it's essentially
the same code, just by different means), but the code becomes a bit more
understandable, especially in the face of exceptions.

From the docs of {Stream,Network Stream}.BeginRe ad:

"Multiple simultaneous asynchronous requests render the request
completion order uncertain."

So it seems you are allowed to do multiple reads.
BTW2: Are you certain you will always receive 1 full packet per read of
the Socket?

No. As a matter of fact there are instances where i only receive a portion
of the packet at a time. I have actually taken this into account in my code


OK, so that pit isn't waiting for you, good ;)

As a last resort, simplify the program while keeping it doing the
invalid behaviour... it's a timeconsuming but effective technique.

--
Helge Jensen
mailto:he****** ****@slog.dk
sip:he********* *@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Jul 21 '05 #4
Hello Helge,

So sorry for the late reply but i was away on holiday.

I am back now and i figured out my problem. The problem was caused by the
fact that i was processing incomplete packets and putting them in temporary
byte arrays. I have now simplified the component and basically set it to
ignore incomplete packets and wait for the packet to be resent and received
as a full packet.

Now i have come across another problem. I am now running the TestClient on
an embedded board loaded with Win CE 5.0. Obviously, there isnt much RAM or
CPU to work with. Running the TestServer on my PC and the TestClient on the
board the same way i did before, it works for a while and then for some
reason, on the embedded side, an OutOfMemoryExce ption occurs. It occurs at a
random point in time when a structure is received from the network and is
passed to another thread for processing. When Thread.Start is called, the
exception occurs. I then ran the app again with the RAM settings opened (from
Control Panel->Settings->System) and noticed that the Program Memory was
decreasing as it ran. However, when the exception occurs, there seems to be a
few MBs left in the Program Memory side. I dont understand, why did this
happen? Thanks.

"Helge Jensen" wrote:


Michael--J wrote:
that only run in the .NET Compact Framework environment. And since the
Compact Framework does not support .NET Remoting, I could not go down that
path.


That's understandable.
Avoid async-IO unless absolutely forced to do it. It is *very*
complicated to get right -- really!


Yeah right?!! Is Async-IO really that difficult? If I only knew that
earlier, I may have resorted to sych methods only. :(


With async-io there really isn't much of a "program-flow", which makes
it very hard to use the usual ways of understanding/arguing how a
program works.
You can try insert some Thread.Sleep(0) in order to try and provoke some
timing errors by having the threads rescheduled.
Yes you are right, timing does play a great deal in this component. I
noticed that putting a Thread.Sleep(80 ) in place of the code that prints out
packet contents on the client app’s RichTextBox, made system run fine.


The "Thread.Sleep(0 )" is an old hack to try and provoke synchronization
errors by littering the code with thread-switches. Thread.Sleep(0) (in
win32 atleast, not sure about .NET) yields the rest of a proccess'
timeslice to another thread.

Maybe an instrumenter that puts sleep-calls inbetween the original .NET
code would be nice to have for testing multi-threaded programs..... hmmmm...
However, putting a Thread.Sleep(20 ) did not. If I change my implementation to
a synch-IO approach, will that help make things run better? Guaranteed? Or
will I still get timing issues?


The problem will propbably not go away, but you will have an improved
chance of finding out whats going on.

If writing to the textbox/console/... is too slow to have the program
exhibit incorrect behaviour you can make an array of logs, something
like this:

public class CyclicLog {
public object[] Logs;
int current;
public CyclicLog(int count) { this.Logs = new object[count]; }
public void Log(object o) {
lock ( this ) {
Logs[current] = o;
current = (current + 1) % Logs.Length;
}
}
}

So you can have a kind of program-flow trace while still getting the
incorrect behaviour.
Could easily be a synchronization bug in your async-IO. It's *very* hard
to get async I/O right.
That is really a shame. Thanks for the heads up. I think another issue that
may cause problems is the resending. When i have about 100 clients connected
the server maybe struggling to keep up with them, and each client resends
packets at an interval of 200ms (RTO). Maybe the high influx of packets
corrupts the streams or something?? I was thinking of increasing the RTO to
about 1 sec and see how that goes. I might give that a try.


It may help, but only indirectly -- by reducing the amount of concurrency.
BTW1: I don't really understand why you are using async IO for reading
the input. you could just as well use sync, with exactly the same result.
The only reason why i used asych IO was simply because it made things
"easier" and there was no blocking taking place. Using sych IO will give
exactly the same result? Including the timing issues? I hope not - i may give
it a try...


Instead of the pattern:

ReadHandler(Asy ncResult r) {
...
s.BeginRead(... , new AsyncCallBack(R eadHandler));
}
void Main() {
s.BeginRead(... , new AsyncCallBack(R eadHandler));
}

you can do:

ReadHandler() {
while(...) {
int read = s.Read(...);
...
}
}
void Main() {
new Thread(new ThreadStart(Rea dHandler));
}

Don't expect it to have any influence on the behaviour (it's essentially
the same code, just by different means), but the code becomes a bit more
understandable, especially in the face of exceptions.

From the docs of {Stream,Network Stream}.BeginRe ad:

"Multiple simultaneous asynchronous requests render the request
completion order uncertain."

So it seems you are allowed to do multiple reads.
BTW2: Are you certain you will always receive 1 full packet per read of
the Socket?

No. As a matter of fact there are instances where i only receive a portion
of the packet at a time. I have actually taken this into account in my code


OK, so that pit isn't waiting for you, good ;)

As a last resort, simplify the program while keeping it doing the
invalid behaviour... it's a timeconsuming but effective technique.

--
Helge Jensen
mailto:he****** ****@slog.dk
sip:he********* *@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-

Jul 22 '05 #5

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

Similar topics

34
3274
by: jblazi | last post by:
Let us assume I have a list like and would like to transoform it into the string '{1,2},{7,8},{12,13}' Which is the simplest way of achiebing this? (The list is in fact much longer and I may have to cut the resulting strings into chunks of 100 or
1
3219
by: Dom Leonard | last post by:
Hi all, I occasionally use the javascript protocol in window.open to retrieve a window property of the opener for use as HTML source: window.htmlSrc="<html>...blah ....<\/html>"; window.open("javascript:opener.htmlSrc", testWindow); The technique was absolutely needed in NS4.xx to overcome reentrancy problems with document.writing to generated windows, and has been useful
6
3815
by: Charles Law | last post by:
This is going to seem like a basic OO question, but it comes up and bites me every now and again. Suppose we have a multi-tiered protocol to implement, what is the logical, OO way to design the handler? For example, let's say that we have to implement the ISO 7-layer protocol, or something like an Allen-Bradley master-slave protocol. At the lowest layer we might only need to top and tail the data being transported, such as DLE STX DLE...
0
1221
by: shoorik | last post by:
Folks, SiteID is an exciting new protocol that I've been planning and designing during the past couple of months. It's currently in early release, but I've been hoping to make somewhat of a following and a development community around it to take it to its full potential. In essense, SiteID is a simple XML protocol that is used to describe websites as logical entities, rather than relying on loose links between webpages.
5
1925
by: Fatih BOY | last post by:
Hi, How can i register my own protocol like app-help://somepage.html which searchs typed files under a predefined folder (like document folder under installation root) ? Fatih BOY
12
5512
by: Laszlo Zsolt Nagy | last post by:
Hello, I would like to develop a new network protocol, where the server and the clients are Python programs. I think to be effective, I need to use TCP_NODELAY, and manually buffered transfers. I would like to create a general messaging object that has methods like sendinteger recvinteger
4
2471
by: Bob Badger | last post by:
Hi, Simple question (although I guess with a complicated answer). Is HTTP an async protocol? For instance, if I send a message to a c# webservice via http what is the protocol actually doing? Thanks in advance Steffan
1
2682
by: Benry | last post by:
Hi guys. I hope I can discuss Twisted here. If not, direct me to the correct place please. My question(s): I'm working on a custom network protocol (application layer in TCP/IP model) for a custom network. Please don't try to talk me out of this. I'm limiting many of the other protocols, because this is in a highly secure network, and latency/bandwidth is key. The packet sizes are minimized by the protocol design, which is ideal. ...
6
16420
by: tycoonajayverma | last post by:
Hi, Can some one help me out for these following errors 24-OCT-2006 11:52:03 * (CONNECT_DATA=(SID=pro1)(CID=(PROGRAM=)(HOST=report2)(USER=crystal))) * (ADDRESS=(PROTOCOL=tcp)(HOST=10.36.50.114)(PORT=48564)) * establish * pro1 * 0 24-OCT-2006 11:52:06 * 12560 TNS-12560: TNS:protocol adapter error TNS-00530: Protocol adapter error Solaris Error: 130: Software caused connection abort 24-OCT-2006 11:56:21 * 12560
4
48723
by: Pool | last post by:
I tried to connect DB2 (Sitting in Unix server at my client location) using Db2 connect V8. I am getting the following error message. I tried all the possible options BUt the error is same.. See each type belpw Standard connection failed.
0
7882
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8379
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8017
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
3872
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3908
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2391
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1490
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1222
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.