import java.net.*;
import java.io.*;
public class Server {
public static void main(String args[])throws Exception{
ServerSocket ss = new ServerSocket(6666);//端口被监听
while(true){
Socket s = ss.accept();//Server端口允许客户端的链接,返回一个客户端对象
DataInputStream dis = new DataInputStream(s.getInputStream());
System.out.println(dis.readUTF());
dis.close();
s.close();
System.out.println("Accept!");
}
}
}
import java.net.*;
import java.io.*;
public class Client {
public static void main(String[] args)throws Exception{
Socket c = new Socket("127.0.0.1",6666);
OutputStream os = c.getOutputStream();
DataOutputStream dd = new DataOutputStream(os);
Thread.sleep(3000);
dd.writeUTF("hello Server!");
dd.flush();dd.close();
c.close();
}
}
TCP传输协议中的Server和Cilent之间的交互
最新推荐文章于 2025-04-29 00:14:15 发布
本文提供了一个简单的Java网络编程示例,包括服务器端和客户端的实现。服务器端监听6666端口接收消息,客户端连接该端口并发送字符串“helloServer!”。通过这个例子可以了解基本的Java网络通信流程。
9884

被折叠的 条评论
为什么被折叠?



