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 ?