Only one usage of each socket address (protocol/network address/port) is normally permitted - C#
hi encounter above error when tick , untick checkbox on form. code below
  public void udplisten()          {              try              {                  //e = new emailsetup();                  //e.showdialog();                  //re = e.getemail();                  //debug.write(re);                  // construct socket , bind trap manager port 162                   socket socket = new socket(addressfamily.internetwork, sockettype.dgram, protocoltype.udp);                  ipendpoint ipep = new ipendpoint(ipaddress.any, 162);                  endpoint ep = (endpoint)ipep;                  socket.bind(ep);                  // disable timeout processing. block until packet received                   socket.setsocketoption(socketoptionlevel.socket, socketoptionname.receivetimeout, 0);                           bool run = true;              while (run)              {                  byte[] indata = new byte[16 * 1024];                  // 16kb receive buffer                   int inlen = 0;                  ipendpoint peer = new ipendpoint(ipaddress.any, 0);                  endpoint inep = (endpoint)peer;                  try                  {                      inlen = socket.receivefrom(indata, ref inep);                  }                  catch (exception ex)                  {                      console.writeline("exception {0}", ex.message);                      inlen = -1;                  }  my checkbox code
 if (chktraps.checked)              {                  trap = new traps();                                    thread othread = new thread(new threadstart(trap.udplisten));                  othread.start();                                }   
you can't have 2 different sockets listening on same port number. listener should started once. you put code in class constructor async event can continue runnning other code whiee wait incoming data (non blocking) . code blocking. simple have a test doens't start listener if started.
a form project automatically blocking. there built in infinite loop program doesn't terminate. gets run off of event. code added a 2nd block form whichisn't required.
the website below shows async listener class. code meants console application, , in form project can eliminate manualresetevent. simple call function dobeginaccepttcpclient once in code start listener , let function return without blocking parent function. every time receive message ocurs code execute event doaccepttcpclientcallback.
jdweng
                                                                          Visual Studio Languages                                                             ,                                                                 .NET Framework                                                     >                                                                 Visual C#                                                                            
 
 
Comments
Post a Comment