.net - What is the opposite class to TcpListener in C# and how to use it to send message to server -
i have programmed tcp server in application can handle incoming connections , decode inoming messages.
but wanted create client on windows phone send short string message: "i client" server. in server used class tcplistener
looking "opposite" class tcpsender
not find one.
the servers ip is: 192.168.0.13
, port 13000
.
to sum question: how create short client send single message server?
my server looks - post show i've done it:
using system; using system.net; using system.net.sockets; using system.text; using system.threading; namespace homesecurity { class tcpeventserver { private tcplistener tcplistener; private thread listenthread; public tcpeventserver() { this.tcplistener = new tcplistener(ipaddress.any, 13000); this.listenthread = new thread(new threadstart(listenforclients)); this.listenthread.start(); } private void listenforclients() { this.tcplistener.start(); while (true) { tcpclient client = this.tcplistener.accepttcpclient(); thread clientthread = new thread(new parameterizedthreadstart(handleclientcomm)); clientthread.start(client); } } private void handleclientcomm(object client) { tcpclient tcpclient = (tcpclient)client; networkstream clientstream = tcpclient.getstream(); byte[] message = new byte[4096]; int bytesread; while (true) { bytesread = 0; try { bytesread = clientstream.read(message, 0, 4096); } catch { break; } if (bytesread == 0) { break; } asciiencoding encoder = new asciiencoding(); string messagedecoded = encoder.getstring(message, 0, bytesread); messagedecoded = messagedecoded.replace("\r", string.empty).replace("\n", string.empty); string ip = ((ipendpoint)tcpclient.client.remoteendpoint).address + ""; console.writeline("message: " + messagedecoded + " ip: " + ip); videostream.passmessage(messagedecoded, ip); } tcpclient.close(); } } }
look @ tcpclient class here. has functions establishing connection listener, example code.
Comments
Post a Comment