展开全部
可以参考一下这段代码
//1. 构造ServerSocket实例,指定服务端口。
ServerSocket servSock = new ServerSocket(servPort);
while(true)
{
// 2.调用accept方法,建立和客户端的连接
Socket clntSock = servSock.accept();
SocketAddress clientAddress =
clntSock.getRemoteSocketAddress();
System.out.println("Handling client at " + clientAddress);
// 3. 获取连接的InputStream,OutputStream来进行数据读写
InputStream in = clntSock.getInputStream();
OutputStream out = clntSock.getOutputStream();
while((recvMsgSize = in.read(receiveBuf)) != -1)
{
out.write(receiveBuf, 0, recvMsgSize);
}
// 4.操作结束,关闭socket.
clntSock.close();
}
//1. 构造ServerSocket实例,指定服务端口。
ServerSocket servSock = new ServerSocket(servPort);
while(true)
{
// 2.调用accept方法,建立和客户端的连接
Socket clntSock = servSock.accept();
SocketAddress clientAddress =
clntSock.getRemoteSocketAddress();
System.out.println("Handling client at " + clientAddress);
// 3. 获取连接的InputStream,OutputStream来进行数据读写
InputStream in = clntSock.getInputStream();
OutputStream out = clntSock.getOutputStream();
while((recvMsgSize = in.read(receiveBuf)) != -1)
{
out.write(receiveBuf, 0, recvMsgSize);
}
// 4.操作结束,关闭socket.
clntSock.close();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询