0
votes

I'm working on a C# WebSocket server (currently supported by http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17). The server is working with the Socket object of the .NET for the server to listen and for each client to send and receive messages.

I built a web client that connect to the server, It can connect successfully and i can send messages between clients.

Everything is working great!

Now, if i'm connecting to the server and leave the client for a while without sending messages, the server throwing an exception that says:

Int32 Send(Byte[], Int32, Int32, System.Net.Sockets.SocketFlags):An existing connection was forcibly closed by the remote host.

The exception, as you can see is from the Send method of the client socket in the server, this is looks very wired because i didn't sent any data from the client and no one sending data to this client back so how can it be that the Send method can throw an exception and why this exception is thrown?

2
Can you post the send code (in particular, the line that threw the exception)?spender
...also, are you aware the WebSockets will be supported directly in .Net4.5?spender
yeah i'm aware with the WebSocket in .Net 4.5 but i don't know when it will be published.udidu
and the line that throws the exception is: this.clientSocket.Send(sendData.ToArray()); another are thrown from: this.clientSocket.EndReceive(_result); as i said, i didn't sending any data...udidu

2 Answers

0
votes

I assume that you have exclude the usage of different protocols between the servers and the clients (silly assumption, but you never know).

If your code reaches the Send method without a prior Receive from the client, then it's obvious that something is wrong with the server code. Use trace and/or log to get more information even for abc's like entering wait to receive, receiving, received, exiting receiving etc.

0
votes

It's called a timeout!

WebSockets are just a wrapper around TCP/IP raw sockets (Socket class in .NET) - which timeout if nothing is sent, and nothing is keeping the connection alive.

AFAIK currently the WebSocket API isn't very well defined as far as how to keep the connection alive. I was experiencing the same and had to just switch over to using a ping (empty message) to keep the connection alive (I'm using the Microsoft sockets implementation).

If you're reinventing the wheel for a non final spec, just remember that you'll have to keep reinventing it every time the spec changes. I specifically chose to use the Microsoft sockets preview so that when it's released I'm pretty much not going to have to change any code. I don't run in IIS - I run as a console app and it's working mostly great so far but I have very very few users.

Note: The problem i was having that led me to find this question was if I send 10 messages without receiving a reply then the connection is closed. I'm still looking into why this is - whether its a bug / feature of WebSockets or a feature of the Socket class. it's possible I'm hitting a 65kb limit but my messages are small and I don't think that's why. Just be aware of this when testing whatever you're working on becasue it gives the same error you got.