cool hit counter TCP communication based on java_Intefrankly

TCP communication based on java


TCP is a connection-oriented, reliable, byte-stream-based transport layer communication protocol. TCP communication is divided into client-side and server-side, and the corresponding objects are Socket and Server Socket, respectively.

ServerSocket class

Create a ServerSocket class, and also create a listening service on the specified port of the computer running the statement, e.g.

ServerSocket MyServerSocket = new ServerSocket(port);

Here the port on which the service is provided is formulated as 500, and a single computer can provide multiple services at the same time, and these different services are distinguished from each other by the port number, and different services are provided on different port numbers. To listen for possible Client requests, execute the following statement.

Socket LinkSocket = MyServerSocket.accept();

The called accept method puts the server-side program in a waiting state, and the program will remain congested until it captures a request from the client and returns a Socket object for that Client communication LinkSocket. Thereafter the Server program can read and write data to the remote terminal Client as long as it reads and writes data to this Socket object. When ending listening, close the ServerSocket object.

MyServerSocket.close();

Create the server-side process.

Establishing a TCP server-side socket service, via a ServerSocket object.

A port must be provided on the server side, otherwise the client cannot connect.

Obtaining the client object to which the connection was made.

(a) Get the socket stream through the client object and read the data sent by the client.

Close the resource, close the client and close the server.

Socket Class

When the Client program needs to get information and other services from the Server side, the camp creates a Socket object.

Socket MySocket = new Socket(host,port);

The first parameter of the Socket class constructor is the host address of the Server computer, and the second parameter is the port number on which the service is provided on the Server. Once the Socket object is successfully established, a connection can be created between the Client and Server and data can be passed between the two endpoints through this connection. Use the socket class getOutStream() and getInputStream() to get the input/output streams for reading and writing data to the socket respectively, and finally return the data read from the Server side back to the Server side. When the communication between the Server and Client ends, you can call the close() method of the Socket class to close the socket and dismantle the connection. ServerSocket is generally only used to set the port number and listen, the real communication is between the socket on the server side and the socket on the client side, and after the ServerSocket does an accept, the initiative is transferred.

Client creation process.

Establishing a Socket service for TCP clients, using a Socket object with an explicit address to connect to the host.

Once established successfully, that transport channel is the Socket stream. Get the input and output streams via getOutputStream() and getInputStream().

Writing out the data using the output stream.

Shutting down the Socket service.

The TCP server and client establishment process is shown in the following diagram.

Here we are using an example to implement the service on TCP, the case is written in java on the eclipse platform, for Java basics not in the expansion, interested readers can learn on their own. The case implements the following functions.

(a) The client sends a message to the server and outputs it in the server's Console window.

The server replies with a message to the client and outputs it in the Console window of the client.

Server-side source code

Client-side source code

Output results

complete

2018/7/25

Acknowledgements.

Thank you to the Governor of the two regions for the technical support!


Recommended>>
1、Coin Source Community Newsletter is hot 100 million candy waiting for you
2、What is an Ether DAO decentralized autonomous organization
3、Bitcoin mines devastated as successive rainstorms hit many parts of Sichuan tens of thousands of graphics cards abandoned on the streets
4、Heres a chance to get rich overnight
5、Too many unfamiliar words in a web page You need this crossword translation plugin

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号