Transfer Java network programming with Socket

xiaoxiao2021-03-06  41

Socket is one end of the two-way communication between two programs running on the network, which can be accepted, or send requests, using it to write data from network data. In Java, there is a dedicated Socket class to handle the user's request and response. With the Socket class, communication between two computers can be achieved. Here is how to use Socket to network programming in Java.

Socket in Java can be understood as a special object of the client or server side, this object has two key ways, one is the GetInputStream method, the other is the Getputstream method. The GetInputStream method can get a input stream, 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. GetputStream method gets an output stream, the output stream returned on the GetoutPutStream method on the client socket object is the data stream to be sent to the server side, (in actually a buffer, temporarily stored will send past data).

The program can be further encapsulated as needed for these data streams. The examples of this paper have a certain package for these data streams (regarding the implementation of the package can be referred to in Java).

In order to better illustrate the problem, here, an example of online dialogue, after the client starts, the server will start a thread to communicate with the customer.

To complete this job, you need to complete the work of three parts, the following explanation:

First, establish a server class

There is a class that is specifically used to establish a Socket server, named ServerSocket, you can use the port number you need to use as a parameter to create a server object.

Serversocket Server = New Serversocket (9998)

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

Socket incoming = server.accept ()

Further, the input stream and output flow are obtained, and packaged

BufferedReader IN = New BufferedReader (IncutStreamRead); PrintWriter Out = New PrintWriter (incoming.getOutputStream (), true)

Subsequently, you can use the in.readline () method to get the client's input, or you can send data to the client using the out.println () method. Thereby, different requests for the client can be responding according to the needs of the program.

After all communication, the two data streams should be closed, the order in which the closing is closed, turn off the output stream, then turn off the input stream,

Out.close (); in. close ();

Second, establish client code

Compared to the server side, the client is simple, the client only needs to create a socket object with the server's IP and the port of the server as a parameter. Once this object is obtained, the input and output of the data can be implemented with the method described in the "Building Server" section.

Socket socket = new Socket ( "168.160.12.42", 9998); in = new BufferedReader (new InputStreamReader (socket.getInputStream ())); out = new PrintWriter (socket.getOutputStream (), true); program code to establish the above A Socket object, which is connected to the host of the IP address of 168.160.12.42, with a port 9998 server object. And the input stream and output stream are established, and the output of the server and the writing of the client are respectively corresponding.

Third, establish a user interface

Readers can build their own user interface according to their own preferences, which is not the focus of this article.

After three steps, a relatively simple dialogue can be established. However, in order to make this program more perfect, the following improvements should be performed:

First, the server can only serve a customer, which is single thread. It can be improved to multi-threaded servers.

Try {file: // Established Server ServerSocket Server = New Serversocket (9998); INT i = 1; for (;;) {socket incoming = server.accept (); new serverthread (incoming, i) .start (); i ;}}}} Catch (ooException ex) {ex.printstacktrace ();}

The loop detection has a client to connect to the server. If there is, create a thread to serve this customer, the name of this thread is Serverthread, this class extends the Thread class, which is the same as the aforementioned server.

Second, in order to get the message delivered at any time, a separate thread can be established in the server and the client to view the input stream. If there is input in the input stream, it can be displayed immediately. code show as below:

New thread () {public void run () {use {while () {checkinput (); Sleep (1000); // Detect once every 1000 millisecond}} catch (ioexception ex) {} catch (ioException ex) {} }}. Start (); CHECKINPUT () method is private void checkinput () throw; if ((line = in.readline ())! = null) file: // Detecting the input stream New Data T.SETPARTNER (LINE); File: // Show messages in the data stream}

Through the above improvements, the program can run better.

Attachment: server implementation code

转载请注明原文地址:https://www.9cbs.com/read-60026.html

New Post(0)