1
votes

I have Ubuntu 11.10 running into vmware. I am running a Java tcp server into Ubuntu. So when I am connecting this server with a client from Ubuntu, its working fine. But when I am trying to connect with this server from another OS (Windows 7), its showing connection error. I tried to connect with both java and C# client, but both time its showing connection error. Here is the Error message :

System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 192.168.0.129:20000 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.Connect(EndPoint remoteEP) at TestUbuntuSocket.Form1.button1_Click(Object sender, EventArgs e) at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Here is my C# client Socket code :

        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        s.Connect(new IPEndPoint(IPAddress.Parse(textBox1.Text), int.Parse(textBox2.Text)));
        if (s.Connected)
        {
            s.Send(Encoding.ASCII.GetBytes(textBox3.Text));
        }
        else
            MessageBox.Show("Not Connected");

And here is my Java Client Socket code :

    Socket socket = null;
    try{
        socket = new Socket(txtIp.getText(), Integer.parseInt(txtPort.getText()));
    }
    catch(Exception exc){
        JOptionPane.showMessageDialog(this, "Server is not available!!");
        return;
    }
    try{
        PrintWriter out = new PrintWriter(socket.getOutputStream(),true);
        out.println(txtMessage.getText());
        socket.close();
    }
    catch(Exception exc){
        JOptionPane.showMessageDialog(this, "Error when sending data!!");
    }

Server Java Code :

        ServerSocket s = new ServerSocket(port);

        while (start)
        {
            Socket incoming = s.accept();
            BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
            String message = "";
            String line = in.readLine();
            while(line != null){
                 message += line;
                 line = in.readLine();
            }
            JOptionPane.showMessageDialog(null, message);
        }

This is the output of 'sudo netstat -atnp':

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 408/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 925/cupsd
tcp6 0 0 ::1:42098 :::* LISTEN 2168/java
tcp6 0 0 :::22 :::* LISTEN 408/sshd
tcp6 0 0 ::1:631 :::* LISTEN 925/cupsd
tcp6 0 0 :::20000 :::* LISTEN 3015/java
tcp6 0 0 127.0.0.1:20000 127.0.0.1:56269 CLOSE_WAIT 3015/java

So What I am dong wrong ?

3
Can you please replace the screenshot with the full error message in text? - fge
Firewall settings? I guess the port is closed for non-local access. - Fildor
What's the exception in Java code? - Eng.Fouad
@Fildor I opened the port before running the appliaction. My command was : sudo ufw allow 20000 - Barun
@Eng.Fouad java.net.ConnectException: Connection refused: connect - Barun

3 Answers

0
votes

Check the firewall with telnet host port.
If you get connection refused/timeout, there is either a network issue or the server is not working, if you can enter a carriage return and the line changes/the server answers, things are good.
This link explains the installation of telnet on Windows.

0
votes

Are there any errors on the server side? Your server only accepts one request at a time, usually you would pass the accepted socket into another thread to deal with it, without blocking the server. I'm not exactly sure how the Dialog blocks the server thread, do you see any reaction on server side?

Another thing you could try, is setting a different port, like 8080 this usually shouldn't be blocked.

0
votes

Don't know much java but it's usual to have to call listen/accept type methods on the server, without these the server will not be listening for incoming connections.

If you use telnet to connect locally on the server on the same ip/port and it fails this would point to the same diagnosis. If telnet suceeds its perhaps a firewall/network setup issue.

[Edit]

I think your problem may be that your ubuntu box is set up for ipv6 and your java code is only listening on ipv6.

Check the docs on ipv6 here.

Checking the output of your netstat, your java code is featured here:

tcp6 0 0 :::20000 :::* LISTEN 3015/java

Which means you have a java process listening on ipv6 multicast (any ipv6 address can connect) using port 20000.

Without a magic process to tunnel ipv4 requests to ipv6 inside the ubuntu machine or changing your code to listen on ipv4 only/as well, your windows client will need to:

You should be able to find the ipv6 address using the system menu or running "ifconfig". It will be harder to type in than the ipv4 address, but you should be able to ping that address from windows 7 as well as connect to your server.