socket中使用序列化传结构体
使用UDP方式,效果如图
:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using System.Threading; using System.Net; using System.IO; using System.Runtime.Serialization.Formatters.Binary; namespace UdpTest { public partial class Form1 : Form { UdpClient udpSend; UdpClient udpRecv; bool IsUdpcRecvStart = false; Thread thrRecv; [Serializable] struct Student { public string Name; public int Age; }; public Form1() { InitializeComponent(); } private void btnSend_Click(object sender, EventArgs e) { // 实名发送 IPEndPoint localIpep = new IPEndPoint( IPAddress.Parse("127.0.0.1"), 12345); // 本机IP,指定的端口号 udpSend = new UdpClient(localIpep); IPEndPoint remoteIpep = new IPEndPoint( IPAddress.Parse("127.0.0.1"), 11111); // 发送到的IP地址和端口号 //序列化 stu Student stu; stu.Name = "xiaoming"; stu.Age = 18; MemoryStream mStream = new MemoryStream(); BinaryFormatter bformatter = new BinaryFormatter(); //二进制序列化类 bformatter.Serialize(mStream, stu); //将消息类转换为内存流 mStream.Flush(); mStream.Position = 0; byte[] buffer = new byte[180]; while (mStream.Read(buffer, 0, buffer.Length) > 0) { udpSend.Send(buffer,buffer.Length,remoteIpep); //从内存中读取二进制流,并发送 } // byte[] sendbytes = Encoding.Unicode.GetBytes(txtSendMssg.Text.ToString()); // udpSend.Send(sendbytes, sendbytes.Length, remoteIpep); ShowMessage(txtSendMssg, "发送结构体:"+ stu.Name.ToString() + ":" + stu.Age); udpSend.Close(); } private void btnRecv_Click(object sender, EventArgs e) { if (!IsUdpcRecvStart) // 未监听的情况,开始监听 { IPEndPoint localIpep = new IPEndPoint( IPAddress.Parse("127.0.0.1"), 11111); // 本机IP和监听端口号 udpRecv = new UdpClient(localIpep); thrRecv = new Thread(ReceiveMessage); thrRecv.Start(); IsUdpcRecvStart = true; ShowMessage(txtRecvMssg, "UDP监听器已成功启动"); } else // 正在监听的情况,终止监听 { thrRecv.Abort(); // 必须先关闭这个线程,否则会异常 udpRecv.Close(); IsUdpcRecvStart = false; ShowMessage(txtRecvMssg, "UDP监听器已成功关闭"); } } private void ReceiveMessage(object obj) { byte[] buffer = new byte[200]; MemoryStream mStream = new MemoryStream(); mStream.Position = 0; IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Any, 0); while (true) { try { byte[] bytRecv = udpRecv.Receive(ref remoteIpep); mStream.Write(bytRecv, 0, bytRecv.Length); //将接收到的数据写入内存流 string message = Encoding.Unicode.GetString(bytRecv, 0, bytRecv.Length); //MessageBox.Show(message); //ShowMessage(txtRecvMssg,string.Format("{0}[{1}]", remoteIpep, message)); } catch (Exception ex) { ShowMessage(txtRecvMssg, ex.Message); break; } mStream.Flush(); mStream.Position = 0; BinaryFormatter bFormatter = new BinaryFormatter(); if (mStream.Capacity > 0) { Student msg = (Student)bFormatter.Deserialize(mStream);//将接收到的内存流反序列化为对象 ShowMessage(txtRecvMssg, "收到结构体msg:"+msg.Name.ToString()+":"+msg.Age.ToString()); } } } // 向TextBox中添加文本 delegate void ShowMessageDelegate(TextBox txtbox, string message); private void ShowMessage(TextBox txtbox, string message) { if (txtbox.InvokeRequired) { ShowMessageDelegate showMessageDelegate = ShowMessage; txtbox.Invoke(showMessageDelegate, new object[] { txtbox, message }); } else { txtbox.Text += message + "\r\n"; } } private void FormMain_FormClosing(object sender, FormClosingEventArgs e) { Environment.Exit(0); thrRecv.Abort(); } } }