471,122 Members | 890 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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(buffer). 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(buffer);
}

or

for (int i=0; i < 1000; i++)
{
// Async way
IAsyncResult asyncSend = _socket.BeginSend(regPackage, 0,
regPackage.Length, System.Net.Sockets.SocketFlags.None, null, null);
_socket.EndSend(asyncSend);
}

or

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

public int SendData(TSSocket 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.Sockets.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(_socket, buffer);
Thread.Sleep(10);
}

HELP!

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

--
William Stacey [MVP]

"Matt" <ma*******@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.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(buffer). 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(buffer);
}

or

for (int i=0; i < 1000; i++)
{
// Async way
IAsyncResult asyncSend = _socket.BeginSend(regPackage, 0,
regPackage.Length, System.Net.Sockets.SocketFlags.None, null, null);
_socket.EndSend(asyncSend);
}

or

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

public int SendData(TSSocket 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.Sockets.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(_socket, 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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Nuno Magalhaes | last post: by
2 posts views Thread by djc | last post: by
11 posts views Thread by hazz | last post: by
2 posts views Thread by manasap | last post: by
2 posts views Thread by Ali Hamad | last post: by

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.