C#实现读取IPv6 UDP Socket数据,再发送出去

C#实现读取IPv6 UDP Socket数据,再发送出去。

不知为何,黑框点一下就停止刷新了,再点一下,就继续刷新了。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace ConsoleApplication1
{
class Program
{
static Socket s1, s2, s3;
static int s1_rxcnt = ;
static int s2_txcnt = ;
static int s3_txcnt = ;
static void Main(string[] args)
{
s1 = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
s1.Bind(new IPEndPoint(IPAddress.IPv6Any, )); s2 = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
s2.Bind(new IPEndPoint(IPAddress.IPv6Loopback, )); s3 = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
s3.Bind(new IPEndPoint(IPAddress.IPv6Loopback, )); Thread t = new Thread(ReciveMsg);//开启接收消息线程
t.Start();
} static void ReciveMsg()
{
byte[] buffer = new byte[];
int length;
while (true)
{
length = s1.Receive(buffer);
s1_rxcnt++; s2.SendTo(buffer, new IPEndPoint(IPAddress.IPv6Loopback, ));
s2_txcnt++; s3.SendTo(buffer, new IPEndPoint(IPAddress.IPv6Loopback, ));
s3_txcnt++; Console.WriteLine("s1_rxcnt: " + s1_rxcnt.ToString("D8") +
", s2_txcnt: " + s2_txcnt.ToString("D8") +
", s3_txcnt: " + s3_txcnt.ToString("D8"));
}
}
}
}

输出样式:

s1_rxcnt: , s2_txcnt: , s3_txcnt:
s1_rxcnt: , s2_txcnt: , s3_txcnt:
s1_rxcnt: , s2_txcnt: , s3_txcnt:
s1_rxcnt: , s2_txcnt: , s3_txcnt:
s1_rxcnt: , s2_txcnt: , s3_txcnt:
s1_rxcnt: , s2_txcnt: , s3_txcnt:
s1_rxcnt: , s2_txcnt: , s3_txcnt:
s1_rxcnt: , s2_txcnt: , s3_txcnt:
s1_rxcnt: , s2_txcnt: , s3_txcnt:
s1_rxcnt: , s2_txcnt: , s3_txcnt:
s1_rxcnt: , s2_txcnt: , s3_txcnt:
s1_rxcnt: , s2_txcnt: , s3_txcnt:
s1_rxcnt: , s2_txcnt: , s3_txcnt:
s1_rxcnt: , s2_txcnt: , s3_txcnt:
s1_rxcnt: , s2_txcnt: , s3_txcnt:
s1_rxcnt: , s2_txcnt: , s3_txcnt:
s1_rxcnt: , s2_txcnt: , s3_txcnt:

Visual C#官方例子教程

https://docs.microsoft.com/zh-cn/visualstudio/ide/tutorial-3-create-a-matching-game?view=vs-2015

上一篇:黄聪:bootstrap的模态框modal插件在苹果iOS Safari下光标偏离问题解决方案


下一篇:python函数基础以及函数参数简解