我有一个服务器插座和3-4个android设备作为客户端。我正在使用TCP/IP进行通信。哪种是最好的方法。我应该为每个客户端使用多个端口吗?或者我应该使用相同的端口。如果使用相同的功能,那么我应该如何识别发往不同设备的通信?
发布于 2011-11-24 16:43:48
您可以使用一个端口。客户端可以将其id发送给您。如果不能,你可以查看客户端的IP地址,以确定是哪一个。
web上有成千上万的TCP客户端/服务器代码示例,但我将从JDK附带的示例代码开始,
发布于 2011-11-24 16:24:00
不需要,您不需要多个端口。
ServerSocket server = new ServerSocket(port);
while (true)
{
Socket socket = server.accept();
// do something with this socket - aka 1 client
new SomeClientClass(socket);
InputStream in = socket.getInputStream();
in.read(byte[]);
OutputStream out = socket.getOutputStream;
// out will only write response to its own client.
// when this new SomeClientClassis created, method returns to this point
// in while loop and waits for the next client
}https://stackoverflow.com/questions/8253859
复制相似问题