c# - Tcp connection Keep alive -


i creating client server application. server design , in place waiting connection client. in client section keep connection alive throughout th life of application , connection closes when main client application close's or shutdown or server closes it.

currently every 10 seconds server closes tcp connection.i tried

socket.setsocketoption(socketoptionlevel.tcp, socketoptionname.keepalive, true); 

but doesn't work me.. below code block

public tcpstreamdevice(string remoteipaddress, int remoteport, string sourceipaddress, int sourceportno)         {     mipaddress = remoteipaddress;     mport = remoteport;      mclient = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp);     system.net.ipendpoint localendpoint = new system.net.ipendpoint(system.net.ipaddress.parse(sourceipaddress), sourceportno);      mclient.bind(localendpoint);      mdatareceivedcallback = new asynccallback(datareceivedtcpcallback_handler);     mbuffer = new byte[1024];     description = new devicedescription(); } 

and in handler have:

private void datareceivedtcpcallback_handler(iasyncresult ar) {     try     {         socket client = (socket)ar.asyncstate;         int bytesreceived = client.endreceive(ar);          if (bytesreceived > 0)         {             //to know transport level errors             //engineinterface.reponsereceived(mbuffer, false);              receivecallbackfunc(mbuffer, bytesreceived);              client.beginreceive(mbuffer, 0, 1024, socketflags.none, datareceivedtcpcallback_handler, client);         }         else         {             //disconnect             /* when there no datapacket  means no tcp connection alive (how can keep tcp alive here) */         }     } } 

in call setsocketoption(), keepalive not valid @ socketoptionlevel.tcp level, instead use socketoptionlevel.socket.

setsocketoption( socketoptionlevel.socket, socketoptionname.keepalive, true );


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -