Java Network Programming Note 4
Socket and ServerSocket. network programming can be divided into creating a Socket, opening the input stream and output stream connecting to the Socket, programming the Socket, and disabling the Socket.
Create Socket
The working process of a fully functional Socket includes the following four steps:
1. Create a Socket.
2. Open the input/output stream connected to the Socket.
3. Perform read/write operations on the Socket according to certain protocols.
4. Disable Socket
In the java.net package, the Socket and ServerSocket classes are defined. The Socket class represents the two ends of the client or server Socket connection.
The constructor in class Socket is as follows:
Socket (inetAddress address, int port)
Socket (inetAddress address, int port, boolean stream)
Socket (String host, int port)
Socket (String host, int port, boolean stream)
Socket (sockeibd impl)
Socket (String host, int port, inetAddress localAddr, int localport)
Socket (inetAddress address, int port, inetAddress localAddr, int localport)
The parameter address indicates the IP address, host indicates the host name, port indicates the port number, stream indicates whether the Socket is a stream or a datagram, localPort indicates the port number of the local host, and localAddr indicates the local host and its address, impl is the parent class of Socket.
The ServerSocket construction method is as follows:
ServerSocket (int port)
ServerSocket (int port, int backlog)
ServerSocket (int port, int backlog, InetAddress bindAddr)
The bindAddr parameter indicates the local address.
Both the Socket and ServerSocket class libraries are located in the java.net package. ServerSocket is used on the server side and used when a network connection is established. After the connection is successful, a Socket instance is generated at both ends. Through this Socket instance, you can complete the required session.
Common Socket methods include the Accept method, which is mainly used to generate "blocking" until a connection is received and a client's Socket object instance is returned. The getInputStream method is used to obtain network connection input and return an instance of an InputStream object. GetOutputStream method to obtain the OutputStream instance.
Refer to client-server communication:
Server:
public class Server {public static void main(String[] args) {ServerSocket ss;try {ss = new ServerSocket(30000);while(true){Socket socket=ss.accept();OutputStream outputStream=socket.getOutputStream();InputStream inputStream=socket.getInputStream();BufferedReader br=new BufferedReader(new InputStreamReader(inputStream));String request=br.readLine();System.out.println(request);socket.close();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
Client:
public class Client {public static void main(String[] args) {try {Socket socket=new Socket(192.168.120.112,30000);OutputStream outputStream=socket.getOutputStream();InputStream inputStream=socket.getInputStream();PrintStream printStream=new PrintStream(outputStream);printStream.print(Client--->);socket.close();} catch (UnknownHostException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}