C# Client - Server Socket Disconnect Handling -
i've got client - server code going on @ moment while working on thesis. , can spawn connection , send data, client disconnects , tries reconnect goes sideways , can't seem figure out why. many exceptions being thrown , have no idea start catching them all. doing wrong or not doing isn't allowing nether client nor server handle disconnections ?!
this serverside code:
using system; using system.net; using system.net.sockets; namespace server.networking { public class serversocket { private socket _socket; byte[] _buffer = new byte[61144]; public serversocket() { _socket = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp); } public void bind(int port) { _socket.bind(new ipendpoint(ipaddress.any, port)); } public void listener(int backlog) { _socket.listen(backlog); } public void accept() { _socket.beginaccept(acceptedcallback, null); } private void acceptedcallback(iasyncresult result) { try { socket clientsocket = _socket.endaccept(result); if (clientsocket.connected) { console.writeline("client has connected!"); _buffer = new byte[61144]; clientsocket.beginreceive(_buffer, 0, _buffer.length, socketflags.none, receivedcallback, clientsocket); accept(); } else { console.writeline("client hasn't connected!"); return; } }catch(socketexception ex) { console.writeline(ex.message); close(clientsocket); } } private void receivedcallback(iasyncresult result) { try { socket clientsocket = result.asyncstate socket; socketerror se; int buffersize = clientsocket.endreceive(result, out se); if (buffersize > 0) { if (se == socketerror.success) { byte[] packet = new byte[buffersize]; array.copy(_buffer, packet, packet.length); console.writeline("handling packet ip:" + clientsocket.remoteendpoint.tostring()); //handle packet stuff here. packethandler.handle(packet, clientsocket); _buffer = new byte[61144]; clientsocket.beginreceive(_buffer, 0, _buffer.length, socketflags.none, receivedcallback, clientsocket); } else { close(clientsocket); } } else { console.writeline("probably received bad data."); close(clientsocket); } } catch (socketexception ex) { console.writeline(ex.message); close(clientsocket); } } public void close(socket sock) { console.writeline("closing socket ip:" + sock.remoteendpoint.tostring() + " , releasing resources."); sock.dispose(); sock.close(); } } }
and clientside code:
using system; using system.net; using system.linq; using system.net.sockets; using client.networking.packets; using system.net.networkinformation; using client.networking.packets.request; namespace client.networking { public class clientsocket { private socket _socket; private byte[] _buffer; public delegate void raiseconnect(object source, textargs e); public static event eventhandler disconnected; private static void raisedisconnect() { eventhandler handler = disconnected; if(handler !=null) { handler(null, eventargs.empty); } } public clientsocket() { udpbroadcast.connect += new raiseconnect(onconnectraise); } public string machineip() { return dns.gethostentry(dns.gethostname()).addresslist.firstordefault(ip => ip.addressfamily == addressfamily.internetwork).tostring(); } private void onconnectraise(object sender, textargs e) { checkregisteredrequest computer_name = new checkregisteredrequest(environment.machinename.tostring() + "," + machineip()); connect(e.message, 6556); send(computer_name.data); } public void connect(string ipaddress, int port) { string ip = ipaddress; int port = port; _socket = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp); iasyncresult result =_socket.beginconnect(new ipendpoint(ipaddress.parse(ip), port), connectcallback, null); bool success = result.asyncwaithandle.waitone(5000, true); if(!success) { _socket.close(); console.writeline("failed connect server. trying again."); connect(ip, port); } } private void connectcallback(iasyncresult result) { try { if (_socket.connected) { console.writeline("connected server!"); _buffer = new byte[61144]; _socket.beginreceive(_buffer, 0, _buffer.length, socketflags.none, receivedcallback, null); } else { console.writeline("could not connect."); close(_socket); } } catch(socketexception ex) { console.writeline("clientsocket connectcallback - "+ex.message); close(_socket); } } private void receivedcallback(iasyncresult result) { try { socketerror se; int buflength = _socket.endreceive(result, out se); if (buflength > 0) { if(se == socketerror.success) { byte[] packet = new byte[buflength]; array.copy(_buffer, packet, packet.length); //handle package packethandler.handle(packet, _socket); _buffer = new byte[61144]; _socket.beginreceive(_buffer, 0, _buffer.length, socketflags.none, receivedcallback, null); } else { close(_socket); } } else { close(_socket); } } catch (exception ex) { console.writeline("clientsocket receivedcallback - " + ex.message); close(_socket); } } public void send(byte[] data) { byte[] send = new byte[data.length]; send = data; if( _socket.connected) { _socket.send(data); } else { console.writeline("not connected yet!"); close(_socket); } } public bool connectionstatus() { return _socket.connected; } public static void close(socket sock) { console.writeline("closing socket , releasing resources."); sock.dispose(); sock.close(); raisedisconnect(); } } }
two things can think of. documentation on socket recommends calling shutdown() first, before close().
also, cannot reuse socket object. make sure you're using new socket object before trying new connection, either _socket = new socket(), or using totally new serversocket/clientsocket.
Comments
Post a Comment