Socket programming server and client code

Source: Internet
Author: User

// Server socket <SPAN class = 'wp _ keywordlink '> Program </span> creates a main program to receive messages from the client. <br/> Public void serversocket () <br/> {<br/> socket listener = new socket (addressfamily. interNetwork, sockettype. stream, protocoltype. TCP); // (1) first define a socket on the server and use the TCP protocol <br/> listener. BIND (New ipendpoint (IPaddress. any, 2112); // (2) set the binding IP address of the socket and accept the message sent from any IP address to port 2112. <br/> listener. listen (100); // (3) set the socket to the listening status and set the listening queue to 100 <br /> While (true) // infinite loop, that is, messages are always accepted without exception interruption <br/>{< br/> string receiverallstr = string. empty; <br/> Socket socket = listener. accept (); // (4) create a new socket for a newly created connection <br/> while (true) <br/> {<br/> byte [] receivebytes = new byte [1024]; <br/> int numbytes = socket. receive (receivebytes); // (5) receives messages from the socket and writes the received messages to the defined byte array. <br/> string receivestr = encoding. ASCII. getstring (R Eceivebytes, 0, numbytes); <br/> receiverallstr + = receivestr; <br/> If (receivestr. indexof ("[Final]")>-1) <br/>{< br/> console. writeline (receiverallstr); <br/> break; <br/>}< br/> string replysuccess = "the data can be successfullly received! "; // Not required <br/> byte [] bytestr = encoding. ASCII. getbytes (replysuccess); // optional <br/> socket. send (bytestr); // not required. Send a successful response to send data to connected system to the client. net. sockets. socket <br/> socket. shutdown (socketshutdown. both); // (6) disables sends and ES on a system. net. sockets. socket <br/> socket. close (); // (7) Close the socket connection, release all occupied resources, and continue to accept other messages in an endless loop <br/>}< br/> listener. close (); // close system. net. Sockets. socket Connection and release all associated resources. <br/> console. read (); <br/>}< br/> // create a main program to run the sending program. <br/> Public void clientsocket () <br/> {<br/> socket sender = new socket (addressfamily. interNetwork, sockettype. stream, protocoltype. TCP); // (1) first define a socket on the server and use the TCP protocol <br/> iphostentry iphost = DNS. resolve ("127.0.0.1"); <br/> IPaddress = iphost. addressl Ist [0]; <br/> ipendpoint = new ipendpoint (IPaddress, 2112); <br/> sender. connect (ipendpoint); // (2) set the link IP address of the socket and send it to port 2112 of the specified IP address <br/> string sendstr = "hello, the socket test! "; <Br/> byte [] sendbytes = encoding. ASCII. getbytes (sendstr + "[Final]"); <br/> sender. send (sendbytes); // (3) send data bit streams <br/> byte [] receivebytes = new byte [1024]; <br/> sender. receive (receivebytes); // (4) obtain the Server Response Data <br/> console. writeline ("received from server:" + encoding. ASCII. getstring (receivebytes); <br/> sender. shutdown (socketshutdown. both); // (5) disables sends and ES on a system. net. sockets. socket <br/> sender. close (); // (6) Close the socket connection and release all occupied resources <br/>}

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.