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

推荐订阅源

月光博客
月光博客
WordPress大学
WordPress大学
博客园 - 三生石上(FineUI控件)
H
Help Net Security
小众软件
小众软件
The Cloudflare Blog
人人都是产品经理
人人都是产品经理
Apple Machine Learning Research
Apple Machine Learning Research
S
SegmentFault 最新的问题
Last Week in AI
Last Week in AI
爱范儿
爱范儿
量子位
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
博客园 - 【当耐特】
V
Visual Studio Blog
大猫的无限游戏
大猫的无限游戏
博客园_首页
Jina AI
Jina AI
D
Docker
博客园 - 司徒正美
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Security Blog
Microsoft Security 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 server 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 client
星星之火 · 2004-06-19 · via 博客园 - 星星之火

和我的UdpEchoServer配合使用,在本机上试验用。设定接收的时间限制是3秒。

/*
 * Created by SharpDevelop.
 * User: xuhx
 * Date: 2004-6-19
 * Time: 14:21
 *
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

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

namespace DefaultNamespace
{
 /// <summary>
 /// Description of Class1. 
 /// </summary>
 public class UdpEchoClient
 {
  private static readonly int BUF_SIZE=2048;
  
  public static void Main(){
   Socket clientSocket=new Socket(AddressFamily.InterNetwork,
                                  SocketType.Dgram,
                                  ProtocolType.Udp
                                  );
   clientSocket.SetSocketOption(SocketOptionLevel.Socket,
                                 SocketOptionName.ReceiveTimeout,
                                 3000
                                 );
   EndPoint serverEndPoint=new IPEndPoint(IPAddress.Parse("127.0.0.1"),
                                          8004
                                          );
   Console.WriteLine("echo client start");
   while(true){
    string input=Console.ReadLine();
    if("exit"==input.ToLower()){
     clientSocket.Close();
     Console.WriteLine("echo client stop.");
     break;
    }
    byte[] buf=Encoding.ASCII.GetBytes(input);
    clientSocket.SendTo(buf,SocketFlags.None,serverEndPoint);
    buf=new byte[BUF_SIZE];
    EndPoint remoteEndPoint=new IPEndPoint(IPAddress.Any,0);
    try{
     int receive=clientSocket.ReceiveFrom(buf,ref remoteEndPoint);
     string strReceive=Encoding.ASCII.GetString(buf,0,receive);
     Console.WriteLine("echoed from server: {0} message:{1}",remoteEndPoint,strReceive);
    }catch(SocketException e){
     Console.WriteLine(e.Message);
    }
   }//while
  }
 }
}