c# - How to reconnect socket to the server if the socket wasn't disconnected/closed by the server? -


i have tcp asynchronous project , there's problem when comes reconnect client server if connection closed.(wasn't closed server).

when server closed client gets following error :

an existing connection forcibly closed remote host @ endreceive(iayncresult ar) @ receivecallback(iasyncresult ar) 

ps:the client can receive streams server.

things being said , understand need somehow reconnect server . have tried didn't worked because client not responding , exiting.

what did , in catch block of receiving callback contains steps below , set clientsocket disconnected(true) , in while loop (!clientsocket.connected) , recalling main method starts asynchronous operation again , last line of code thread.sleep(n);

these things done in receive callback

  1. getting string buffer
  2. doing whatever.. string
  3. calling again beginreceive

ps: don't want use port.

you must catch exception (you can specific error code) , close socket , rid of it. create new 1 scratch.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -