c# - Async TCP Server in .NET Compact framework -
i'm writing asynchronous tcp server in .net compact framework 3.5(console app), used socket class tcplistener not supported .netcf. used same server code given in msdn (http://msdn.microsoft.com/en-us/library/fx6588te(v=vs.90).aspx). after 2/3 days system hangs, system arm device 64 mb ram. may possible cause. didn't such problem in synchronous tcp server. here code.
public void startlistening() { try { iphostentry iphostinfo = dns.resolve(dns.gethostname()); ipaddress ipaddress = iphostinfo.addresslist[0]; ipendpoint localendpoint = new ipendpoint(ipaddress, 8001); // create tcp/ip socket. socket listener = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp); // bind socket local endpoint , listen incoming connections. listener.bind(localendpoint); listener.listen(4); while (true) { try { // start asynchronous socket listen connections. listener.beginaccept(new asynccallback(acceptcallback), listener); // wait until connection made before continuing. alldone.waitone(); } catch (exception pex) { _serverlog.writefile(pex); } } } catch (exception pex) { _serverlog.writefile(pex); } } /// <summary> /// /// </summary> /// <param name="ar"></param> public void acceptcallback(iasyncresult ar) { alldone.set(); socket listener = (socket)ar.asyncstate; socket handler = listener.endaccept(ar); //listener.beginaccept(new asynccallback(acceptcallback), listener); stateobject state = new stateobject(); state.worksocket = handler; handler.beginreceive(state.buffer, 0, stateobject.buffersize, 0, new asynccallback(readcallback), state); //listener.beginaccept(new asynccallback(acceptcallback), listener); } /// <summary> /// /// </summary> public void readcallback(iasyncresult ar) { stateobject state = (stateobject)ar.asyncstate; socket handler = state.worksocket; string localip = handler.remoteendpoint.tostring(); var _port = localip.split(':'); string _ip = _port[0] + ":" + _port[1]; int bytesread = handler.endreceive(ar); if (bytesread > 0) { state.sb.append(encoding.ascii.getstring(state.buffer, 0, bytesread)); _receiveddata.enqueue(state.sb.tostring()); send(handler, " - ack - data received."); //threadpool.queueuserworkitem(new waitcallback(processdata), _ip); this.processdata(_ip); } } /// <summary> /// /// </summary> /// <param name="handler"></param> /// <param name="data"></param> private void send(socket handler, string data) { // convert string data byte data using ascii encoding. byte[] bytedata = encoding.ascii.getbytes(data); // begin sending data remote device. handler.beginsend(bytedata, 0, bytedata.length, 0, new asynccallback(sendcallback), handler); } /// <summary> /// /// </summary> /// <param name="ar"></param> private void sendcallback(iasyncresult ar) { try { // retrieve socket state object. socket handler = (socket)ar.asyncstate; // complete sending data remote device. int bytessent = handler.endsend(ar); handler.shutdown(socketshutdown.both); handler.close(); } catch (exception pex) { _serverlog.writefile(pex); } }
maybe in part haven't posted here, can't see anywhere code empty stringbuffer state.sb
or queue _receiveddata
(just assuming types name/method). filling 2 ressources can bring system hang @ point there no more memory...
Comments
Post a Comment