c# - specify Ip of the Client in Client/Server manually (By hand) -



c# - specify Ip of the Client in Client/Server manually (By hand) -

i building client/server using tcp socket in c#

here code server :

namespace server { public partial class form1 : form { private list<socket> _clientsockets; private socket server = null; private byte[] _buffer; public form1() { initializecomponent(); } private void button1_click(object sender, eventargs e) { server = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp); server.bind(new ipendpoint(ipaddress.any, 13000)); server.listen(10); server.beginaccept(new asynccallback(onconnect), null); } private void onconnect(iasyncresult ar) { socket client = server.endaccept(ar); _clientsockets.add(client); messagebox.show("connected"); client.beginreceive(_buffer, 0, _buffer.length, socketflags.none, new asynccallback(onrecieve), client); } private void onrecieve(iasyncresult ar) { socket client = (socket) ar.asyncstate; int32 numberofbytes = client.endreceive(ar); byte[] rec = new byte[numberofbytes]; array.copy(_buffer, rec, numberofbytes); messagebox.show(encoding.ascii.getstring(rec)); client.beginreceive(_buffer, 0, _buffer.length, socketflags.none, new asynccallback(onrecieve), client); } private void form1_load(object sender, eventargs e) { _clientsockets = new list<socket>(); _buffer = new byte[4096]; } } }

and client code:

namespace client { public partial class form1 : form { private socket _client; public form1() { initializecomponent(); } private void form1_load(object sender, eventargs e) { _client = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp); } private void button1_click(object sender, eventargs e) { int32 port = 13000; ipaddress my_ip_address = ipaddress.parse(textbox1.text); ipendpoint endpoint = new ipendpoint(my_ip_address, port); _client.beginconnect(endpoint, new asynccallback(onconnect), null); } private void onconnect(iasyncresult ar) { _client.endconnect(ar); } private void button2_click(object sender, eventargs e) { byte [] tosend = encoding.ascii.getbytes(textbox2.text); _client.beginsend(tosend, 0, tosend.length ,socketflags.none, new asynccallback(onsend), _client); } private void onsend(iasyncresult ar) { socket client = (socket) ar.asyncstate; client.endsend(ar); } } }

but when click button on client, it's not connecting server .. it's connected server if changed statements on client side

ipendpoint endpoint = new ipendpoint(my_ip_address, port);

to

ipendpoint endpoint = new ipendpoint(ipaddress.loopback, port);

then it's connected , message box in server appear whit text "connected" , why ?? because want specify ip address of client hand not using ipaddress.loopback

c# sockets client-server

Comments

Popular posts from this blog

c - Compilation of a code: unkown type name string -

java - Bypassing "final local variable defined in an enclosing type" -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -