Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 修复C中套接字异常的原因#_C#_Winforms_Sockets_Network Programming - Fatal编程技术网

C# 修复C中套接字异常的原因#

C# 修复C中套接字异常的原因#,c#,winforms,sockets,network-programming,C#,Winforms,Sockets,Network Programming,我正在为通过internet连接到它的多个winform客户端创建一个小型服务器。 以下代码在设置为本地地址时有效: 服务器: static void Main(string[] args) { TcpListener serverSocket = new TcpListener(9000); TcpClient clientSocket = default(TcpClient); int counter = 0; serverSocket.Start();

我正在为通过internet连接到它的多个winform客户端创建一个小型服务器。 以下代码在设置为本地地址时有效:

服务器:

static void Main(string[] args)
{
    TcpListener serverSocket = new TcpListener(9000);
    TcpClient clientSocket = default(TcpClient);
    int counter = 0;

    serverSocket.Start();
    Console.WriteLine("Chat Server Started ....");
    counter = 0;
    while ((true))
    {
        counter += 1;
        clientSocket = serverSocket.AcceptTcpClient();

        byte[] bytesFrom = new byte[10025];
        string dataFromClient = null;

        NetworkStream networkStream = clientSocket.GetStream();
        networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
        dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
        dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));

        clientsList.Add(dataFromClient, clientSocket);

        broadcast(dataFromClient + " Joined ", dataFromClient, false);

        Console.WriteLine(dataFromClient + " Joined chat room ");
        handleClinet client = new handleClinet();
        client.startClient(clientSocket, dataFromClient, clientsList);
    }

    clientSocket.Close();
    serverSocket.Stop();
    Console.WriteLine("exit");
    Console.ReadLine();
}
Winform客户端:

public partial class Form1 : Form
{
    System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
    NetworkStream serverStream = default(NetworkStream);
    string readData = null;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        readData = "Conected to Chat Server ...";
        msg();
        clientSocket.Connect("127.0.0.1", 9000);
        serverStream = clientSocket.GetStream();

        byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textBox3.Text + "$");
        serverStream.Write(outStream, 0, outStream.Length);
        serverStream.Flush();

        Thread ctThread = new Thread(getMessage);
        ctThread.Start();
    }
}
这些代码在通过网络相互交谈时工作得很好。 但是当我决定改变这行代码时

clientSocket.Connect(“127.0.0.1”,9000)
clientSocket.Connect(“81.62.126.41”,9000)

对于我的ip地址,它给了我以下SocketException:

连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机没有响应

所以我尝试了一些事情。端口转发端口9000,因为我在路由器后面。正在为端口9000创建入站和出站规则。当我的服务器运行时,我去了,而网站实际上可以看到我的服务

有人知道我能做些什么来解决这个问题吗

下面是服务器运行时netstat的图片。我想这可能会有帮助。
我也有类似的问题

  • 如果您只更改代码中的一个字符串,并且它停止工作,请仔细检查您正在使用的工具。某些内部代理可能允许/不允许您与内部(127.0.0.1)或外部ip通信
  • 在浏览器127.0.0.1:9000和81.62.126.41:9000中检查相同的URL。如果在第二种情况下它没有打开,则在防火墙中或设置服务器以允许外部访问时发出问题,而不是在代码中
  • 您的异常意味着您的服务器出现问题:您可以尝试打开
    http://81.62.12.32/
    并打开
    http://81.62.126.41:9000/
    您将看到两个不同的错误
    结论:问题不在代码中。它在您的web服务器设置或防火墙设置中。

    根据您的描述,有东西阻止了主机的IP数据包。请尝试使用Wireshark或Microsoft Network Monitor查看主机上的数据包,它会为您指明数据包被过滤的方向。@William不知道这是什么,但是我会测试它。@William我使用Wireshark,我已经能够捕获我想要的包和信息,但我不知道我应该寻找什么。在一个正常工作的场景中捕获包,这将显示您想要一个成功的TCP连接。然后在失败的场景中捕获服务器和客户端上的数据包,将结果与工作集进行比较,您应该能够看到哪一方被阻止。