import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Main2 {
public static void main(String[] args) throws IOException {
//服务端接收客户端数据
//创建TCP客户端socket服务
ServerSocket socket = new ServerSocket(8888);
//获取客户端socket对象
Socket s = socket.accept();
//获取客户端socket输入流对象
InputStream in = s.getInputStream();
//读取数据
byte[] buf = new byte[1024];
int len = in.read(buf);
String text = new String(buf, 0, len);
String ip = s.getInetAddress().getHostAddress();
System.out.println("Server " + ip + ":" + text);
s.close();
//关闭资源 (相互连接的不需要再关out输入流)
socket.close();
s.close();
}
}
java 网络 TCP服务端接收数据 SeverSocket
最新推荐文章于 2023-07-30 16:55:12 发布