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();
  }
}
上一篇:蓝牙通信测试app之蓝牙通信(二)


下一篇:java 文件处理 工具类