java network programming-TCP-multi-person chat (object-oriented encapsulation)

Keywords: Java socket

Client:
Multiple customers can send and receive information normally, because they can send and receive information at the same time, not wait after sending information.
Return information, so add multithreading

 public class Client {

public static void main(String[]args) throws UnknownHostException, IOException
{
    System.out.println("Client startup...");

    Socket client =new Socket("localhost",9999);

    new Thread(new Send(client)).start();

    new Thread(new Receive(client)).start();

}
}

The sender of the client is encapsulated:
send message
Read messages from the keyboard
run()
Release resources

 public class Send implements Runnable{

private BufferedReader br;
private DataOutputStream dos;
private Socket client;
private boolean flag=true;

public Send(Socket client)
{
    this.client=client;
    br=new BufferedReader(new InputStreamReader(System.in));
    try {
        dos=new DataOutputStream(client.getOutputStream());
    } catch (IOException e) {

        this.release();
    }
}
public void run()
{
    while(flag)
    {
        System.out.println("Please enter a message:");
        String msg=getstr();
        send(msg);

    }
}
private void release()
{
    this.flag=false;
    utils.close(dos,client);
}
//Get messages from the console
private String getstr()
{
    try {
        String msg=br.readLine();
        return msg;
    } catch (IOException e) {

        e.printStackTrace();
    }
    return null;
}
private void send(String msg)
{
    try {
        dos.writeUTF(msg);
        dos.flush();

    } catch (IOException e) {
    release();
    }
}
}

The receiving end of the client is encapsulated:
receive messages
run()
Release resources

public class Receive implements Runnable {
private DataInputStream dis;
private Socket client;
private boolean flag=true;

public Receive(Socket client)
{
    this.client=client;
    try {
        dis=new DataInputStream(client.getInputStream());
    } catch (IOException e) {
        release();
    }
}

private String receive()
{
    String msg;
    try {
        msg = dis.readUTF();
        return msg;
    } catch (IOException e) {
        release();
    }
    return null;
}
public void run()
{
    while(flag)
    {
        String msg=receive();
        System.out.println(msg);
    }

}
private void release()
{
    this.flag=false;
    utils.close(dis,client);
}
}

As a tool class for releasing resources

public class utils {

public static void close(Closeable... target )
{
    for(Closeable it:target)
    {
            try {
                if(null!=it)
                {
                it.close();
                }
            } catch (IOException e) {

                e.printStackTrace();
            }

    }       
}
}

Online chat room
The server:

 public class Chat {

public static void main(String[]args) throws IOException
{
    System.out.println("Server startup...");

    ServerSocket server=new ServerSocket(9999);

    while(true)
    {
        Socket client =server.accept();
        System.out.println("A client establishes a connection");
        new Thread(new channel(client)).start();

    }

}

static class channel implements Runnable{
    private DataInputStream dis;
    private DataOutputStream dos;
    private BufferedReader br;
    private Socket client;
    private boolean flag;

    public channel(Socket client)
    {
        this.client=client;
        try {
            dis=new DataInputStream(client.getInputStream());
            dos=new DataOutputStream(client.getOutputStream());
            flag=true;
        } catch (IOException e) {
            release();          }
    }
    //receive messages
    private String receive() 
    {
        try {
            String msg=dis.readUTF();
            return msg;
        } catch (IOException e) {
            release();
        }
        return null;

    }
    //Send to message
    private void send(String msg)
    {
        try {
            dos.writeUTF(msg);
            dos.flush();
        } catch (IOException e) {
            release();
        }

    }
    private void release()
    {
        this.flag=false;
        utils.close(dis,dos,client); //Write a tool class using Closeable... variable parameters    
    }
    public void run()
    {
        while(flag)
        {
            String msg=receive();
            send(msg);
        }
        release();

    }

}
}

Posted by Balmung-San on Thu, 03 Oct 2019 22:46:18 -0700