Cannot connect Java socket server and client -


I'm learning about socket programming with Java Using Java 8, this is a simple server:

  import java.net.InetAddress; Import java.net.ServerSocket; Import Java.net.Socket; The public square throws main {public static zero principal (string [] args) {System.out.println ("My IP address" + InetAddress.getLocalHost). GetHostAddress ()); ServerSocket Server = New ServerSocket (4031); Socket socket = server.accept (); System.out.println ("is connected to anyone!" + Socket); Socket.close (); Server.close (); }}   

Shows all the host's address and then waits for the socket to connect.

And this is the customer:

  import java.net.Socket; Public square main {public static zero principal (string [] args) Exception {System.out.println ("trying to connect ...") throws; Socket s = new socket ("123.456.7.890", 4031); Println ("cool"); S.close (); }}   

When I run the server and the client in my home (in two separate computers), it works: I just capture the IP address displayed by the server I am supplying it to the customer, and have established the connection.

Now this is the only server in my house, while I am running my client in the office. Under the same process, I supplied the IP address printed by the server to my client. However, the connection never establishes: neither does the server or the client socket generate.

  • Java happens between both 7 and 8.
  • Tried several different ports
  • No exception is thrown
  • I am using Mac OSX Mavericks 10.9.5 on both machines.
  • Firewalls are locked on both machines In fact, I've used Chrome Remote Desktop to control the server and check its status.
  • It works fine when both machines are in the same house.
  • And yes, it also works for the local host.

    The above points suggest that this issue may have been used to connect to my server with IP address InetAddress.getLocalHost (). GetHostAddress () is not suitable for this - although I guess it does this work with two machines in the same house.

    What is this I am doing wrong?

    You say that your firewalls are locked on both machines, but you also have to ensure that < / P>

    1. You join your public IP, the report of your server is probably the LAN IP that you have received through DHCP from your router < / P>

    2. You also need to make sure that your router is configured to forward the connection to your Wajnik IP port 4031 (the IP address of your server and not) on some other machine on your local network. Finally, there is a small risk that your ISP is prohibited setting up the server at home. In this case, they may be so bad that they block incoming connections.

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -