C# SocketUdpServer
public interface ISocketUdpServer
{
void Start();
void Stop();
int SendData(byte[] data, IPEndPoint remoteEndPoint); event ReceiveDataHandler ReceivedDataEvent;
event ErrorHandler ErrorEvent;
} public delegate void ReceiveDataHandler(SocketState state); public delegate void OnlineChangeHandler(int onlines, EndPoint client); public delegate void ErrorHandler(string error, EndPoint client); public class SocketUdpServer : ISocketUdpServer
{
private readonly Socket _udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
private bool _isListening; public SocketUdpServer(IPEndPoint localPoint)
{
_udpSocket.ReceiveBufferSize = * ;
_udpSocket.Bind(localPoint);
} public Socket GetUdpSocket()
{
return _udpSocket;
} public void Start()
{
_isListening = true;
BeginReceive();
} public void Stop()
{
_isListening = false;
} public int SendData(byte[] data, IPEndPoint remoteEndPoint)
{
return _udpSocket.SendTo(data, remoteEndPoint);
} public event ReceiveDataHandler ReceivedDataEvent; public event ErrorHandler ErrorEvent; private void BeginReceive()
{
if (_isListening)
{
SocketState state = new SocketState { Self = _udpSocket };
_udpSocket.BeginReceiveFrom(state.Buffer, , state.Buffer.Length, SocketFlags.None,
ref state.RemotePoint, ReceiveCallback, state);
}
} private void ReceiveCallback(IAsyncResult ar)
{
var state = ar.AsyncState as SocketState;
try
{
if (state != null)
{
int receiveLen = state.Self.EndReceiveFrom(ar, ref state.RemotePoint);
if (receiveLen > )
{
byte[] receivedData = new byte[receiveLen];
Array.Copy(state.Buffer, , receivedData, , receiveLen);
state.Buffer = receivedData;
state.ReceivedTime = DateTime.Now;
ReceivedDataEvent?.Invoke(state);
}
}
}
catch (Exception error)
{
ErrorEvent?.Invoke(error.Message, state?.RemotePoint);
}
finally
{
if (state != null) BeginReceive();
}
} } public class SocketState
{
public byte[] Buffer = new byte[ * ];
public Socket Self;
public EndPoint RemotePoint = new IPEndPoint(IPAddress.Any, );
public DateTime ReceivedTime { get; set; }
}
C# SocketUdpServer的更多相关文章
- 用c#写的一个局域网聊天客户端 类似小飞鸽
用c#写的一个局域网聊天客户端 类似小飞鸽 摘自: http://www.cnblogs.com/yyl8781697/archive/2012/12/07/csharp-socket-udp.htm ...
- Unity Socket UDP
using System.Collections; using System.Collections.Generic; using System.Net.Sockets; using System.N ...
- 8.9.网络编程_Socket 远程调用机制
1.网络编程 1.1.网络编程概述: 通过通信线路(有线或无线)可以把不同地理位置且相互独立的计算机连同其外部设备连接起来,组成计算机网络.在操作系统.网络管理软件及网络 通信协议的管理和协调下,可以 ...
随机推荐
- 接口IDisposable的用法
C#的每一个类型都代表一种资源,而资源又分为两类: 托管资源 由CLR管理分配和释放的资源,即从CLR里new出来的对象. 非托管资源 不受CLR管理的对象,如Windows内核对象,或者文件.数 ...
- C#基础笔记(第十八天)
1.HTMLHyper Text Markup Language 超文本标记语言在HTML当中存在着大量的标签,我们用HTML提供的标签,将要显示在网页中的内容包含起来.就构成了我们的网页. CSS: ...
- C语言的第零次作业
C语言--第0次作业 Q1:对于网络专业的了解 一开始我对网络工程这个专业并不是很了解,在报志愿之前,我完全没想过自己会进这个专业,但是经过了一个暑假的时间,我慢慢地开始了解这个学科,并开始对这个专业 ...
- 【Oracle 12c】CUUG OCP认证071考试原题解析(32)
32.choose the best answer View the Exhibit and examine the data in EMP and DEPT tables. In the DEPT ...
- LOJ#162. 快速幂 2(分块)
题面 传送门 题解 orzljz 我们分块,设\(s=\sqrt{p}+1\),那么\(x^a\)可以拆成\((x^s)^{a/s}\)和\(x^{a\bmod s}\),\(O(s)\)预处理,\( ...
- Java多线程学习(一)
在Java多线程应用中,队列的使用率很高,多数生产消费模型的首选数据结构就是队列.Java提供的线程安全的Queue可以分为阻塞队列和非阻塞队列,其中阻塞队列的典型例子是BlockingQueue,非 ...
- 【flask macro】No caller defined
https://segmentfault.com/q/1010000005352059/a-1020000005352912 先码着 有时间了再换成caller() 先用老方法吧...
- [ActionScript 3.0] 幻灯片效果实例
package com.fylibs.components.effects { import com.fylibs.utils.LoaderQueues; import com.tweener.tra ...
- Linux nl --让输出的文件内容自动加上行号
nl命令在linux系统中用来计算文件中行号.nl 可以将输出的文件内容自动的加上行号!其默认的结果与 cat -n 有点不太一样, nl 可以将行号做比较多的显示设计,包括位数与是否自动补齐 0 等 ...
- 总结day7 ---- 文件操作,读,写,追加,以及相关方法
内容大纲 一:文件的基本操作, >常见问题 >encoding >绝对路径和相对路径的 二:文件的读写追加相关操作 >读(r, r+ ,rb,r+b) >写(w,w+,w ...