直接使用Socket做客户端,采用Udp
客户端代码:
![](https://i-blog.csdnimg.cn/blog_migrate/0be121fa5b8988fbabbbc526af3b0fc0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
示范代码using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; using TcpLabCommon; using System.Net; namespace UdpLabClient1 { class Program { static void Main(string[] args) { Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); EndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6000); //socket.Bind(endPoint); NetPacketService packetSvr = new NetPacketUdpService(socket, endPoint); for (int i = 0; i < 10; i++) { packetSvr.SendObject(new Book { Title = "Java编程思想_" + i, Price = i + 1 }); packetSvr.SendText("jiang_" + i); } packetSvr.SendFile(@"E:\简单工厂参考.txt"); Console.ReadLine(); } } }
服务端代码:
![](https://i-blog.csdnimg.cn/blog_migrate/0be121fa5b8988fbabbbc526af3b0fc0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
示范代码using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; using System.Net; using TcpLabCommon; using System.IO; namespace UdpLabSvr1 { class Program { static void Main(string[] args) { Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"),6000); serverSocket.Bind(endPoint); NetPacketService packetSvr = new NetPacketUdpService(serverSocket,endPoint); do { NetPacket packet = packetSvr.ReceivePacket(); switch (packet.PacketHead.PType) { case PacketType.STRING: Console.WriteLine(Convert.ToString(packet.Data)); break; case PacketType.BINARY: NetFile f = packetSvr.ParseFile((Byte[])packet.Data); Console.WriteLine("成功接收文件:" + f.FileName); FileStream fs = new FileStream(f.FileName, FileMode.Create); fs.Write(f.Content, 0, f.Content.Length); fs.Flush(); fs.Close(); break; case PacketType.COMPLEX: Book book = packet.Data as Book; Console.WriteLine(book.Title + "\t" + book.Price); break; default: break; } } while (true); } } }
//----------------------------------------------------------------------------------------------------------------------------------------
采用UdpClient做客户端和服务端,简化代码:
客户端代码:
![](https://i-blog.csdnimg.cn/blog_migrate/0be121fa5b8988fbabbbc526af3b0fc0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
示范代码using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; using System.Net; using TcpLabCommon; namespace UdpLabClient2 { class Program { static void Main(string[] args) { //Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6000); //socket.Bind(endPoint); UdpClient udpClient = new UdpClient(); NetPacketService packetSvr = new NetPacketUdpService(udpClient.Client, endPoint); for (int i = 0; i < 10; i++) { packetSvr.SendObject(new Book { Title = "Java编程思想_" + i, Price = i + 1 }); packetSvr.SendText("jiang_" + i); } packetSvr.SendFile(@"E:\简单工厂参考.txt"); Console.ReadLine(); } } }
服务端代码:
![](https://i-blog.csdnimg.cn/blog_migrate/0be121fa5b8988fbabbbc526af3b0fc0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
示范代码using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; using System.Net; using TcpLabCommon; using System.IO; namespace UdpLabSvr2 { class Program { static void Main(string[] args) { //Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6000); //serverSocket.Bind(endPoint); UdpClient udpClient = new UdpClient(endPoint); NetPacketService packetSvr = new NetPacketUdpService(udpClient.Client, endPoint); do { NetPacket packet = packetSvr.ReceivePacket(); switch (packet.PacketHead.PType) { case PacketType.STRING: Console.WriteLine(Convert.ToString(packet.Data)); break; case PacketType.BINARY: NetFile f = packetSvr.ParseFile((Byte[])packet.Data); Console.WriteLine("成功接收文件:" + f.FileName); FileStream fs = new FileStream(f.FileName, FileMode.Create); fs.Write(f.Content, 0, f.Content.Length); fs.Flush(); fs.Close(); break; case PacketType.COMPLEX: Book book = packet.Data as Book; Console.WriteLine(book.Title + "\t" + book.Price); break; default: break; } } while (true); } } }