Use sockets for simple server client interaction

Source: Internet
Author: User

Socket Explanation:

The two programs on the network realize the exchange of data through a two-way communication connection, one end of this connection is called a socket.

The English literal of the socket is "hole" or "socket". As the BSD UNIX process communication mechanism, take the latter one meaning. Often also referred to as a "socket," which describes IP addresses and ports, is a handle to a communication chain that can be used to communicate between different virtual machines or different computers. Hosts on the internet typically run multiple service software, while providing several services. Each service opens a socket and binds to a port, and the different ports correspond to different services. The socket is like a porous socket, as its English intended. A host is like a room full of various sockets, each outlet has a number, some sockets provide 220 vac, some provide 110 volts AC, some provide cable TV programs. Customer software plug into different numbered sockets, you can get different services. The socket interaction principle illustrates the connection process: depending on how the connection is started and the destination to which the local socket is connected, the connection between sockets can be divided into three steps: Server listener, client request, connection acknowledgement. (1) Server monitoring: Is the server end socket does not locate the specific client socket, but in the status of waiting for the connection, real-time monitoring network status. (2) Client request: Refers to the client's socket to make a connection request, to connect to the target is the server-side socket. To do this, the client's socket must first describe the socket of the server it is connecting to, indicate the address and port number of the server-side socket, and then make a connection request to the server-side socket. (3) Connection confirmation: When the server-side socket is heard or received a client socket connection request, it responds to the client socket request, set up a new thread, the server-side socket description to the client, once the client confirms the description, the connection is established. While the server-side socket continues to be in the listening state, it continues to receive connection requests from other client sockets. The socket connection process diagram below is a simple server client interaction Demo:server with the socket:
1         Try {2             //create a server socket bound to a specific port3ServerSocket serversocket=NewServerSocket (9999);4SYSTEM.OUT.PRINTLN ("* * * server is about to start, waiting for client links * * * *");5             //listen for and accept connections to this socket6Socket socket=serversocket.accept ();7             //Get input stream8InputStream is=Socket.getinputstream ();9             //stream byte input to character inputTenInputStreamReader isr=NewInputStreamReader (IS); OneBufferedReader br=NewBufferedReader (ISR); AString str=NULL; -              while((Str=br.readline ())! =NULL){ -SYSTEM.OUT.PRINTLN ("server says: Client Sent:" +str); the br.readline (); -             } -             //The input stream for this socket is placed at the end of the stream - socket.shutdowninput (); + br.close (); - isr.close (); + is.close (); A}Catch(IOException e) { at e.printstacktrace (); -}

Client side:

1        Try {2Socket socket=NewSocket ("127.0.0.1", 9999);3OutputStream os=Socket.getoutputstream ();4PrintWriter print=Newprintwriter (OS);5Print.write ("Hello! Server! ");6 print.close ();7 os.close ();8}Catch(Exception e) {9 e.printstacktrace ();Ten}

You first need to start the server side and create ServerSocket to wait for the client connection:

Then, start the client:

In this way, a simple socket communication is implemented!

Use sockets for simple server client interaction

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.