C#的UDP服务器
最新优化版本
/*
http://www.cnblogs.com/zengqinglei/archive/2013/04/27/3046119.html
*/
using System;
using System.Text;
#region 命名空间
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Media;
#endregion namespace SocketServerConsole
{
class Program
{
#region 控制台主函数
/// <summary>
/// 控制台主函数
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
UdpServer(new IPEndPoint(0, 9167));
}
#endregion #region Udp连接方式
/// <summary>
/// Udp连接方式
/// </summary>
/// <param name="serverIP"></param>
public static void UdpServer(IPEndPoint serverIP)
{
SoundPlayer sp = new SoundPlayer();
bool thread_flag = true;
Console.WriteLine("UDP服务器开始监听" + serverIP.Port + "端口");
Socket udpServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
udpServer.Bind(serverIP);
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)ipep;
new Thread(() =>
{
while (thread_flag)
{
byte[] data = new byte[1024];
int length = 0;
try
{
length = udpServer.ReceiveFrom(data, ref Remote);//接受来自服务器的数据
}
catch (Exception ex)
{
Console.WriteLine(string.Format("出现异常:{0}", ex.Message));
break;
}
string datetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
string message = Encoding.UTF8.GetString(data, 0, length);
string ipport = (Remote as IPEndPoint).Address.ToString() + ":" + (Remote as IPEndPoint).Port.ToString();
Console.WriteLine(string.Format("{0} 收到來自{1}的消息:{2}", datetime, ipport, message));
sp.SoundLocation = Thread.GetDomain().BaseDirectory + message;
try
{
sp.PlayLooping();
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Error: {0}, Path={1}", ex.Message, sp.SoundLocation));
} }
udpServer.Close();
}).Start();
Console.WriteLine("\n\n按[F4]键退出。");
ConsoleKey key;
while (true)
{
key = Console.ReadKey(true).Key;
if (key == ConsoleKey.F4)
{
Console.WriteLine("end waiting for udp data.");
thread_flag = false;
break;
}
}
}
#endregion
}
}
C#的UDP服务器的更多相关文章
- windows下UDP服务器和客户端的实现
UDP是面向非连接的协议,因此在实现UDP服务器时,服务器不用总是处于监听状态.可以直接收发数据. 服务器端 1.初始化 WASStartup ( ... ) 2.创建Socket s ...
- Linux系统编程(37)—— socket编程之UDP服务器与客户端
典型的UDP客户端/服务器通讯过程: 编写UDP Client程序的步骤 1.初始化sockaddr_in结构的变量,并赋值.这里使用"8888"作为连接的服务程序的端口,从命令行 ...
- 【实验 1-2】编写一个简单的 UDP 服务器和 UDPP 客户端程序。程序均为控制台程序窗口。
1.服务器 #include<winsock2.h> //包含头文件#include<stdio.h>#include<windows.h>#pragma comm ...
- <摘录>详谈高性能UDP服务器的开发
上一篇文章我详细介绍了如何开发一款高性能的TCP服务器的网络传输层.本章我将谈谈如何开发一个高性能的UDP服务器的网络层.UDP服务器的网络层开 发相对与TCP服务器来说要容易和简单的多,UDP服务器 ...
- 【卷二】网络三—UDP服务器与客户端
这是另一个类型的服务器/客户端,无连接的 UDP: (User Datagram Protocol) 用户数据报协议 参考: P58~P60 UDP 时间戳服务器 [时间戳 就是ctime()显示的内 ...
- 安卓作为udp服务器,PC作为客户端,仅监听
安卓客户端作为udp服务器,监听其他客户端的数据,测试已成功 本次实验所用数据: 安卓作为服务器: 端口:8888 IP:192.168.1.104 电脑作为客户端: 端口:50 ...
- udp服务器监听(多线程)
项目一:udp1111 监听的有三个文件分别为: guiHello.java 有关界面和事件响应功能 UdpFunc.java是udp类的封装:发送和接收 udpServer.java是入口函数,无实 ...
- udp 服务器界面监听
今天在做项目的时候,发现不同子网下是不能做UDP通信的,不知道是不是这样呢 遇到一个错误:eclipse遇到报错: The type JPEGImageEncoder is not accessibl ...
- node.js中通过dgram数据报模块创建UDP服务器和客户端
node.js中 dgram 模块提供了udp数据包的socket实现,可以方便的创建udp服务器和客户端. 一.创建UDP服务器和客户端 服务端: const dgram = require('dg ...
随机推荐
- MySQL性能优化总结(转)https://yq.aliyun.com/articles/24249
摘要: 一.MySQL的主要适用场景 1.Web网站系统 2.日志记录系统 3.数据仓库系统 4.嵌入式系统 二.MySQL架构图: 三.MySQL存储引擎概述 1)MyISAM存储引擎 MyIS ...
- CAD的API们
AutoCAD有4种API,.net,lisp,activex和ObjectARX(C++).它们都是用来给cad写插件什么的,依赖cad运行. 另有一个RealDWG SDK,这是用来读写dwg或d ...
- 使用 Windows AIK 创建自定的客户端系统WIM文件
Windows 7/2008 的AIK 3.0下载页面:地址链接 1.8G [3.1补充包为1.4G] 安装3.0后,升级为3.1方法: xcopy E:\ "C:\Program File ...
- hbase shell基础和常用命令详解(转)
HBase shell的基本用法 hbase提供了一个shell的终端给用户交互.使用命令hbase shell进入命令界面.通过执行 help可以看到命令的帮助信息. 以网上的一个学生成绩表的例子来 ...
- Java 大数运算
import java.util.*; import java.math.*; public class Main{ public static void main(String args[]){ S ...
- mysql数据库优化小结
一.常见数据库的优化操作 1.表的设计要符合三范式. 2.添加适当的索引,索引对查询速度影响很大,必须添加索引.主键索引,唯一索引,普通索引,全文索引 3.添加适当存储过程,触发器,事务等. 4.读写 ...
- 一、Owin Identity的使用
参照http://www.cnblogs.com/r01cn/p/5177708.html教程. 注意点: 1.Nuget包分别下载Microsoft.AspNet.Identity(Owin必须), ...
- 【转】windows7 修改环境变量 和 用不用重启电脑的讨论
原文:http://www.cnblogs.com/zhenmingliu/archive/2013/02/21/2921396.html 先到我的电脑>属性>高级>环境变量 ...
- 22. Surrounded Regions
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- 9. Linux远程登录
1. 检查网络是否通畅 C:\Users\cfm>ping 192.168.232.131 正在 Ping 192.168.232.131 具有 32 字节的数据:来自 192.168.232. ...