【发布时间】:2014-06-16 16:20:45
【问题描述】:
(这可能已经被问了一千次了,但我没有弄清楚。)
假设我有以下sn-p:
InetAddress localAddress = InetAddress.getByName("192.168.1.10");
int localPort = 65000;
InetAddress targetAddress = InetAddress.getByName("192.168.1.20");
int targetPort = 65000;
// Create a new serversocket
ServerSocket ss = new ServerSocket(localPort, 50, localAddress);
// Wait for an incoming connection...
Socket acceptedSocket = ss.accept();
// Do something with the accepted socket. Possibly in a new thread.
Set up new connection...
Socket socket = new Socket(targetAddress, targetPort, localAddress, localPort);
// Write something to the socket.
现在我可以使用相同的地址和端口来接受传入连接和连接到地址吗?如果可以,那怎么做?如果不是,那为什么不呢?根据this post,端口是可以共享的,应该没问题。
它是如何工作的?
【问题讨论】: