Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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#_Sockets_Communication_Tcpclient_Networkstream - Fatal编程技术网

C# 服务器连接错误

C# 服务器连接错误,c#,sockets,communication,tcpclient,networkstream,C#,Sockets,Communication,Tcpclient,Networkstream,我现在正在尝试使用服务器。我有以下代码: 我正在使用“ParameterizedThreadStart”在线程中启动“HandleCC”方法。我正在尝试连接“127.0.0.1”。我可以连接。当我连接时,第一个断点很好,第二个断点也很好,但代码会停止。控制台仍在运行,但没有人从以下断点中断代码。 请帮助我,对不起我的英语 完整代码: 在任何客户机-服务器应用程序中,都需要客户机和服务器。你写了服务器。 下面是一个客户机示例,该客户机经过测试,并将您的代码用作服务器: TcpClient

我现在正在尝试使用服务器。我有以下代码:

我正在使用“ParameterizedThreadStart”在线程中启动“HandleCC”方法。我正在尝试连接“127.0.0.1”。我可以连接。当我连接时,第一个断点很好,第二个断点也很好,但代码会停止。控制台仍在运行,但没有人从以下断点中断代码。 请帮助我,对不起我的英语

完整代码:


在任何客户机-服务器应用程序中,都需要客户机和服务器。你写了服务器。 下面是一个客户机示例,该客户机经过测试,并将您的代码用作服务器:

    TcpClient client = new TcpClient("localhost", 20345);
    NetworkStream stream = client.GetStream();

    while (true)
    {
        string message = Console.ReadLine();
        Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);  
        // Send the message to the connected TcpServer. 
        stream.Write(data, 0, data.Length);
    }

    stream.Close();
    client.Close();
将此客户端放在单独的控制台应用程序中,然后:

启动服务器 然后在新的Visual studio实例中启动客户端,或启动exe 在客户端控制台窗口中键入一些内容 接下来,它将完成更新您正在使用的StringBuilder的步骤。
如何创建客户端?您正在连接到哪个服务器?读取不返回的最可能原因是服务器不发送任何数据。而且你使用接球突破的方式似乎是非常错误的。另外,你能把你的代码作为文本而不是截图发布吗?它不会停止,它会阻塞。发送一些东西。@user35443,好的,你介意回答其他问题吗?我正在连接127.0.0.1 me,我是如何创建客户端的?您可以在代码中看到它@汉斯·帕桑:什么?那为什么你的客户不工作呢?如果它对我有效,则意味着它不是代码问题,可能是某种设置。我的客户端工作,但我的服务器不工作。但我的防火墙可能正在阻止端口。
    TcpClient client = new TcpClient("localhost", 20345);
    NetworkStream stream = client.GetStream();

    while (true)
    {
        string message = Console.ReadLine();
        Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);  
        // Send the message to the connected TcpServer. 
        stream.Write(data, 0, data.Length);
    }

    stream.Close();
    client.Close();