Introduction to java socket network programming

Network application modes mainly include:

WWW (World Wide Web) is an information browsing system based on client / server mode, HTML language and HTTP protocol, which can provide various Internet services. Network information is placed in different locations of the host, and the WWW server uses hypertext links to link various information. Www client (browser) is responsible for establishing contact with the server, sending requests to the server, processing HTML hypermedia, providing graphical user interface (GUI), displaying information, etc.

In the client / server working mode, on the server side, prepare to accept the communication of multiple client computers. Therefore, in addition to identifying the computer on the Internet with the IP address, the port number is also introduced to identify the thread serving in the background on the server side. The combination of port number and IP address is called network socket.

In the implementation of C / S mode in Java language, sockets are divided into two types:

Server through port (bus I / O address) provides services for the client machine; the server machine provides several different services at the same time on several different ports. The client accesses a port of the server and requests the server machine to serve it through this port. It is specified that port numbers 0 ~ 1023 are dedicated to the system. For example, HTTP protocol is on port 80, telnet protocol is on port 23, and ports 1024 ~ 65535 are for application programs use.

When the client program and the server program need to communicate, the socket class can be used to establish a socket connection. Socket connection can be imagined as a telephone call: at first, the client program establishes the call and the server program listens; After the call is completed, either party can speak at any time.

There are two optional ways for both parties to realize communication: streaming socket and datagram socket:

Streaming socket establishes a communication channel between client program and server program. Each socket can perform read and write operations. For either end, the communication session process with the other side is to establish a socket connection, obtain the input / output stream, read / write data, and close the socket (remove the connection) after the communication is completed.

Using the socket construction method, the socket object from the client to the server can be established: socket (string host, int port): host is the IP address of the server and port is the port number. These are pre agreed. For example, code:

Then, get the input stream with getinputstream () method, and use this input stream to read the information put into the "line" by the server; Use the getoutputstream () method to obtain the output stream, which is used to write information to the "line".

Using the construction method of ServerSocket, a server socket object that accepts client sockets can be established on the server: ServerSocket (int port): specify the port number and create a ServerSocket object. The port number should be the same as the port number called by the customer. For this purpose, use the following form code:

The server program listens on the specified port. When receiving the service request sent by the client program, it creates a socket object to communicate with the client program corresponding to the port. For example, execute the above code to create a server socket object. After the object ServerSocket is established, it may use the accept () method to get the socket object and receive the information from the socket mysocket from the client program. As shown in the following code:

To revoke the service, close the socket object SC:

[example] client side application in C / S mode. This is a simple example of streaming socket communication on the client side. The code explains the writing method of the client side program. In the example, the client program makes a request to port 4441 of the server host, and reads and writes to the server after the connection is established.

[example] the server-side application corresponding to the client-side application. The program listens on port 4441. When a client request is detected, a string with "Hello, customer, I'm the server" is generated and output to the client.

In order to give full play to the parallel working ability of the computer, the socket connection can be completed by one thread. When the client wants to request the server for service, or when the server receives a customer's service request, it starts a thread dedicated to information communication, creates input and output streams in the thread, and completes the information exchange between the client and the server.

[example] a client applet that places the socket connection work in the thread. The interface has a send information button, a text box and a text area. The client application first establishes a socket connection with the server. Use the data input stream in to repeatedly read the information put into the line by the server, and display the received information in the text area. Snipe rescue [6] is just right After the message closes, the message is recorded б (7) open the door after the door is opened Ф The content in the text box is sent to the server after the text box is sent out.

[example] the program establishes a socket connection with the client with terminal 4441. After receiving the client's application, the server establishes a thread with the client's socket and starts. If there is no client's application, it continues to listen to the client's application. The thread establishes the input data stream in and output data stream out according to the client's socket. The thread uses in to read the information put into the line by the client. For example If the received message is "end", the server replies "end" and closes the socket connection; Otherwise, reply: "I'm the server, you say to me", and the information received by the server.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>