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 = 0;
        static int s2_txcnt = 0;
        static int s3_txcnt = 0;
        static void Main(string[] args)
        {
            s1 = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
            s1.Bind(new IPEndPoint(IPAddress.IPv6Any, 2020));

            s2 = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
            s2.Bind(new IPEndPoint(IPAddress.IPv6Loopback, 3000));

            s3 = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
            s3.Bind(new IPEndPoint(IPAddress.IPv6Loopback, 3001));

            Thread t = new Thread(ReciveMsg);//开启接收消息线程
            t.Start();
        }

        static void ReciveMsg()
        {
            byte[] buffer = new byte[1024];
            int length;
            while (true)
            {
                length = s1.Receive(buffer);
                s1_rxcnt++;

                s2.SendTo(buffer, new IPEndPoint(IPAddress.IPv6Loopback, 3000));
                s2_txcnt++;

                s3.SendTo(buffer, new IPEndPoint(IPAddress.IPv6Loopback, 3001));
                s3_txcnt++;

                Console.WriteLine("s1_rxcnt: " + s1_rxcnt.ToString("D8") +
                                    ", s2_txcnt: " + s2_txcnt.ToString("D8") +
                                        ", s3_txcnt: " + s3_txcnt.ToString("D8"));
            }
        }
    }
}
View Code

 

输出样式:

s1_rxcnt: 00000001, s2_txcnt: 00000001, s3_txcnt: 00000001
s1_rxcnt: 00000002, s2_txcnt: 00000002, s3_txcnt: 00000002
s1_rxcnt: 00000003, s2_txcnt: 00000003, s3_txcnt: 00000003
s1_rxcnt: 00000004, s2_txcnt: 00000004, s3_txcnt: 00000004
s1_rxcnt: 00000005, s2_txcnt: 00000005, s3_txcnt: 00000005
s1_rxcnt: 00000006, s2_txcnt: 00000006, s3_txcnt: 00000006
s1_rxcnt: 00000007, s2_txcnt: 00000007, s3_txcnt: 00000007
s1_rxcnt: 00000008, s2_txcnt: 00000008, s3_txcnt: 00000008
s1_rxcnt: 00000009, s2_txcnt: 00000009, s3_txcnt: 00000009
s1_rxcnt: 00000010, s2_txcnt: 00000010, s3_txcnt: 00000010
s1_rxcnt: 00000011, s2_txcnt: 00000011, s3_txcnt: 00000011
s1_rxcnt: 00000012, s2_txcnt: 00000012, s3_txcnt: 00000012
s1_rxcnt: 00000013, s2_txcnt: 00000013, s3_txcnt: 00000013
s1_rxcnt: 00000014, s2_txcnt: 00000014, s3_txcnt: 00000014
s1_rxcnt: 00000015, s2_txcnt: 00000015, s3_txcnt: 00000015
s1_rxcnt: 00000016, s2_txcnt: 00000016, s3_txcnt: 00000016
s1_rxcnt: 00000017, s2_txcnt: 00000017, s3_txcnt: 00000017
View Code

 

Visual C#官方例子教程

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

 

posted on 2019-06-17 21:35  yanhc  阅读(602)  评论(0编辑  收藏  举报

导航