Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.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/6/multithreading/4.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#_Multithreading_Tcp - Fatal编程技术网

C# 无限,而循环只得到一个结果

C# 无限,而循环只得到一个结果,c#,multithreading,tcp,C#,Multithreading,Tcp,我正在编写一个P2P程序,它必须在多个实例之间发送和接收数据。 当我创建了一个单独的线程来监听无穷远while循环中的传入数据时,我遇到了一个问题,但我只得到第一条消息,然后就什么都没有了。 我做错了什么? 代码: 如果您想使用任务(我强烈建议使用这种方法),那么对于每个传入连接,您需要一个侦听器任务和一个tcp客户端响应程序任务 这里有一个基本的TCP侦听器任务 static async Task StartTcpServerAsync() { var tcpListener = ne

我正在编写一个P2P程序,它必须在多个实例之间发送和接收数据。 当我创建了一个单独的线程来监听无穷远while循环中的传入数据时,我遇到了一个问题,但我只得到第一条消息,然后就什么都没有了。 我做错了什么? 代码:

如果您想使用任务(我强烈建议使用这种方法),那么对于每个传入连接,您需要一个侦听器任务和一个tcp客户端响应程序任务

这里有一个基本的TCP侦听器任务

static async Task StartTcpServerAsync()
{
    var tcpListener = new TcpListener(new IPEndPoint(IPAddress.Any, 9999));
    tcpListener.Start();
    while (true)
    {
        var tcpClient = await tcpListener.AcceptTcpClientAsync();
        _ = StartTcpClientAsync(tcpClient);
    }
}
我们初始化
TcpListener
,并开始监听连接。每次获得连接时,我们都会将关联的
TcpClient
交给一个新任务

这是一个服务器端TCP客户端任务-它位于服务器上,由侦听器启动

static async Task StartTcpClientAsync(TcpClient tcpClient)
{
    Console.WriteLine($"Connection from: [{tcpClient.Client.RemoteEndPoint}]");

    var stream = tcpClient.GetStream();
    var buffer = new byte[1024];

    while (true)
    {
        int x = await stream.ReadAsync(buffer, 0, 1024);
        Console.WriteLine($"[{tcpClient.Client.RemoteEndPoint}] _ " +
            $"read {x} bytes {System.Text.Encoding.UTF8.GetString(buffer)}");
    }
}
在这个例子中,我们只是写出接收到的字节

最后

为了完整起见,这里有一个Main和一个TCPSender-通常发送方将位于完全不同的计算机上,但为了显示任务的威力,我们将从相同的进程运行它

static async Task Main(string[] args)
{
    _ = StartTcpServerAsync();
    await Task.Delay(2000);
    await RunSenderAsync();
}
static async Task RunSenderAsync()
{
    var tcpClient = new TcpClient("127.0.0.1", 9999);
    var s = tcpClient.GetStream();
    tcpClient.NoDelay = true;
    for (var i = 65; i < 91; i++)
    {
        s.Write(BitConverter.GetBytes(i), 0, 1);
        await Task.Delay(1000);
    }
}
static async Task Main(字符串[]args)
{
_=StartTcpServerAsync();
等待任务。延迟(2000);
等待RunSenderAsync();
}
静态异步任务RunSenderAsync()
{
var TCP客户=新TCP客户(“127.0.0.1”,9999);
var s=tcpClient.GetStream();
tcpClient.NoDelay=true;
对于(变量i=65;i<91;i++)
{
s、 写入(位转换器.GetBytes(i),0,1);
等待任务。延迟(1000);
}
}

我发现有一个地方让很多人感到困惑,那就是您需要在服务器上为您接收的每个连接提供一个TCP客户端。

这个答案是针对Selvin的

我已将Main更改为运行三个
RunSenderAsync
客户端

这是控制台窗口中的输出。如您所见,三个客户端连接到侦听器,然后侦听器启动三个任务,每个任务将在WorkerThread池中的工作线程上运行。我已经更新了
Writeline()
以打印线程ID,这样您就可以看到任务在WorkerThreads中移动

