文章标题
程序员文章站
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();
}
}
上一篇: WordPress文章中代码高亮实现方法
下一篇: 文章标题