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

推荐订阅源

A
About on SuperTechFans
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 司徒正美
宝玉的分享
宝玉的分享
美团技术团队
量子位
The Cloudflare Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
IT之家
IT之家
爱范儿
爱范儿
J
Java Code Geeks
博客园 - Franky
Last Week in AI
Last Week in AI
B
Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
InfoQ
GbyAI
GbyAI
Recent Announcements
Recent Announcements
小众软件
小众软件
H
Help Net Security
Microsoft Azure Blog
Microsoft Azure Blog
MyScale Blog
MyScale 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
  }
 }
}