.Net网络通讯编程[利用Socket实现字串、文件、序列化对象传输]--使用封装的网络服务1
直接使用Socket做服务器端和客户端,采用Tcp协议:
客户端代码:
示范代码using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; using TcpLabCommon; using System.Runtime.Serialization.Json; namespace TcpLabClient1 { class Program { static void Main(string[] args) { Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect("127.0.0.1", 6000); NetworkStream netStream = new NetworkStream(socket); NetPacketService packetSvr = new NetPacketTcpService(netStream); for (int i = 0; i < 10; i++) { packetSvr.SendObject(new Book { Title = "Java编程思想_" + i, Price = i + 1 }); packetSvr.SendText("jiang_" + i); } packetSvr.SendFile(@"E:\WorkSpace\VCLabProjects\TcpLabSpace.zip"); Console.ReadLine(); } } }
服务器端代码:
示范代码using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Net.Sockets; using System.Net; using TcpLabCommon; using System.IO; using System.Text; namespace TcpLabSvr1 { class Program { static void Main() { Socket serverSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"),6000); serverSocket.Bind(endPoint); serverSocket.Listen(3); Socket clientSocket = serverSocket.Accept(); NetworkStream netStream = new NetworkStream(clientSocket); NetPacketService packetSvr = new NetPacketTcpService(netStream); 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); } } }
//-------------------------------------------------------------------------------------------------------------------------------
使用TcpListener做服务器端、TcpClient做客户端:
客户端代码:
示范代码using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; using TcpLabCommon; namespace TcpLabClient2 { /// <summary> /// 客户端使用TcpClient /// </summary> class Program { static void Main(string[] args) { TcpClient client = new TcpClient("127.0.0.1",6000); NetworkStream netStream = client.GetStream(); NetPacketService packetSvr = new NetPacketTcpService(netStream); for (int i = 0; i < 10; i++) { packetSvr.SendObject(new Book { Title = "Java编程思想_" + i, Price = i + 1 }); packetSvr.SendText("jiang_" + i); } packetSvr.SendText("测试一下1"); packetSvr.SendText("测试一下2"); packetSvr.SendText("测试一下3"); packetSvr.SendFile(@"E:\简单工厂参考.txt"); Console.ReadLine(); } } }
服务端代码:
示范代码using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Sockets; using TcpLabCommon; using System.IO; namespace TcpLabSvr2 { /// <summary> /// 服务器端使用TcpListener /// </summary> class Program { static void Main(string[] args) { TcpListener svrListen = new TcpListener(IPAddress.Parse("127.0.0.1"),6000); svrListen.Start(3); TcpClient client = svrListen.AcceptTcpClient(); NetworkStream netStream = client.GetStream(); NetPacketService packetSvr = new NetPacketTcpService(netStream); do { NetPacket packet = packetSvr.ReceivePacket(); switch (packet.PacketHead.PType) { case PacketType.STRING: Console.WriteLine(packet.Data.ToString()); 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:24 jiangtongcn 阅读(457) 评论(0) 编辑 收藏 举报