首页 > 代码库 > java TCP通信

java TCP通信

服务器端:

public class TCPServer{

  public static void main(String[]args){

    ServerSocket ss  = new ServerSocket(6666);

    while(true){

      Socket s = ss.accept();

      System.out.println("a client connent!");

      DataInputStream dis = new DataInputStream(ss.getInputStream());

      System.out.println(dis.readUTF());

      dis.close();

      s.close();
    }

    

  }

}

 

客户端:

public class TCPClient{

  public static void main(String[]args){

    Socket s  = new Socket(“127.0.0.1”,6666);

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

    dos.writeUTF("Hello World!");

    dos.flush();

    dos.close();

    s.close();

  }

}

java TCP通信