Java socket programming example code explanation

  • 2020-04-01 02:31:21
  • OfStack

1, the so-called socket is often referred to as "socket", is used to describe the IP address and port, is a communication chain handle. Applications typically make requests to or respond to network requests through "sockets."

The three most common methods used to manipulate a Java socket are:

Accept () : mainly used for "blocking" on the server side, waiting for a link request from the client side, and returning a Socket instance on the client side;

GetInputStream () : the getInputStream() method is used to get network connection input and return an InputStream object instance.

GetOutputStream () : the method is the opposite of the getInputStream above.

2, generally to establish a Java Socket connection, should first identify the server side and the client side, the server side USES the ServerSocket to listen to the specified port, use accept waiting for the client request, link, start the session, complete the session, close the link. Note that normally socket closure should be done on the server side, as described below. The client USES Socket to issue a link request to a port of a server in the network, the connection succeeds, the session starts, the session completes, the Socket closes.

2. Sample code:

Server-side:


package com.icer.server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    private ServerSocket ss;
    private Socket s;
    private BufferedReader br;
    private PrintWriter pw;
    public Server() {
        try {
                ss = new ServerSocket(10000);
                System.out.println("Server is starting...");
                s = ss.accept();
                br = new BufferedReader(new InputStreamReader(s.getInputStream()));
                pw = new PrintWriter(s.getOutputStream(),true);
                String line = br.readLine();
                System.out.println(line);
                pw.println("your word is:" + line);
                //pw.println("helloworld");
                br.close();
                pw.close();
        } catch (IOException ie) {
            ie.printStackTrace();
        }
    }
    public static void main(String[] args) throws Exception {
        new Server();
    }
}

Client:


package com.icer.client;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Client {

    private Socket s;
    private BufferedReader br;
    //private BufferedReader line;
    private PrintWriter pw;
    private String line = "";
    public Client() {
        try{
            s = new Socket("127.0.0.1",10000);
            pw = new PrintWriter(s.getOutputStream(),true);
            br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            pw.println("hello");
            line = br.readLine();
            System.out.println(line);

            br.close();
            pw.close();
        }catch(IOException ie){
            ie.printStackTrace();
        }
    }
    public static void main(String[] args) throws Exception {
        new Client();
    }
}


Related articles: