c# - Multiclient udp server handling error code 10054 properly -


i've got udp server can handle multiple clients, main thing udp is connection less quite suprised when got following error:

an existing connection forcibly closed remote host.

i learned because trying send ipendpoint closed. later learned because network layer send icmp message saying port closed , icmp message why error thrown. started solutions problem but, although i've found many questions on stack overflow, not find 1 correct answer. (some have 0 answers).

when got error wouldn't receive anymore because beginreceivefrom method in try part after exception thrown. placed in catch part result in same error being thrown again.

so problem, once error: "an existing connection forcibly closed remote host." thrown cannot use socket anymore (or seems me)

and question is: how handle exception server can keep running?

this code:

public void listen() {     if (mdisposing == true)     {         throw new objectdisposedexception(null, "this instance disposed");     }      if (mlistening == false)     {         try         {             mlistening = true;             serverendpoint = new ipendpoint(serveraddress, port);             mserversocket = new socket(serveraddress.addressfamily, sockettype.dgram, protocoltype.udp);             mserversocket.bind(serverendpoint);              if (serveraddress.addressfamily == addressfamily.internetworkv6)             {                 operatingsystem os = environment.osversion;                 version version = os.version;                  // note: windows vista or higher have 1 ip stack ipv4 , ipv6                 // therefore can combined , used 1 socket ipv6                 // socket must accept both ipv4 , ipv6 connections.                 if (version.major > 5)                 {                     // note: ipv6_v6only socket option equivalent 27 in winsock snippet below                     // available in framework 4.0. lower version can implement (socketoptionname)27                     mserversocket.setsocketoption(socketoptionlevel.ipv6, socketoptionname.ipv6only, 0);                 }             }              var ipesender = new ipendpoint(ipaddress.any, 0);             var endpointsender = (endpoint)ipesender;             mserversocket.beginreceivefrom(mbytedata, 0, mbytedata.length, socketflags.none, ref endpointsender, new asynccallback(ondatareceived), null);         }         catch (exception exception)         {             mlistening = false;             doerror(exception);         }     }     else     {         var ipesender = new ipendpoint(ipaddress.any, 0);         var endpointsender = (endpoint)ipesender;         mserversocket.beginreceivefrom(mbytedata, 0, mbytedata.length, socketflags.none, ref endpointsender, new asynccallback(ondatareceived), null);     } }  public void close() {     if (mdisposing == true)     {         throw new objectdisposedexception(null, "this instance disposed");     }      if (mlistening == true)     {         mlistening = false;          try         {             foreach (clientinformation client in mclients)             {                 disconnect(client.id);             }              if (mserversocket != null)             {                 mserversocket.close();             }         }         catch (exception exception)         {             doerror(exception);         }     } }  private void waitfordata() {     if (mlistening == true)     {         try         {             var ipesender = new ipendpoint(ipaddress.any, 0);             var endpointsender = (endpoint)ipesender;             mserversocket.beginreceivefrom(mbytedata, 0, mbytedata.length, socketflags.none, ref endpointsender, new asynccallback(ondatareceived), null);         }         catch (exception exception)         {             doerror(exception);         }     } }  private void ondatareceived(iasyncresult asyncresult) {     if (mlistening == true)     {         try         {             ipendpoint ipesender = new ipendpoint(ipaddress.any, 0);             endpoint remoteendpoint = ipesender;              int irx = mserversocket.endreceivefrom(asyncresult, ref remoteendpoint);             var clientinfo = new clientinformation(remoteendpoint);              mclients.add(clientinfo);              var chars = new byte[irx];             buffer.blockcopy(mbytedata, 0, chars, 0, irx);             waitfordata();             doreceived(clientinfo, chars);         }         catch (exception exception)         {             waitfordata();             doerror(exception);         }     } }  public void send(string remoteendpoint, byte[] data) {     if (mlistening == true)     {         var clientinfo = activeconnections.find(remoteendpoint);         if (clientinfo != null)         {             try             {                 lock (locksend)                 {                     clientinfo.dataout = data;                     mserversocket.beginsendto(                         clientinfo.dataout,                         0,                         clientinfo.dataout.length,                         socketflags.none,                         clientinfo.remoteendpoint,                         new asynccallback(ondatasent),                         clientinfo);                 }             }             catch (exception exception)             {                 doerror(exception);             }         }         else         {             mlogger.errorformat("trying send client {0} not exist", remoteendpoint);         }     } }  private void ondatasent(iasyncresult asyncresult) {     if (mlistening == true)     {         var clientinfo = (clientinformation)asyncresult.asyncstate;         try         {             lock (locksend)             {                 int irx = mserversocket.endsendto(asyncresult);                 if (irx == clientinfo.dataout.length)                 {                     byte[] chars = new byte[irx];                     buffer.blockcopy(clientinfo.dataout, 0, chars, 0, irx);                     dosent(clientinfo, chars);                 }             }         }         catch (exception exception)         {             doerror(exception);         }     } } 

i happy provide information when needed , hoping problem can resolved.

the error description provided microsoft:

wsaeconnreset 10054 connection reset peer. existing connection forcibly closed remote host. results if peer application on remote host stopped, host rebooted, host or remote network interface disabled, or remote host uses hard close (see setsockopt more information on so_linger option on remote socket). error may result if connection broken due keep-alive activity detecting failure while 1 or more operations in progress. operations in progress fail wsaenetreset. subsequent operations fail wsaeconnreset.

i've been able find fix namely following code:

var sioudpconnectionreset = -1744830452; var invalue = new byte[] { 0 }; var outvalue = new byte[] { 0 }; mserversocket.iocontrol(sioudpconnectionreset, invalue, outvalue); 

by knowledge suppresses error, had implement in listen method looks this:

public void listen() {     if (mdisposing == true)     {         throw new objectdisposedexception(null, "this instance disposed");     }      if (mlistening == false)     {         try         {             mlistening = true;             serverendpoint = new ipendpoint(serveraddress, port);             mserversocket = new socket(serveraddress.addressfamily, sockettype.dgram, protocoltype.udp);              var sioudpconnectionreset = -1744830452;             var invalue = new byte[] { 0 };             var outvalue = new byte[] { 0 };             mserversocket.iocontrol(sioudpconnectionreset, invalue, outvalue);              mserversocket.bind(serverendpoint);              if (serveraddress.addressfamily == addressfamily.internetworkv6)             {                 operatingsystem os = environment.osversion;                 version version = os.version;                  // note: windows vista or higher have 1 ip stack ipv4 , ipv6                 // therefore can combined , used 1 socket ipv6                 // socket must accept both ipv4 , ipv6 connections.                 if (version.major > 5)                 {                     // note: ipv6_v6only socket option equivalent 27 in winsock snippet below                     // available in framework 4.0. lower version can implement (socketoptionname)27                     mserversocket.setsocketoption(socketoptionlevel.ipv6, socketoptionname.ipv6only, 0);                 }             }              var ipesender = new ipendpoint(ipaddress.any, 0);             var endpointsender = (endpoint)ipesender;             mserversocket.beginreceivefrom(mbytedata, 0, mbytedata.length, socketflags.none, ref endpointsender, new asynccallback(ondatareceived), null);         }         catch (exception exception)         {             mlistening = false;             doerror(exception);         }     }     else     {         var ipesender = new ipendpoint(ipaddress.any, 0);         var endpointsender = (endpoint)ipesender;         mserversocket.beginreceivefrom(mbytedata, 0, mbytedata.length, socketflags.none, ref endpointsender, new asynccallback(ondatareceived), null);     } } 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -