我將Ubuntu 11.10運行到vmware中。我正在運行一個Java tcp服務器到Ubuntu。所以當我連接這臺服務器與Ubuntu的客戶端時,它的工作正常。但是,當我嘗試從另一個操作系統(Windows 7)連接到此服務器時,顯示連接錯誤。我試圖連接java和C#客戶端,但兩次都顯示連接錯誤。以下是錯誤消息:將Java TCP服務器與C#TCP套接字連接到Windows 7中
System.Net.Sockets.SocketException:無連接可以作出,因爲目標機器積極地拒絕它192.168.0.129:20000 在System.Net.Sockets.Socket.DoConnect(端點endPointSnapshot,爲SocketAddress的SocketAddress) 在System.Net.Sockets.Socket.Connect(端點remoteEP) 在TestUbuntuSocket.Form1.button1_Click(對象發件人,EventArgs的) 在System.Windows.Forms.Control.OnClick(EventArgs的) 在System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(M essage &米,MouseButtons按鈕,點擊的Int32) 在System.Windows.Forms.Control.WndProc(消息&米) 在System.Windows.Forms.ButtonBase.WndProc(消息&米) 在System.Windows.Forms的。 Button.WndProc(消息&米) 在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息&米) 在System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息&米) 在System.Windows。 Forms.NativeWindow.Callback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)
這裏是我的C#客戶端的Socket代碼:
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");
這裏是我的Java客戶端套接字代碼:
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!!");
}
服務器Java代碼:
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);
}
這是「須藤的輸出netstat -atnp':
Proto Recv- Q發送Q本地地址外部地址狀態PID /程序名稱 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
所以我是東錯了?
您能否用文本中的完整錯誤信息替換屏幕截圖? – fge
@fge沒關係,只是。 – Barun
防火牆設置?我猜這個端口是關閉的,非本地訪問。 – Fildor