问题
I am using a laptop as my server and a mobile device as my client. I need to send messages between them. I am able to do that by using the local ip address of the laptop and connecting them both to the same wifi network. But how I do use the ip address if I need to send messages though they are connected to different internet through different wifi networks. This is the code in client:
client = new Socket("192.168.0.103", 4444); //local ip address of server
This is the code in server:
serverSocket = new ServerSocket(4444); //Server socket
clientSocket = serverSocket.accept(); //accept the client connection
inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader); //get the client message
String input=bufferedReader.readLine();
Can I use gateways or dynamic or static ip to solve this problem ? If so how?
回答1:
You need to configure the modem/router on the server side so that external calls (from the client mobil) to the port 4444 will be redirected to the correct device in the subnet.
This is a configuration change that must be done manually on the router. Also, not all routers support this function of service redirecting (but most of them do). Even when a router supports it, sometime it might be hard to find the correct way of doing it because this can have different names between models.
Usually, you must do this even if there is a single device connected to the router.
On some installations, the router and the model are two different physical devices but you can also find these two functions united inside a single device. If you have the capabilities of connecting multiple devices to your modem then likely, your modem also offer the routing functionality. However, I remember seeing in the past some modems for which it was impossible to redirect external service calls to a particular device (or at least, I personally never find how to do this for these models).
If your network has a single router - like in most home installations; then there is no real difference between a gateway and a router. However, if your server is located on a complex local network with multiple routers, then it can become very hard to properly configure the whole network; especially when you have security checks put into place.
So, you should either consult the documentation for your wify device if you are at home or ask the person responsible for the company network to do the configuration.
来源:https://stackoverflow.com/questions/15371694/how-to-connect-client-and-server-with-the-help-of-ip-address-which-are-connected