C# Async socket server receives only one message from client -
i'm quite new sockets programming. hope problem presented understandable.
the problem when use client's button1_click
send textbox
's content - server gets first message. have no idea what's going wrong.
what might problem?
here server:
public partial class form1 : form { socket server; byte[] bytedata = new byte[1024]; public form1() { initializecomponent(); } private void form1_load(object sender, eventargs e) { try { server = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp); ipendpoint endpoint = new ipendpoint(ipaddress.any, 20000); server.bind(endpoint); server.listen(1); server.beginaccept(new asynccallback(accept), null); textbox1.text = "server started..."; } catch(exception ex) { messagebox.show(ex.message); } } private void accept(iasyncresult ar) { try { socket client = server.endaccept(ar); server.beginaccept(new asynccallback(accept), null); client.beginreceive(bytedata, 0, bytedata.length, socketflags.none, new asynccallback(receive), client); } catch(exception ex) { messagebox.show(ex.message); } } private void receive(iasyncresult ar) { socket client = (socket)ar.asyncstate; client.endreceive(ar); textbox1.invoke(new action(delegate () { textbox1.appendtext(environment.newline + encoding.ascii.getstring(bytedata)); })); bytedata = null; } }
and here client:
public partial class form1 : form { socket client; public form1() { initializecomponent(); } private void textbox1_textchanged(object sender, eventargs e) { if (textbox1.text == null) button1.enabled = false; else button1.enabled = true; } private void form1_load(object sender, eventargs e) { try { client = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp); ipaddress ip = ipaddress.parse("127.0.0.1"); ipendpoint ipendpoint = new ipendpoint(ip, 20000); client.connect(ipendpoint); } catch(exception ex) { messagebox.show(ex.message); } } private void button1_click(object sender, eventargs e) { try { byte[] bytedata = encoding.ascii.getbytes(textbox1.text); client.beginsend(bytedata, 0, bytedata.length, socketflags.none, new asynccallback(send), null); textbox1.text = string.empty; } catch(exception ex) { messagebox.show(ex.message); } } private void send(iasyncresult ar) { try { client.endsend(ar); } catch (objectdisposedexception) { } catch(exception ex) { messagebox.show(ex.message); } } }
in async
, got re-init beginreceive
in server side whenever want listen message.
therefore, in receive
callback, should re-init beginreceive
after endreceive
. otherwise cannot next message:
private void receive(iasyncresult ar) { socket client = (socket)ar.asyncstate; client.endreceive(ar); client.beginreceive(bytedata, 0, bytedata.length, //add beginreceive again socketflags.none, new asynccallback(receive), client); textbox1.invoke(new action(delegate () { textbox1.appendtext(environment.newline + encoding.ascii.getstring(bytedata)); })); bytedata = null; }
for more of working example async
, check out: sending value server client sockets
Comments
Post a Comment