Java网络编程——Socket

服务器端(单线程)

public class MySocketServer1 {

  private int port;
  private ServerSocket serverSocket;

  DataInputStream in;
  DataOutputStream out;

  public MySocketServer1(int port) throws IOException {
    this.port = port;
    serverSocket = new ServerSocket(port);
  }

  public void start() throws IOException {
    Socket socket = null;
    for (; ; ) {
      try {
        if (socket == null) socket = serverSocket.accept();

        in = new DataInputStream(socket.getInputStream());
        System.out.println("收到: " + socket.getInetAddress().getHostAddress() + "\n消息: " + in.readUTF() + "\n");

        out = new DataOutputStream(socket.getOutputStream());
        out.writeUTF("服务器已收到");

      } catch (Exception e) {
        e.printStackTrace();
        if (socket != null) {
          socket.close();
          socket = null;
        }
      }
    }
  }

  public static void main(String[] args) throws IOException {
    new MySocketServer1(4399).start();
  }
}

服务器端(多线程)

public class MySocketServer2 implements Runnable {

  private int port;
  private ServerSocket serverSocket;

  DataInputStream in;
  DataOutputStream out;

  ExecutorService executorService = Executors.newFixedThreadPool(2);

  public MySocketServer2(int port) throws IOException {
    this.port = port;
    serverSocket = new ServerSocket(port);
  }

  public void start() throws IOException {
    executorService.execute(this);
    executorService.execute(this);
  }

  @Override
  public void run() {
    Socket socket = null;
    for (; ; ) {
      try {
        if (socket == null) socket = serverSocket.accept();

        in = new DataInputStream(socket.getInputStream());
        System.out.println("收到: " + socket.getInetAddress().getHostAddress() + "\n消息: " + in.readUTF() + "\n");

        out = new DataOutputStream(socket.getOutputStream());
        out.writeUTF("服务器已收到");

      } catch (Exception e) {
        e.printStackTrace();
        if (socket != null) {
          try {
            socket.close();
          } catch (IOException ioException) {
            ioException.printStackTrace();
          }
          socket = null;
        }
      }
    }
  }

  public static void main(String[] args) throws IOException {
    new MySocketServer2(4399).start();
  }
}

客户端

public class MySocketClient1 {

  private int port;
  Socket client;

  DataInputStream in;
  DataOutputStream out;

  public MySocketClient1(int port) throws IOException {
    this.port = port;
    client = new Socket("127.0.0.1", port);
  }

  public void start() throws IOException, InterruptedException {
    int n = 0;
    for (int i = 0; i < 3; i++) {
      out = new DataOutputStream(client.getOutputStream());
      out.writeUTF("hello" + " " + n++);

      in = new DataInputStream(client.getInputStream());
      System.out.println("收到: " + client.getRemoteSocketAddress() + "\n消息: " + in.readUTF() + "\n");
      Thread.sleep(2000);
    }
    client.close();
  }

  public static void main(String[] args) throws IOException, InterruptedException {
    new MySocketClient1(4399).start();
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值