BiDi source: [127.0.0.1:50279] connected to Pipeline Server
Connection from: [127.0.0.1:50279]
BiDi source: [127.0.0.1:50280] connected to Pipeline Server
BiDi source: [127.0.0.1:50281] connected to Pipeline Server
Connection from: [127.0.0.1:50280]
[127.0.0.1:50280] read 1 bytes A                                                                                             
    ^^ On Thread ID: 7
[127.0.0.1:50279] read 1 bytes A                                                                                             
    ^^ On Thread ID: 6
Connection from: [127.0.0.1:50281]
[127.0.0.1:50281] read 1 bytes A                                                                                             
    ^^ On Thread ID: 5
[127.0.0.1:50280] read 1 bytes B                                                                                             
    ^^ On Thread ID: 7
[127.0.0.1:50279] read 1 bytes B                                                                                             
    ^^ On Thread ID: 8
[127.0.0.1:50281] read 1 bytes B                                                                                             
    ^^ On Thread ID: 5
[127.0.0.1:50279] read 1 bytes C                                                                                             
    ^^ On Thread ID: 6
[127.0.0.1:50281] read 1 bytes C                                                                                             
    ^^ On Thread ID: 5
[127.0.0.1:50280] read 1 bytes C                                                                                             
    ^^ On Thread ID: 7
[127.0.0.1:50280] read 1 bytes D                                                                                             
    ^^ On Thread ID: 5
[127.0.0.1:50279] read 1 bytes D                                                                                             
    ^^ On Thread ID: 7
[127.0.0.1:50281] read 1 bytes D                                                                                             
    ^^ On Thread ID: 8
[127.0.0.1:50280] read 1 bytes E                                                                                             
    ^^ On Thread ID: 5
[127.0.0.1:50281] read 1 bytes E                                                                                             
    ^^ On Thread ID: 7
[127.0.0.1:50279] read 1 bytes E                                                                                             
    ^^ On Thread ID: 8
[127.0.0.1:50280] read 0 bytes E  
以下是完整的源代码:

using System;
using System.Threading;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;

namespace ReadAsync
{
    class Program
    {
        static async Task StartTcpClientAsync(TcpClient tcpClient)
        {
            Console.WriteLine($"    Connection from: [{tcpClient.Client.RemoteEndPoint}]");

            var stream = tcpClient.GetStream();
            var buffer = new byte[1024];

            while (true)
            {
                int x = await stream.ReadAsync(buffer, 0, 1024);
                Console.WriteLine($"    [{tcpClient.Client.RemoteEndPoint}] " +
                    $"read {x} bytes {System.Text.Encoding.UTF8.GetString(buffer)} "+
                    $"        ^^ On Thread ID: {Thread.CurrentThread.ManagedThreadId}");
            }
        }
        static async Task StartTcpServerAsync()
        {
            var tcpListener = new TcpListener(new IPEndPoint(IPAddress.Any, 9999));
            tcpListener.Start();

            while (true)
            {
                var tcpClient = await tcpListener.AcceptTcpClientAsync();

                _ = StartTcpClientAsync(tcpClient);
            }
        }
        static async Task Main(string[] args)
        {
            _ = StartTcpServerAsync();

            await Task.Delay(2000);

            _ = RunSenderAsync();
            _ = RunSenderAsync();
            await RunSenderAsync();
        }
        static async Task RunSenderAsync()
        {
            using var tcpClient = new TcpClient("127.0.0.1", 9999);

            Console.WriteLine($"    BiDi source: [{tcpClient.Client.LocalEndPoint}] connected to Pipeline Server");

            using var s = tcpClient.GetStream();

            tcpClient.NoDelay = true;

            for (var i = 65; i < 70; i++)
            {
                s.Write(BitConverter.GetBytes(i), 0, 1);
               await Task.Delay(1000);
            }
        }
    }
}
使用系统;
使用系统线程;
使用System.Threading.Tasks;
Net系统;
使用System.Net.Sockets;
名称空间ReadAsync
{
班级计划
{
静态异步任务STARTTCPClientSync(TcpClient TcpClient)
{
WriteLine($“来自:[{tcpClient.Client.RemoteEndPoint}]的连接]”;
var stream=tcpClient.GetStream();
var buffer=新字节[1024];
while(true)
{
intx=wait stream.ReadAsync(缓冲区,0,1024);
Console.WriteLine($“[{tcpClient.Client.RemoteEndPoint}]”+
$“读取{x}字节{System.Text.Encoding.UTF8.GetString(缓冲区)}”+
$“^^^在线程ID上:{Thread.CurrentThread.ManagedThreadId}”);
}
}
静态异步任务StartTcpServerAsync()
{
var tcpListener=new tcpListener(new IPEndPoint(IPAddress.Any,9999));
tcpListener.Start();
while(true)
{
var tcpClient=await tcpListener.AcceptTcpClientAsync();
_=starttcpclientsync(tcpClient);
}
}
静态异步任务主(字符串[]args)
{
_=StartTcpServerAsync();
等待任务。延迟(2000);
_=RunSenderAsync();
_=RunSenderAsync();
等待RunSenderAsync();
}
静态异步任务RunSenderAsync()
{
使用var tcpClient=新的tcpClient(“127.0.0.1”,9999);
Console.WriteLine($“BiDi源:[{tcpClient.Client.LocalEndPoint}]已连接到管道服务器”);
使用var s=tcpClient.GetStream();
tcpClient.NoDelay=true;
对于(变量i=65;i<70;i++)
{
s、 写入(位转换器.GetBytes(i),0,1);
等待任务。延迟(1000);
}
}
}
}

