惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
Cloudbric
Cloudbric
P
Palo Alto Networks Blog
T
Threatpost
T
Tor Project blog
T
Tenable Blog
AWS News Blog
AWS News Blog
Project Zero
Project Zero
L
LangChain Blog
Cyberwarzone
Cyberwarzone
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
C
CERT Recently Published Vulnerability Notes
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Latest
Security Latest
云风的 BLOG
云风的 BLOG
I
Intezer
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
V
Vulnerabilities – Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
MongoDB | Blog
MongoDB | Blog
aimingoo的专栏
aimingoo的专栏
K
Kaspersky official blog
Jina AI
Jina AI
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
D
DataBreaches.Net
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Secure Thoughts
TaoSecurity Blog
TaoSecurity Blog
P
Privacy & Cybersecurity Law Blog
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
IT之家
IT之家
Forbes - Security
Forbes - Security
The Hacker News
The Hacker News
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
Y
Y Combinator Blog

博客园 - 星星之火

Writing own regular expression parser How Regexes Work Content Based Routing Web Services in .NET and J2EE Poly-Engine Crypt String 绝不因寂寞而爱上别人 比尔·盖茨给青年一代的11点忠告 在.Net环境下用C#操纵活动目录 - 星星之火 - 博客园 谁能简单说一下活动目录是什么东西? 广播,多播(二)(Broadcasting, Multicasting) 两毛钱爽一把 可以建立一个Udp Server,接收发往本机所有端口的数据包吗? 广播,多播(一)(Broadcasting, Multicasting) C#异步网络编程 应该由国家建立非法网站数据库 use Helper Classes to simplify you network programming a udp echo client xml digital signature when udp goes bad and how to solve it(C#) when tcp goes bad, and how to solve it
a udp echo server
星星之火 · 2004-06-17 · via 博客园 - 星星之火

一个简单的udp echo server程序:
(1)没有测试是否正确,还没有编写udp echo client程序
(2)接收包的buffer大小是固定的,如果包的大小超过buffer的话,会产生异常,完美的udp程序应该捕获异常,调整buffer大小,通知重发等。
(3)不知道结束工作线程的方式是否合适,还有没有更好的方法。

/*
 * Created by SharpDevelop.
 * User: xuhx
 * Date: 2004-6-17
 * Time: 22:30
  */

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace Echo
{
 /// <summary>
 /// this is a udp echo server. i have not test it because i does not
 /// write a udp echo client. It still expects my further testing.
 /// </summary>
 public class UdpEchoServer
 {
  private static Socket serverSocket;
  private static Thread serverThread;
  
  private static readonly int BUF_SIZE=2048;
  
  public static void Main(){
   IPEndPoint serverEndPoint=new IPEndPoint(IPAddress.Any,8004);
   serverSocket=new Socket(AddressFamily.InterNetwork,
                           SocketType.Dgram,
                           ProtocolType.Udp);
   serverSocket.Bind(serverEndPoint);
   serverThread=new Thread(new ThreadStart(EchoThreadWorkMethod));
   serverThread.Start();
   
   Console.WriteLine("the echo server has run, press \"exit\" to terminate the run");
   
   while(true){
    string input=Console.ReadLine();
    if ( "exit"==input ) {
     if (serverThread.IsAlive){
      serverThread.Abort();
      Console.WriteLine("thread was terminated");
     }
     Console.WriteLine("server stop");
     serverSocket.Close();
     break;
    }
   }
  }
  
  private static void EchoThreadWorkMethod(){
   while(true){
    byte[] buf=new byte[BUF_SIZE];
    EndPoint remoteEndPoint=new IPEndPoint(IPAddress.Any,0);
    int receive=serverSocket.ReceiveFrom(buf,SocketFlags.None,ref remoteEndPoint);
    Console.WriteLine("receive from: [{0}]  message is: {1}",
                      remoteEndPoint,
                      Encoding.ASCII.GetString(buf,0,receive));
    serverSocket.SendTo(buf,receive,SocketFlags.None,remoteEndPoint);
   }
  }
 }
}