hey i want to ask a question about asynchronous socket communication on c#. everything is working well for now apart from closing clients.开发者_C百科 server doesnot close immediately the worker socket for client when a client close its conneciton. it closes a few time later. how can i resolve this problem??
check this link http://msdn.microsoft.com/en-us/library/fx6588te.aspx#2 my problem is that I can't keep the connection open after I receive a message from the client. If I do as said on that sample the connection is closed immediately after receiving the message. If I don't close the connection I can only receive one message and nothing more. If you have any solution to this throw it this way.
I got it!
If anybody else has this problem they should do the following. Change this code:
content = state.sb.ToString()
to this:
content = state.sb.ToString().TrimEnd(New Char() {ChrW(13)})
then you should change this:
If content.IndexOf("<EOF>") > -1 Then
to this:
If content.IndexOf(New Char() {ChrW(13)}) > -1 Then
this will receive Enter (chrw(13)) as the end of line.
then here:
Console.WriteLine("Read {0} bytes from socket. " + vbLf + " Data : {1}", content.Length, content)
' Echo the data back to the client.
Send(handler, content)
you should do this:
Console.WriteLine("Read {0} bytes from socket. " + vbLf + " Data : {1}", content.Length, mid(content,1,content.length -2))
' Echo the data back to the client.
'Send(handler, content)
content = String.Empty
state.sb.Clear()
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReadCallback), state)
And your done.
精彩评论