0

I'm currently building an application using TCP/IP connection and app establishes connection between server and client by querying an ip address from mysql database. But let's say there are

people A and B connected to a network NET_A (220.241.XXX.XXX)

AND

people C and D connected to a network NET_B (221.221.xxx.xxx)

and if I want A and D to be connected using some sort of ip address and B and C to be connected to each others, then what kind of ip address or method should I have to use so that those people can successfully connect to each others? I've successfully connected A and B in network NET_A using local IPv4 address achieved using following code:

InetAddress thisIp =InetAddress.getLocalHost();
System.out.println("IP:"+thisIp.getHostAddress());

Also following is serversocket code:

   ServerSocket ss = new ServerSocket(PORT);
   ss.setSoTimeout(10000);
   connection = ss.accept();

Follow is client socket code:

connection = new Socket(InetAddress.getByName(SERVER_IP), PORT);
4

1 回答 1

0

您需要在两个网络中设置一个具有 IP 的路由器,并向所有主机添加一条路由,告诉它们远程网络和路由器(网关)的 IP 地址。

您可以通过启用数据包转发来设置 linux 机器来轻松完成此操作。只需添加net.ipv4.ip_forward = 1/etc/sysctl.conf运行sudo sysctl -p

作为套接字程序员,路由通常不是您需要关心的事情,它是为您完成的。

于 2012-06-30T16:19:58.790 回答