Socket通讯之UDP
2012-02-28 10:04
190 查看
发送消息的类:
接收消息的类
测试代码:打开监听端:
发送字符串
发送文件
/// <summary> /// 发送消息 /// </summary> public class UdpSendMessage { System.Net.Sockets.UdpClient udpClient = null; public static int BufferSize = 1024; public UdpSendMessage() { udpClient = new System.Net.Sockets.UdpClient(); } /// <summary> /// 发送Socket消息 /// </summary> /// <param name="ip">远程IP</param> /// <param name="port">远程端口号</param> /// <param name="datagram">数据包</param> private void Send(string ip, int port, byte[] datagram) { // System.Net.IPEndPoint endPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Parse(ip), port); if (datagram.Length > BufferSize) { for (int i = 0, srcOffset = 0; i < datagram.Length / BufferSize + 1; i++, srcOffset++) { if (i == datagram.Length / BufferSize) { byte[] dst = new byte[datagram.Length - BufferSize * i]; Buffer.BlockCopy(datagram, srcOffset * BufferSize, dst, 0, datagram.Length - BufferSize * i); udpClient.BeginSend(dst, dst.Length, endPoint, new AsyncCallback(SendCallback), true); } else { byte[] dst = new byte[BufferSize]; Buffer.BlockCopy(datagram, srcOffset * BufferSize, dst, 0, BufferSize); udpClient.BeginSend(dst, dst.Length, endPoint, new AsyncCallback(SendCallback), false); } System.Threading.Thread.Sleep(1); } } else { udpClient.BeginSend(datagram, datagram.Length, endPoint, new AsyncCallback(SendCallback), true); } } private void SendCallback(IAsyncResult asynResult) { try { if ((bool)asynResult.AsyncState) { udpClient.EndSend(asynResult); } } catch (Exception ex) { throw ex; } } }
接收消息的类
/// <summary> /// 接收消息 /// </summary> public class UdpResiveMessage { System.Net.Sockets.UdpClient udpClient = null; StringBuilder sbResvieMsg = new StringBuilder(); /// <summary> /// 打开监听端 /// </summary> /// <param name="port">监听端口号</param> public void BeginListen(int port) { udpClient = new System.Net.Sockets.UdpClient(port, System.Net.Sockets.AddressFamily.InterNetwork); udpClient.BeginReceive(new AsyncCallback(ReceiveCallback), null); } private void ReceiveCallback(IAsyncResult asynResult) { try { IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); byte[] buffer = udpClient.EndReceive(asynResult, ref remoteEP); string ss = System.Text.Encoding.Default.GetString(buffer); if (!string.IsNullOrWhiteSpace(ss)) { sbResvieMsg.Append(ss); } if (buffer.Length < 1024) { MessageBox.Show(sbResvieMsg.ToString()); } udpClient.BeginReceive(new AsyncCallback(ReceiveCallback), null); } catch (Exception ex) { throw ex; } } }
测试代码:打开监听端:
UdpResiveMessage udpListen = new UdpResiveMessage(); udpListen.BeginListen(10003);
发送字符串
UdpSendMessage udpSend = new UdpSendMessage(); udpSend.Send("127.0.0.1", 10003, System.Text.Encoding.Default.GetBytes(this.textBox1.Text.Trim()));
发送文件
/// <summary> /// 将文件转换为字节流 /// </summary> /// <param name="sFilePath"></param> /// <returns></returns> public byte[] GetFileByte(string sFilePath) { try { if (!System.IO.File.Exists(sFilePath)) { return null; } else { FileStream fs = new FileStream(sFilePath, FileMode.Open, FileAccess.Read); byte[] buffur = new byte[fs.Length]; fs.Read(buffur, 0, (int)fs.Length); fs.Close(); return buffur; } } catch //(Exception ex) { return null; } } /// <summary> /// 发送文件 /// </summary> private void SendFile() { UdpSendMessage udpSend = new UdpSendMessage(); udpSend.Send("127.0.0.1", 10003, GetFileByte("d:\\BookOrder.pdf")); }
相关文章推荐
- 基于UDP的网络通讯(socket)
- Socket编程 (异步通讯) (Tcp,Udp)Part2
- Socket的TCP、UDP通讯(实现功能:文字发送,窗口震动,大文件传输,语音发送,语音通话、直接发送全屏截图)
- python实现socket通讯(UDP)
- 基于udp socket 通讯
- C# udp socket通讯
- python实现socket通讯(UDP)
- Android 开发 Socket 基于UDP通讯
- 闲来无事,写个基于UDP协议的Socket通讯Demo
- python实现socket通讯(UDP)
- SOCKET UDP 通讯过程中 10022 和 10014 偶尔出现问题
- Socket编程 (异步通讯,解决Udp丢包)
- oc下实现局域网udp广播通讯使用开源框架GCDAsyncUdpSocket
- JAVA与网络开发(TCP:Socket、ServerSocket;UDP:DatagramSocket、DatagramPacket;多线程的C/S通讯、RMI开发概述)
- java 实现socket基于UDP的双机通讯
- Python 实现 socket 通讯 (TCP/UDP)
- Socket编程 (异步通讯) (Tcp,Udp) - Part2
- socket通讯,TCP,UDP,HTTP的区别
- IP地址,TCP/UDP通讯协议概述,Socket,UDP传输,多线程UDP聊天应用
- Socket编程 (异步通讯,解决Udp丢包) - Part4