473,588 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# .Net Socket.Send Bug?

Hi, I've got a C# client/service application using a Socket object to
communicate. I have a socket bug that I can't solve without
kludged-code. Does anyone know a more elegant solutions? Is this a
known bug with the .NET Socket object?

Under high stress data sending, I occasionally receive duplicate data
on the server which really throws a wrench into the system. The
duplicate data I receive is always part of the tail end of the last
message sent.

Example:
Message 1: FirstMessage
Message 2: SecondMessage
Message 3: ssage (DUPLICATE BUG!)
Message 4: FourthMessage

I need to eliminate this problem. The only solution I have that works
is to add a Thread.Sleep(10 ) immediately after Socket.Send(buf fer). If
I add that sleep, it works perfectly every time.

I have tried using a NetworkStream instead of a Socket, and I have
tried BeginSend/EndSend and they all demonstrate the exact same
problem.

This only happens when I am sending message very fast in a loop such
as:

for (int i=0; i < 1000; i++)
{
_socket.Send(bu ffer);
}

or

for (int i=0; i < 1000; i++)
{
// Async way
IAsyncResult asyncSend = _socket.BeginSe nd(regPackage, 0,
regPackage.Leng th, System.Net.Sock ets.SocketFlags .None, null, null);
_socket.EndSend (asyncSend);
}

or

for (int i=0; i < 1000; i++)
{
SendData(_socke t, regPackage);
}

public int SendData(TSSock et s, byte[] data)
{
int total = 0;
int size = data.Length;
int dataleft = size;
int sent;
while (total < size)
{
sent = s.Send(data, total, dataleft,
System.Net.Sock ets.SocketFlags .None);
total += sent;
dataleft -= sent;
}
return total;
}
Again, the only solution that has worked for me is adding a
Thread.Sleep(10 ), or some other code that keeps the processor busy so
that the Send has time complete. I tried shorter wait times, but 1 or
2 doesn't give enough time, so I played it safer and choose 10.

The is a sample of how I've currently got it working:

for (int i=0; i < 1000; i++)
{
SendData(_socke t, buffer);
Thread.Sleep(10 );
}

HELP!

Nov 17 '05 #1
2 2702
Not sure, but are you trying to send the same buffer multiple times?

--
William Stacey [MVP]

"Matt" <ma*******@gmai l.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
Hi, I've got a C# client/service application using a Socket object to
communicate. I have a socket bug that I can't solve without
kludged-code. Does anyone know a more elegant solutions? Is this a
known bug with the .NET Socket object?

Under high stress data sending, I occasionally receive duplicate data
on the server which really throws a wrench into the system. The
duplicate data I receive is always part of the tail end of the last
message sent.

Example:
Message 1: FirstMessage
Message 2: SecondMessage
Message 3: ssage (DUPLICATE BUG!)
Message 4: FourthMessage

I need to eliminate this problem. The only solution I have that works
is to add a Thread.Sleep(10 ) immediately after Socket.Send(buf fer). If
I add that sleep, it works perfectly every time.

I have tried using a NetworkStream instead of a Socket, and I have
tried BeginSend/EndSend and they all demonstrate the exact same
problem.

This only happens when I am sending message very fast in a loop such
as:

for (int i=0; i < 1000; i++)
{
_socket.Send(bu ffer);
}

or

for (int i=0; i < 1000; i++)
{
// Async way
IAsyncResult asyncSend = _socket.BeginSe nd(regPackage, 0,
regPackage.Leng th, System.Net.Sock ets.SocketFlags .None, null, null);
_socket.EndSend (asyncSend);
}

or

for (int i=0; i < 1000; i++)
{
SendData(_socke t, regPackage);
}

public int SendData(TSSock et s, byte[] data)
{
int total = 0;
int size = data.Length;
int dataleft = size;
int sent;
while (total < size)
{
sent = s.Send(data, total, dataleft,
System.Net.Sock ets.SocketFlags .None);
total += sent;
dataleft -= sent;
}
return total;
}
Again, the only solution that has worked for me is adding a
Thread.Sleep(10 ), or some other code that keeps the processor busy so
that the Send has time complete. I tried shorter wait times, but 1 or
2 doesn't give enough time, so I played it safer and choose 10.

The is a sample of how I've currently got it working:

for (int i=0; i < 1000; i++)
{
SendData(_socke t, buffer);
Thread.Sleep(10 );
}

HELP!

Nov 17 '05 #2
I'm really not trying to send the same buffer multiple times, that's
just an example of what I mean.

Nov 17 '05 #3

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

Similar topics

4
18102
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 as I find appropriate. I am using the socket asynchronously by calling the BeingSend and BeginReceive calls. I would like to be able to call shutdown and close asynchronously if possible.
2
4146
by: Nuno Magalhaes | last post by:
I've got a simple problem I guess. How do I know when a connection is terminated without losing any data? I do something like the code below, but sometimes between socket.Receive and socket.Send I get the last chunk of data and am not able to retrieve it anymore cause the socket will be dead. Loop: { socket.Receive <----------- data arrives
2
15316
by: djc | last post by:
I read a network programming book (based on framework 1.1) which indicated that you should 'never' use the RecieveTimeout or the SendTimeout 'socket options' on TCP sockets or you may loose data. I now see the socket.RecieveTimeout 'property' in the visual studio 2005 help documentation (framework 2.0) and it has example of it being used with TCP socket. This propery is also listed as 'new in .net 2.0'. 1) is the socket.RecieveTimeout...
11
7700
by: hazz | last post by:
smtpClient.Send(message) is causing me problems as per specifics in the trace below. Email is sent but not without this error typically upon sending the second email, but sometimes when running the app, even the first time. The application will be required to be sending out repeated emails, about one every second or two. Must this be done asynchronously? Thank you. -Greg I get the generic error messages;
3
4280
by: BuddyWork | last post by:
Hello, Could someone please explain why the Socket.Send is slow to send to the same process it sending from. Eg. Process1 calls Socket.Send which sends to the same IP address and port, the receiver is running within Process1. If I move the receiver into Process2 then its fast. Please can someone explain.
0
3153
by: Buddy Home | last post by:
There is two examples of code. Example 1. Send and Receive within the same process. Put this code in a console app called SendAndReceive and run the code. using System; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Runtime.Serialization.Formatters.Binary;
14
12747
by: ahlongxp | last post by:
Hi, everyone, I'm implementing a simple client/server protocol. Now I've got a situation: client will send server command,header paires and optionally body. server checks headers and decides whether to accept(read) the body. if server decided to throw(dump) the request's body, it'll send back a response message, such as "resource already exists" and close the connection.
2
3658
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 smoothly without any errors but,sometimes I get the following error. "An existing connection was forcibly closed by the remote host." Why is this happening when i am not closing the client program? Could someone guide me in this issue
2
2085
by: Ali Hamad | last post by:
Hello All : A socket question from a networking newbie. I need to create a server that: 1) receive a message from client. 2) check that message and response to it. 3) the client get the server message and send another message. 4) finally, the server receive the message and close the connection.
0
7929
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7860
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
8222
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
8354
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
7984
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
6634
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3883
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2371
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
1458
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.