Java is a language that can be used to programmatically, which provides two powerful network support mechanisms: URL access to network resources classes and classes with Socket communication to meet different requirements. First, the URL is used to access the application of Internet online resources; the other is an application for Client / Server mode and the application of certain special protocols, which is based on TCP / IP protocol. Transport layer interface socket is implemented. This article wants to briefly introduce the Java implementation method programming for Socket.
Customers are implemented based on a Socket interface based on most communication components used between servers. Socket is a network communication endpoint for two-way data transfer, with an address and a port number to identify. Each service program is performed at a port when providing services, and the client wants to use the service must also connect to the port. Socket is based on the transport layer, so it is a comparative original communication protocol mechanism. Through Socket's data representation is byte stream information, the communication between communication must complete a specific application must be formatted and interpreted by the two parties. We can see that it is more troublesome to use the Socket program, but it Has stronger flexibility and broader use areas.
Some friends will ask, what is the mode of the client / server work? Ok, let's take a picture to introduce their working mode.
So how does the Java application implement the above process? There are two class sockets and serversockets in the Java.net package, which are used to create Socket communication on the client and server.
Let's take a look at the process prepared by the customer segment program:
1. First call the constructor of the Socket class, create a Socket stream with the specified IP address or specified host name and the specified port number of the server, and create a Socket stream that contains the Socket stream that contains communication connection to the server request. Process implementation.
2. After establishing the client communication socket. You can use the Socket method getInputStream () and getOutputStream () to create an input / output stream. Thus, after using the Socket class, the network input and outputs are also converted to the process of using the stream object.
3. Read the word stream data using the corresponding method of the input / output stream, because the stream is connected to the socket, the socket is also an endpoint that establishes a connection with the server side, so the data will be or send to the server from the server. At this time, we can process the byte stream data by protocol between the client and the server, complete the communication tasks of both parties.
4. After the mission is completed, we use the CLOSE () method of the flow object to close the input and output stream for network communication, and close the socket in the close () method of the socket object.
Below, I want to further introduce the writing of client programs through a simple example.
Code one:
Import java.io. *;
Import java.net. *;
Public Class SocketCommunicationClient
{
Public static void main (string [] args)
{
Try {
Socket ClientSocket = New Socket ("Mice", 9000); // Create a stream socket and connect to the port 9000 on the host MICE
OutputStream output = clientsocket.getOutputStream (); // Write an output stream to this Socket
DataInputStream Input = New DataInputStream (ClientSocket.getInputStream ());
File: //
Create a new data input stream to read data from the specified input stream
INT C;
String response;
While ((c = system.in.read ())! = - 1) // Accept the input string on the screen and decompose into a character
{
Output.write (BYTE) C);
IF (c == '/ n') //
If the character is a carriage return, the output string buffer
{
Output.flush ();
Response = INPUT.READLINE ();
System.out.println ("Communication:" Response);
}
}
Output.close ();
INPUT.CLOSE ();
Clientsocket.close ();
} catch (exception e) {
System.err.Println ("Exception:" E);
}
}
}
This program is an example of a very simple data communication. The program first creates a socket and connects to the port 9000 on the host mice, then turn on the input and output stream, then the program is received from the standard input to receive characters and write flow, each Write a line (logo in the user), send the string in the buffer to the server-side program on the MICE for processing, waiting for the server to respond. Input.readline () method call will cause the program to stop until the response information is received, and the program will repeat this process until the user inputs the mediation. The last program is to close the Socket input and output streams, turn off the connection of the socket and server side.
Above we read how to write the client's Socket interface program using Java, let me briefly talk about the Java implementation method of the server-side Socket interface program, the process is as follows: 1, first call the serversocket class with a port number To the parameter, create a Serversocket object, that is, the server-side service program listens on the specified port. 2, the server-side program uses the Accept () method of the ServerSocket object, receives a connection request from the client program, at which point the server side will remain stagnant until the client is received, and the method will return. An instance of a newly built Socket class, representative, and a communication link established by the client in the serving endpoint. If you use Java multi-thread programming methods, you can implement concurrent servers, continue to listen to connection requests from other customers. 3. Create an input and output flow object using the newly built Socket object. 4. Complete the data transfer of the stream object and the client's data transmission, identify and process request data from the client, and return the result of the process to the client. 5, after the client works, the server-side program is turned off and the stream of the client communications and the socket of communication.