Socket programming simple server and simple client

Source: Internet
Author: User
1. simple Server/* using system. data; using system. net. sockets; using system. net; using system. threading; Private Static int Port = % 2; Private Static thread ththreadread; Private Static tcplistener tcplisten; Private Static bool blistener = true; Private Static socket stread; */Private Static void listen () {try {tcplisten = new tcplistener (port); tcplisten. start (); stread = tcplisten. acceptsocket (); endpoint tempremoteep = stread. remoteendpoint; ipendpoint tempremoteip = (ipendpoint) tempremoteep; iphostentry host = DNS. gethostbyaddress (tempremoteip. address); string shostname = host. hostname; while (blistener) {stread. send (encoding. ASCII. getbytes (% 1); string stime = datetime. now. toshorttimestring (); byte [] byread = new byte [1024]; int Iread = stread. receivefrom (byread, ref tempremoteep); byte [] bytext = new byte [Iread]; array. copy (byread, 0, bytext, 0, Iread); string line = system. text. encoding. default. getstring (byread) ;}} catch (system. security. securityexception) {// listener Failed} ththreadread = new thread (New threadstart (Listen); ththreadread. start (); 2. simple client/* using system. data; using system. net. sockets; using system. net; Private Static ipendpoint datetimehost; */string hostipstring = % 1; string hostportstring = % 2; IPaddress hostip = IPaddress. parse (hostipstring); datetimehost = new ipendpoint (hostip, int32.parse (hostportstring); socket conn = new socket (addressfamily. interNetwork, sockettype. stream, protocoltype. TCP); Conn. connect (datetimehost); int bytes = 0; byte [] recvbytes = new byte [256]; bytes = Conn. receive (recvbytes, recvbytes. length, 0); string recvstring = encoding. ASCII. getstring (recvbytes, 0, bytes); console. writeline (recvstring); Conn. shutdown (socketshutdown. both); Conn. close (); 3. obtain the local IP address // using system. net; IPaddress [] Addresslist = DNS. gethostbyname (DNS. gethostname ()). addresslist; string % 1 = NULL; For (INT I = 0; I <Addresslist. length; I ++) {% 1 + = Addresslist [I]. tostring ();}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.