欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

文章标题

程序员文章站 2022-04-16 23:54:26
...
package network;
import java.io.DataInputStream;
import java.net.*;
public class Tcpserver {

    public static void main(String[] args) throws Exception{
        // TODO Auto-generated method stub
        ServerSocket ss=new ServerSocket(8002);
        while(true)
        {
            Socket s=ss.accept();
            System.out.println("A client connect");
            DataInputStream dis = new DataInputStream(s.getInputStream());
            System.out.println(dis.readUTF());
            dis.close();
            s.close();
        }
    }

}


package network;
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.*;
import java.rmi.server.SocketSecurityException;
public class TCPclient {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        Socket s =new Socket("127.0.0.1",8002);
        OutputStream os =s.getOutputStream();
        DataOutputStream dos=new DataOutputStream(os);
        dos.writeUTF("hello world");
        dos.flush();
        dos.close();
        s.close();
    }

}

文章标题