繁体   English   中英

使用C#TCP套接字将Java TCP服务器连接到Windows 7中的Ubuntu

[英]Connecting with Java TCP server into Ubuntu with C# TCP socket into Windows 7

我有运行VMware的Ubuntu 11.10。 我正在将Java tcp服务器运行到Ubuntu中。 因此,当我将此服务器与来自Ubuntu的客户端连接时,其工作正常。 但是,当我尝试从另一个操作系统(Windows 7)与该服务器连接时,它显示连接错误。 我尝试同时连接Java和C#客户端,但是两次都显示连接错误。 这是错误消息:

System.Net.Sockets.SocketException:无法建立连接,因为目标计算机在System.Net.Sockets.Socket上主动拒绝了System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot,SocketAddress socketAddress)上的192.168.0.129:20000在System的System.Windows.Forms.Control.OnClick(EventArgs e)在System.Windows.Forms.Button.OnClick(EventArgs e)处的TestUbuntuSocket.Form1.button1_Click(Object sender,EventArgs e)处的.Connect(EndPoint remoteEP)。 System.Windows.Forms.Control.WndProc(Message&m)处的System.Windows.Forms.Control.WmMouseUp(Message&m,MouseButtons button,Int32 clicks)处的Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)(位于System.Windows.Forms.Control.ControlNativeWindow)处的System.Windows.Forms.Button.WndProc(Message&m)中的Forms.ButtonBase.WndProc(Message&m)。 System.Windows.Forms.NativeWindow.Callback处的WndProc(Message&m)(IntPtr hWnd,Int32 msg,IntPtr wparam,Int Ptr 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);
        }

这是'sudo netstat -atnp'的输出:

Proto Recv-Q Send-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:*监听925 / cupsd
tcp6 0 0 :: 1:42098 ::: * LISTEN 2168 / java
tcp6 0 0 ::: 22 ::: *听408 / sshd
tcp6 0 0 :: 1:631 ::: *听925 / cupsd
tcp6 0 0 ::: 20000 ::: *听3015 / java
tcp6 0 0 127.0.0.1:20000 127.0.0.1:56269 CLOSE_WAIT 3015 / java

那我错了吗?

Java知识不多,但是通常必须在服务器上调用listen / accept类型的方法,否则,服务器将不会监听传入的连接。

如果您使用telnet在同一ip /端口上的服务器上本地连接,但失败,则将指向同一诊断。 如果telnet解决了它的防火墙/网络设置问题。

[编辑]

我认为您的问题可能是您的ubuntu框设置为ipv6,而您的Java代码仅在ipv6上侦听。

此处检查ipv6上的文档。

检查netstat的输出,此处显示了Java代码:

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

这意味着您有一个Java进程使用端口20000侦听ipv6组播(任何ipv6地址都可以连接)。

如果没有将ipv4请求隧道传输到ubuntu机器内部的ipv6的魔术过程,或者没有更改您的代码以仅/也监听ipv4的过程,则Windows客户端将需要:

您应该可以使用系统菜单或运行“ ifconfig”来找到ipv6地址。 键入比ipv4地址更难,但是您应该能够从Windows 7 ping该地址并连接到服务器。

使用telnet主机端口检查防火墙。
如果连接被拒绝/超时,则可能是网络出现问题或服务器无法正常工作,如果您可以输入回车符并且线路更改/服务器可以应答,则表示一切正常。
此链接说明了Windows上telnet的安装。

服务器端是否有任何错误? 您的服务器一次只接受一个请求,通常您会将接受的套接字传递给另一个线程来处理它,而不会阻塞服务器。 我不确定对话框如何阻止服务器线程,您是否在服务器端看到任何反应?

您可以尝试的另一件事是设置其他端口,例如8080,通常不应阻止该端口。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM