美文网首页
2018-11-25

2018-11-25

作者: 壹切在與 | 来源:发表于2018-11-25 20:01 被阅读0次

服务端

public class Server {

  public static void main(String[] args) {

    try {// 建立服务器连接

      ArrayList<Socket> list = new ArrayList<Socket>();

      ServerSocket server = new ServerSocket(8000);

      while (true) {

        // 等待客户连接

        Socket socket = server.accept();

        list.add(socket);

        Accpetss accept = new Accpetss(socket, list);

        Sends sends = new Sends(list);

        Thread t1 = new Thread(accept);

        Thread t2 = new Thread(sends);

        t1.start();

        t2.start();

      }

      // socket.close();

      // server.close();

    } catch (IOException e) {

      e.printStackTrace();

    }

  }

}

class Accpetss implements Runnable {

  ArrayList<Socket> list = null;

  Socket socket = null;

  public Accpetss(Socket _socket, ArrayList<Socket> _list) {

    this.socket = _socket;

    this.list = _list;

  }

  @Override

  public void run() {

    try {

      while (true) {

        DataInputStream in = new DataInputStream(this.socket.getInputStream());

        String msg = in.readUTF();

        System.out.println(msg);

        for (Socket s : this.list) {

          if (s != this.socket) {

            DataOutputStream out = new DataOutputStream(s.getOutputStream());

            out.writeUTF(msg);

          }

        }

      }

    } catch (IOException e) {

      // TODO: handle exception

    }

  }

}

class Sends implements Runnable {

  ArrayList<Socket> list;

  public Sends(ArrayList<Socket> _list) {

    this.list = _list;

  }

  public void run() {

    try {

      while (true) {

        Scanner scaner = new Scanner(System.in);

        String s = scaner.next();

        for (Socket so : this.list) {

          DataOutputStream out = new DataOutputStream(so.getOutputStream());

          out.writeUTF(s);

        }

      }

    } catch (IOException e) {

      // TODO: handle exception

    }

  }

}

public class Client{

  public static void main(String[] args) throws IOException{

    try {

      // 连接到服务器

      Socket socket = new Socket("127.0.0.1", 8000);

      Accpet accept = new Accpet(socket);

      Send send = new Send(socket);

      Thread t1 = new Thread(accept,"server_accept");

      Thread t2 = new Thread(send,"server_send");

      t1.start();

      t2.start();

      // socket.close();

    } catch (IOException e) {

        e.printStackTrace();

    }

  }

}

客户端

class Accpet implements Runnable{

  Socket socket = null;

  public Accpet(Socket _socket) {

    socket = _socket;

  }

  @Override

  public void run() {

    try {

      DataInputStream in = new DataInputStream(socket.getInputStream());

      while (true) {

        String msg = in.readUTF();

        System.out.println(msg);

      }

    } catch (IOException e) {

      //TODO: handle exception

    }

  }

}

class Send implements Runnable{

  Socket socket = null;

  public Send(Socket _socket) {

    socket = _socket;

  }

  public void run() {

    try {

      DataOutputStream out = new DataOutputStream(socket.getOutputStream());

      Scanner scaner = new Scanner(System.in);

      while (true) {

        out.writeUTF(scaner.next());

      }

    } catch (IOException e) {

      //TODO: handle exception

    }

  }

}

相关文章

网友评论

      本文标题:2018-11-25

      本文链接:https://www.haomeiwen.com/subject/yukmqqtx.html