zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Odometer使用JavaScript和CSS制作数字滑动效果
    50个必备的实用jQuery代码段
    优化移动体验的HTML5技巧
    20 个非常棒的jQuery内容滑动插件
    最全的js正则表达式用法大全
    大神都未必解决的了简单问题,关于文字左右两端对齐。
    盘点20款表现出众的HTML5游戏
    蓝桥杯 格子问题:输出同行同列同对角线格子的位置
    与曹学长的一番谈话
    21天学习活动之——我的讲课新体验
  • 原文地址:https://www.cnblogs.com/yanhc/p/11042318.html
Copyright © 2011-2022 走看看