您应该有两个线程。。。一个接受,一个阅读。。。另外,每个重新加入线程都应该有自己的客户端引用。。。这里不要提你的任务使用是错误的。。。还有一条评论:为什么不首先使用TcpListener的异步api?这将不起作用,因为您没有等待
StartTcpClientSync
so
stream。ReadAsync
也没有等待
stream。ReadAsync
不创建线程。。。它在系统中断时工作,不等待就不工作了。。。它不同于
任务。运行
。。另外,
System.Text.Encoding.UTF8.GetString(缓冲区)
是错误的。。。它应该是
System.Text.Encoding.UTF8.GetString(缓冲区,0,x)
Hi@Selvin
starttcpclientsync
。如果等待,则在“starttcpclientsync”完成之前,我们将永远不会看到任何新的连接,而“starttcpclientsync”只有在第一个客户端断开连接时才会发生。因为这是从控制台应用程序运行的,所以没有同步上下文。我已检查(复制代码并编译)…它不工作-它只按预期接受,不读取(也按预期)修复程序将替换
\uu=starttcpclientsync(tcpClient)
Task.Run(异步()=>await=starttcpclientsync(tcpClient))
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;

namespace ReadAsync
{
    class Program
    {
        static async Task StartTcpClientAsync(TcpClient tcpClient)
        {
            Console.WriteLine($"    Connection from: [{tcpClient.Client.RemoteEndPoint}]");

            var stream = tcpClient.GetStream();
            var buffer = new byte[1024];

            while (true)
            {
                int x = await stream.ReadAsync(buffer, 0, 1024);
                Console.WriteLine($"    [{tcpClient.Client.RemoteEndPoint}] " +
                    $"read {x} bytes {System.Text.Encoding.UTF8.GetString(buffer)} "+
                    $"        ^^ On Thread ID: {Thread.CurrentThread.ManagedThreadId}");
            }
        }
        static async Task StartTcpServerAsync()
        {
            var tcpListener = new TcpListener(new IPEndPoint(IPAddress.Any, 9999));
            tcpListener.Start();

            while (true)
            {
                var tcpClient = await tcpListener.AcceptTcpClientAsync();

                _ = StartTcpClientAsync(tcpClient);
            }
        }
        static async Task Main(string[] args)
        {
            _ = StartTcpServerAsync();

            await Task.Delay(2000);

            _ = RunSenderAsync();
            _ = RunSenderAsync();
            await RunSenderAsync();
        }
        static async Task RunSenderAsync()
        {
            using var tcpClient = new TcpClient("127.0.0.1", 9999);

            Console.WriteLine($"    BiDi source: [{tcpClient.Client.LocalEndPoint}] connected to Pipeline Server");

            using var s = tcpClient.GetStream();

            tcpClient.NoDelay = true;

            for (var i = 65; i < 70; i++)
            {
                s.Write(BitConverter.GetBytes(i), 0, 1);
               await Task.Delay(1000);
            }
        }
    }
}