Java Programming Network Programming TCP Programming

Java Programming Network Programming TCP Programming

Complete the development of TCP programs using Socket s

Use this class to easily establish a reliable, two-way, continuous, point-to-point communication connection

In Socket development, the server side uses ServerSocket to wait for the client to connect. For Java network programs, each client uses a Socket object to represent

ServerSocket Class and Socket Class

The ServerSocket class is primarily used for server-side program development to receive connection requests from clients

ServerSocket class

public ServerSocket(int port) throws IOExceptionCreate a ServerSocket instance and specify a listening port
public Socket accept() throws IOExceptionWaiting for client connection, blocking until connection
public InetAddress getInetAddress()Return the IP address of the server
public boolean isClosed()Returns the closed state of the ServerSocket
public void close() throws IOExceptionClose ServerSocket

Use the accept() method to wait for client connections each time the server runs

The server side will be blocked after this method is executed

The program cannot continue down until the client is connected

The return value type of this method is Socket

Each Socket represents a client object

Socket class

public Socket(String host,int port) throws UnknownHostException,IOExceptionConstruct the Socket object, specifying the host name and port to connect to the server
public InputStream getInputStream() throws IOExceptionReturns the input stream for this socket
public OutputStream getOutputStream() throws IOExceptionReturns the output stream of this socket
public void close() throws IOExceptionClose this Socket
public boolean isClosed()Determine if this socket is closed

On the client side, the program can get the output information of the server through the getInputStream() method of the Socket class, and on the server side, it can get the output information of the client through the getOutputStream() method.

Write TCP program

Write server program


public class Hello {
    public static void main(String[] args) throws IOException {
        ServerSocket server = null;
        Socket client = null;
        PrintStream out = null;
        server = new ServerSocket(8889);
        System.out.println("Server running, waiting for client connection");
        client = server.accept();
        String str = "HELLO";
        out = new PrintStream(client.getOutputStream());

[External chain picture transfer failed, source station may have anti-theft chain mechanism, it is recommended to save the picture and upload it directly (img-M4azSWf1-1645464825525) (C:/Users/30452/AppData/Roaming/Typora/typora-user-images/image-20220220220213550615.png)]

Write client program


public class Hello2 {
    public static void main(String[] args) throws Exception{
        Socket client = null;
        client = new Socket("localhost",8889);
        BufferedReader buf = null;
        buf = new BufferedReader(new InputStreamReader(client.getInputStream()));
        String str = buf.readLine();
        System.out.println("Server Output:"+str);

Keywords: Java network TCP/IP

Added by markjia on Mon, 21 Feb 2022 19:42:41 +0200