Skip to content
Advertisement

Java socket only use loopback

I have tried to connect my laptop to another laptop (on the same network, both using Windows 10) using java socket.

Here is the client code :

Socket SocketServIAChat;
try 
{
    SocketServIAChat = new Socket("192.168.1.10", 50065);
} 
catch (IOException ex) 
{
    jTextAreaErrorMessage.setText(ex.getMessage());
    return;
}

And the server code :

try 
{
    listeningSocket = new ServerSocket(50065);
    listeningSocket.setSoTimeout(1500);
} 
catch (IOException ex) {
    Logger.getLogger(ThreadLoginRequests.class.getName()).log(Level.SEVERE, null, ex);}

...

Socket socketLogin = listeningSocket.accept();

But for some reason, even if I specify an ip address, the laptop only look for a host in loopack, not on the private network. Here is the proof :

Loopback traffic (wireshark)

Wi-fi traffic (wireshark)

I also added a rule to the firewall of both laptop to accept tcp incoming and outgoing on the port 50065.

So is there a way to parameterize the socket to use the right network interface (the one using the private network) or am I missing something here ?

Advertisement

Answer

First, check if you are able to ping one laptop from another. If yes then try below.

The Address field contains the base address that this listen socket is listening on. It contains the IP address and the port number.

If your listen socket listens on all IP addresses for the machine, the IP part of the address is 0.0.0.0.

Try to bind on your local IP or on any address using “0.0.0.0”

InetAddress addr = InetAddress.getByName("0.0.0.0");// Based on your requirement you can use local address also "192.168.1.10"

try 
{
    listeningSocket = new ServerSocket(50065, 50, addr);
    listeningSocket.setSoTimeout(1500);
} 
catch (IOException ex) {
    Logger.getLogger(ThreadLoginRequests.class.getName()).log(Level.SEVERE, null, ex);}

Advertisement