C# TCP Server cannot response to client -
so job create server can respond , connect several client @ once, that's why create threaded server , test client check.
the client can connect server them pressing button, send message server, server put message interface respond client. lastly client receive response , put onto interface.
the problem got server can receive message server , display response server client not successful though similar. debugger seems client stuck @ read procedure.
below code server. think problem come @ connection handler , client side. stuck now, please show me did wrong @@
public class server { tcplistener serversocket = new tcplistener(ipaddress.any, 3000); tcpclient clientsocket = default(tcpclient); int counter = 0; private examplecallback callback; public server(examplecallback callback) { callback = callback; } public void initserver() { serversocket.start(); //call main thread update display if (callback != null) callback(">> server initialized!"); console.writeline(">> server initialized!"); } public void waitconnection() { int counter = 0; while (true) { counter += 1; clientsocket = serversocket.accepttcpclient(); if (callback != null) callback(">> client number: " + counter + " started!"); console.writeline(">> client number: " + counter + " started!"); handleclient handerler = new handleclient(new examplecallback(receivecallback), clientsocket, counter); thread clientthread = new thread(new threadstart(handerler.startconv)); clientthread.start(); } } public void closeserver() { clientsocket.close(); serversocket.stop(); } private void receivecallback(string message) { callback(message); } }
the code connection handerler:
public class handleclient { private examplecallback callback2; tcpclient clientsocket; int clientno; public handleclient(examplecallback callback, tcpclient client, int clno) { callback2 = callback; clientsocket = client; clientno = clno; } public void startconv() { int requestcount = 0; byte[] bytefrom = new byte[10025]; string xmlfromclient = null; string datafromclient = null; string serverresponse = null; string serverxmlresponse = null; networkstream serverstream = clientsocket.getstream(); requestcount = 0; while (true) { try { requestcount++; //receive requerst client serverstream.read(bytefrom, 0, (int)clientsocket.receivebuffersize); xmlfromclient = system.text.encoding.ascii.getstring(bytefrom); callback2("\n >> client :" + clientno + ". message: \n" + xmlfromclient); //console.writeline(">> client :" + clientno + ". message: \n" + xmlfromclient); //send acknowledgement client serverresponse = "server client(" + clientno + "). request time: " + requestcount; byte[] byteto = encoding.ascii.getbytes(serverresponse); serverstream.write(byteto, 0, byteto.length); serverstream.flush(); console.writeline(">> " + serverresponse); } catch (exception ex) { callback2(ex.tostring()); } } } }
and lastly code client size:
public partial class form1 : form { system.net.sockets.tcpclient clientsocket = new system.net.sockets.tcpclient(); networkstream serverstream; public form1() { initializecomponent(); } public void msg(string mesg) { this.display.text += environment.newline + " >> " + mesg; } private void sendbutton_click(object sender, eventargs e) { serverstream = clientsocket.getstream(); byte[] outstream = system.text.encoding.ascii.getbytes("world domination"); serverstream.write(outstream, 0, outstream.length); serverstream.flush(); thread receive = new thread(new threadstart(receivemess)); } private void receivemess() { serverstream = clientsocket.getstream(); byte[] instream = new byte[10025]; serverstream.read(instream, 0, (int)clientsocket.receivebuffersize); string returndata = system.text.encoding.ascii.getstring(instream); msg("data server : " + returndata); } private void connectbutton_click(object sender, eventargs e) { msg("client started"); clientsocket.connect("127.0.0.1", 3000); this.display.text = "client socket program - server connected ..."; } }
you seem forget start receive
thread:
private void sendbutton_click(object sender, eventargs e) { // ... skipped ......... thread receive = new thread(new threadstart(receivemess)); receive.start(); // <--- try adding line }
Comments
Post a Comment