C# Socket(server) wont receive any datas -
i trying make advanced chat in c#. not new programming close first tcp chat.
the problem socket
(server) looks not receive datas. in void receivedatalistener
backgroundworker
, added console.writeline();
check locks , display first console.writeline("receive data listener 0")
. know normal socket.receive()
lock until datas received seems stay locked if send datas.
i want add event onclientconnect
, onclientdisconnect
invoked fine know clients connects fine.
here code of server class :
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.net; using system.net.sockets; using system.componentmodel; using system.threading; namespace jachat.library { class socketserver { private socket socket; private backgroundworker bwsocketconnectlistener; private backgroundworker bwcheckifconnected; private backgroundworker bwreceivedatalistener; private list<clientmanager> clientslist; #region constructor public socketserver(int port) { clientslist = new list<clientmanager>(); socket = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp); socket.bind(new ipendpoint(ipaddress.any, port)); socket.listen(100); bwsocketconnectlistener = new backgroundworker(); bwsocketconnectlistener.dowork += new doworkeventhandler(listensocketconnection); bwsocketconnectlistener.runworkerasync(); bwcheckifconnected = new backgroundworker(); bwcheckifconnected.dowork += checkifclientstillconnectedthread; bwcheckifconnected.runworkerasync(); bwreceivedatalistener = new backgroundworker(); bwreceivedatalistener.dowork += receivedatalistener; bwreceivedatalistener.runworkerasync(); } #endregion #region getter public list<clientmanager> connectedclients { { return clientslist; } } #endregion #region public methods /// <summary> /// parse , send command object targets /// </summary> public void sendcommand(command cmd) { backgroundworker test = new backgroundworker(); test.dowork += delegate { foreach(clientmanager cmanager in clientslist){ cmanager.sendcommand(cmd); } }; test.runworkerasync(); } /// <summary> /// disconnect , close socket /// </summary> public void disconnect() { socket.disconnect(false); socket.close(); socket = null; //stop background worker } #endregion #region private methods private void listensocketconnection(object sender, doworkeventargs e) { while (socket != null) { //get , wait new connection clientmanager newclientmanager = new clientmanager(socket.accept()); clientslist.add(newclientmanager); onclientconnect.invoke(newclientmanager); } } private void checkifclientstillconnectedthread(object sender, doworkeventargs e){ while(socket != null){ for(int i=0;i<clientslist.count;i++){ if(clientslist[i].socket.poll(10,selectmode.selectread) && clientslist[i].socket.available==0){ clientslist[i].socket.close(); onclientdisconnect.invoke(clientslist[i]); clientslist.remove(clientslist[i]); i--; } } thread.sleep(5); } } private void receivedatalistener(object sender, doworkeventargs e){ while (socket != null){ console.writeline("receive data listener 0"); //read command's type. byte[] buffer = new byte[4]; int readbytes = this.socket.receive(buffer, 0, 4, socketflags.none); console.writeline("receive data listener 1"); if (readbytes == 0) break; console.writeline("receive data listener 2"); commandtype cmdtype = (commandtype)(bitconverter.toint32(buffer, 0)); console.writeline("receive data listener 3"); //read sender ip size. buffer = new byte[4]; readbytes = this.socket.receive(buffer, 0, 4, socketflags.none); if (readbytes == 0) break; int senderipsize = bitconverter.toint32(buffer, 0); //read sender ip. buffer = new byte[senderipsize]; readbytes = this.socket.receive(buffer, 0, senderipsize, socketflags.none); if (readbytes == 0) break; ipaddress cmdsenderip = ipaddress.parse(system.text.encoding.ascii.getstring(buffer)); //read sender name size. buffer = new byte[4]; readbytes = this.socket.receive(buffer, 0, 4, socketflags.none); if (readbytes == 0) break; int sendernamesize = bitconverter.toint32(buffer, 0); //read sender name. buffer = new byte[sendernamesize]; readbytes = this.socket.receive(buffer, 0, sendernamesize, socketflags.none); if (readbytes == 0) break; string cmdsendername = system.text.encoding.unicode.getstring(buffer); //read target ip size. string cmdtarget = ""; buffer = new byte[4]; readbytes = this.socket.receive(buffer, 0, 4, socketflags.none); if (readbytes == 0) break; int targetipsize = bitconverter.toint32(buffer, 0); //read command's target. buffer = new byte[targetipsize]; readbytes = this.socket.receive(buffer, 0, targetipsize, socketflags.none); if (readbytes == 0) break; cmdtarget = system.text.encoding.ascii.getstring(buffer); //read command's metadata size. string cmdmetadata = ""; buffer = new byte[4]; readbytes = this.socket.receive(buffer, 0, 4, socketflags.none); if (readbytes == 0) break; int metadatasize = bitconverter.toint32(buffer, 0); //read command's meta data. buffer = new byte[metadatasize]; readbytes = this.socket.receive(buffer, 0, metadatasize, socketflags.none); if (readbytes == 0) break; cmdmetadata = system.text.encoding.unicode.getstring(buffer); //create command object command cmd = new command(cmdtype, cmdsenderip, cmdsendername, ipaddress.parse(cmdtarget), cmdmetadata); this.oncommandreceived(cmd); } console.writeline("receive data listener closed"); } #endregion #region events public delegate void onclientconnecteventhandler(clientmanager client); /// <summary> /// events invoked when client connect server /// </summary> public event onclientconnecteventhandler onclientconnect = delegate { }; public delegate void onclientdisconnecteventhandler(clientmanager client); /// <summary> /// events invoked when client disconnect server /// </summary> public event onclientdisconnecteventhandler onclientdisconnect = delegate { }; public delegate void oncommandreceivedeventhandler(command cmd); /// <summary> /// events invoked when command has been sent server /// </summary> public event oncommandreceivedeventhandler oncommandreceived = delegate { }; #endregion } }
and here code use test server sending datas :
clientsocket = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp); clientsocket.connect("localhost", 2000); networkstream = new networkstream(clientsocket); //commandtype byte[] buffer = new byte[4]; buffer = bitconverter.getbytes(1); this.networkstream.write(buffer, 0, 4); this.networkstream.flush(); //sender ip + size byte[] senderipbuffer = encoding.ascii.getbytes("192.54.67.8"); buffer = new byte[4]; buffer = bitconverter.getbytes(senderipbuffer.length); this.networkstream.write(buffer, 0, 4); this.networkstream.flush(); this.networkstream.write(senderipbuffer, 0, senderipbuffer.length); this.networkstream.flush(); //sender name + size byte[] sendernamebuffer = encoding.ascii.getbytes("james"); buffer = new byte[4]; buffer = bitconverter.getbytes(sendernamebuffer.length); this.networkstream.write(buffer, 0, 4); this.networkstream.flush(); this.networkstream.write(sendernamebuffer, 0, sendernamebuffer.length); this.networkstream.flush(); //command target ip + size byte[] targetipbuffer = encoding.ascii.getbytes("192.43.54.6"); buffer = new byte[4]; buffer = bitconverter.getbytes(targetipbuffer.length); this.networkstream.write(buffer, 0, 4); this.networkstream.flush(); this.networkstream.write(targetipbuffer, 0, targetipbuffer.length); this.networkstream.flush(); //command metadata + size byte[] metabuffer = encoding.unicode.getbytes("metadata contents"); buffer = new byte[4]; buffer = bitconverter.getbytes(metabuffer.length); this.networkstream.write(buffer, 0, 4); this.networkstream.flush(); this.networkstream.write(metabuffer, 0, metabuffer.length); this.networkstream.flush();
- socketserver has class-level variable named "socket"
- socketserver's constructor creates socket listening on specified port , saves value of socket
- listensocketconnection calling socket.accept (good) , keeping list of connections
- error: receivedatalistener trying read socket listening on specified port instead of reading individual sockets returned accept call in listensocketconnection
note: server throws exception right on startup (for me @ least) in receivedatalistener, before client has attempted connect, since socket reader worker tries read socket in listen state.
hope helps - harold
Comments
Post a Comment