Home > Java > javaTutorial > body text

Detailed explanation of the methods and functions of Socket in Java

黄舟
Release: 2017-09-06 09:52:12
Original
2051 people have browsed it

1.java.net.Socket; The socket encapsulates the TCP communication protocol. It can be used to connect and communicate with the server application on the remote computer based on TCP.
Instantiating Socket is the process of establishing a connection with the server. Here you need to pass in two parameters to specify the server address information:
Parameter 1: Server computer address
Parameter 2: Service port opened by the server application running on the server computer
Via IP Find the server computer and use the port to connect to the server application running on the server extreme. Since instantiation is the connection process, if the server responds, instantiating the Socket here will throw an exception.
Socket provides methods:

OutputStream getOutputStream()
Copy after login

Bytes written out of the input stream obtained through Socket will be sent to the remote computer through the network. This is equivalent to sending it to the server.
2. java.net.ServerSocket running on the server;
has two main functions:
1. Apply to the system for an external service port. The client Socket establishes a connection with the server program through this port. .
2. Listen to the service port. Once a client Socket attempts to establish a connection through this port, serverSocket will sense and instantiate a Socket to communicate with the client.
ServerSocket provides methods:

Socket accept()
Copy after login

This method is a blocking method, used to listen to the service port until a client connects. A Socket will be returned here, through which you can communicate with the client.
Obtain the input stream through Socket, and the data read is the data sent from the remote computer. This is equivalent to the data sent by the client when reading;

InputStream in = socket.getInputStream();
Copy after login

When using the buffer stream to read a line of string sent from the client, the br.readLine method will block until If the client disconnects, the response here will be different depending on the client's system. When the Windows client is disconnected, the method will throw an exception. When the Linux client is disconnected, the method will return null

The above is the detailed content of Detailed explanation of the methods and functions of Socket in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template