Using socket for Java network programming

Source: Internet
Author: User

The socket is one end of two-way communication between two programs running on the network, it can accept the request, also can send the request, can use it to write the data on the network more conveniently. In Java, there is a special socket class to handle the user's request and response. Using the method of the socket class, you can realize communication between two computers. Here is how to use socket for network programming in Java.

In Java, the socket can be understood as a special object on the client or server side, which has two key methods, one is the getInputStream method and the other is the Getoutputstream method. The getInputStream method can get an input stream, and the input stream obtained by the getInputStream method on the client's socket object is actually the data stream sent back from the server side. The Getoutputstream method gets an output stream, and the output stream returned by the Getoutputstream method on the client socket object is the data stream that will be sent to the server side (in fact, a buffer that temporarily stores the data that will be sent in the past).

Programs can further encapsulate these data streams as needed. The examples in this article encapsulate these data streams (the encapsulation can refer to the implementation part of Java streaming).

To better illustrate the problem, here is an example of an online conversation, the client started, the server will start a thread to communicate with the customer text.

To complete this work, three parts of the work need to be completed, which in turn illustrates:

First, establish the server class

In Java, there is a class that is designed to create a socket server, called ServerSocket, that can be used as a parameter for creating a server object using the port number that the server needs to use.

ServerSocket Server = new ServerSocket (9998)

This statement creates a server object that uses port number No. 9998. When a client program establishes a socket connection and the port number that is connected is 9998, the server object Server responds to the connection and the Server.accept () method creates a socket object. The server side can use this socket object to communicate with the customer.

Socket incoming = Server.accept ()

The input stream and the output stream are obtained and encapsulated

BufferedReader in = new BufferedReader(new
       InputStreamReader(incoming.getInputStream()));
PrintWriter out = new PrintWriter(incoming.getOutputStream(),true);

You can then use the In.readline () method to get input from the client, or you can use the Out.println () method to send data to the client. This allows the client to respond to different requests based on the needs of the program.

The two streams should be closed after all traffic is closed, in order to close the output stream first and then close the input stream, that is, using the

out.close();
in.close();

Second, the establishment of client code

Compared to the server side, the client is simpler, and the client simply creates a socket object with the IP of the server's machine and the port of the server as a parameter. After you get this object, you can use the method described in the "Building a Server" section to implement data input and output.

Socket socket = new Socket("168.160.12.42",9998);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(),true);

The above program code establishes a socket object that is connected to a server object with an IP address of 168.160.12.42 and a port of 9998. and the input stream and output stream are established, respectively corresponding to the output of the server and the client writing.

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.