2

I have a use case, where I have to send data from a .NET application, to a Java application and I'm trying to do this using sockets. I have a server created using C# -

        TcpListener tcpListener = null;
        IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];

        try
        {
            // Set the listener on the local IP address 
            // and specify the port.
            tcpListener = new TcpListener(ipAddress, 13);
            tcpListener.Start();
            Console.WriteLine("The server is running at port 13...");
            Console.WriteLine("The local End point is  -" +
                              tcpListener.LocalEndpoint);
            output = "Waiting for a connection...";
            Console.WriteLine(output);

            Socket s = tcpListener.AcceptSocket();
            Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
        }
        catch (Exception e)
        {
            output = "Error: " + e.ToString();
            Console.WriteLine(output);
        }
    }

On the Java side, I have created a socket which listens to the same host and port -

Socket socket;
    try {
        socket = new Socket( "localhost", 13);
        System.out.println("Connection established");
        BufferedReader input =
                new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String answer = input.readLine();
        System.out.println(answer);
        socket.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

I'm getting the below error - java.net.ConnectException: Connection refused: connect. I have used telnet localhost 13, to check if my server is really running, and it is. So i don't think it could be an issue with server not running or firewalls, since both are running locally. Any ideas on how to resolve this?

8
  • try using port numbers of higher range i.e thousands series Commented Oct 5, 2016 at 9:21
  • you could also try to bind to the wildcard address (0.0.0.0). Maybe you also have an IPv6 binding on localhost and your c# server only listens to that. Commented Oct 5, 2016 at 9:23
  • The connect method in net library give an exception if you use ip address 127.0.0.1 (local host). You need to use the IP address of the PC when using the connect method. The Listener should use IPAddress.Any. The Windows operating System will automatically forward messages from the PC IP address to the virtual socket IP.Any. Commented Oct 5, 2016 at 9:24
  • @Sanjeev - Tried that already, didn't work. Commented Oct 5, 2016 at 9:25
  • 1
    No, just that java will probably give same error. There is a host file on the windows folder that defines localhost. On some PCs it is an IP address others use the loopback address 127.0.0.1. heinzbeinz gave a solution for the server but not your java client that is creating the exception. I think both solutions are needed. The java client will not be able to connect to the current server. Commented Oct 5, 2016 at 9:48

1 Answer 1

4

I tried your code and I had exactly the same problem. Your C# TCP Server only binds to the IPv6 interface (check e.g. the resource monitor listening addresses). If you change your server to new TcpListener(IPAddress.Any, 13) it should work.

Sign up to request clarification or add additional context in comments.

2 Comments

Awesome. It does work. However, if I want to bind an IPV4 interface, instead of IPV6, can it be done?
you could filter your ipadresses via: IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList.First(a => a.AddressFamily == AddressFamily.InterNetwork)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.