user1250264
user1250264

Reputation: 905

How to abort when socketclient is waiting for socket server response aka TimeOut

I wrote a socket client that send data to the socket server and waits for a response from the socket server on the received data size. For error handling, if the socket client is waiting too long, I want it to abort the connection like a timeout factor. How do I do that. Here is my code:

clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSocket.Connect(new IPEndPoint(IPAddress.Parse(host), port));

clientSocket.Send(Encoding.ASCII.GetBytes(data));

// Get the total length
clientSocket.Receive(data);

Any help is greatly appreciated.

Upvotes: 0

Views: 310

Answers (2)

mat_vee
mat_vee

Reputation: 85

As per my knowledge you are using TCP. And TCP ensures reliable delivery. i.e. if server is down you get value zero in "clientSocket". Also if you want to explicitly send data from server you can set timeout on both sides at server and client side by {ReceiveTimout} property.

Upvotes: 0

dee-see
dee-see

Reputation: 24088

Set the ReceiveTimeout property on your Socket object.

http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.receivetimeout.aspx

Upvotes: 3

Related Questions