Frage

I got this Socket.Send code, my protocol is tcp.

byte[] commandBytes = Encoding.ASCII.GetBytes(command);
int iBytesSent = socket.Send(commandBytes, commandBytes.Length, SocketFlags.None);
Debug.WriteLine("SEND: " + command);

I have over 10,000 commands to send, each command is around 100 characters. I call the Socket.Send in a foreach loop for 10,000 commands.

I notified that the Socket.Send code blocks the current thread and takes forever to finish. (The debug.WriteLine stop printing to output console.)

So my question:

Is it possible that because I send the commands too fast and this cause the stream full?

So it have to wait until the stream is available and continue to send?

I also notified that there is a moment the Socket.Send block the code for 5-10 seconds, and then continue. But as time passed, 5-10 seconds becomes 20-30 and then it takes forever.

War es hilfreich?

Lösung

Yes, it is possible for Send to block, although in most code you rarely see it. If you don't want to tie up a thread, you could use async socket IO (BeginSend/EndSend, or SendAsync). However, this doesn't make it any faster - it just means that you aren't blocking a thread while the socket is busy. Are you perhaps out-pacing the other end of this connection?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top