.Net网络通讯编程[利用Socket实现字串、文件、序列化对象传输]--使用封装的网络服务2
直接使用Socket做客户端,采用Udp
客户端代码:
示范代码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(); } } }
服务端代码:
示范代码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做客户端和服务端,简化代码:
客户端代码:
示范代码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(); } } }
服务端代码:
示范代码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); } } }
posted on 2012-03-28 09:42 jiangtongcn 阅读(398) 评论(0) 编辑 收藏 举报