Java网络编程——Socket

2021/9/18 12:05:06

本文主要是介绍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();
  }
}


这篇关于Java网络编程——Socket的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程