用序列化,贴一段代码,去年写的
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using System.Text;
namespace SocketTest
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class frmMain : System.Windows.Forms.Form
{
// 版本
[Serializable]
private struct verHead
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] Version;
public byte Compressed;
public byte Encrypted;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public byte[] Reserved;
}
// 请求包头
[Serializable]
private struct reqHead
{
//public verHead Info;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] Version;
public byte Compressed;
public byte Encrypted;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public byte[] Reserved;
public short MainId;
public short AssisId;
public char Priority;
public int PacketLen;
public int RawLen;
}
// 应答包头
[Serializable]
private struct ansHead
{
public int Separator;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] Version;
public byte Compressed;
public byte Encrypted;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public byte[] Reserved;
//public verHead Info;
public int MainId;
public int AssisId;
public char Priority;
public int req;
public int PacketLen;
public int RawLen;
}
// 请求包体9021
[Serializable]
private struct reqBody9021
{
public short ReqId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
public string User;
public bool bDelMyself;
public bool bChangePwd;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
public string oldPwd;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
public string newPwd;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string Reserved;
}
// 应答包体9021
[Serializable]
private struct ansBody9021
{
public char bModify;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 58)]
public string errorMsg;
}
// 请求包9021
[Serializable]
private struct req9021
{
public reqHead req_head;
public reqBody9021 req_body;
}
const int MODIFYINFO_REQ = 9021;
private System.Windows.Forms.Panel plServerTop;
private System.Windows.Forms.Button btnListen;
private System.Windows.Forms.GroupBox gpbxServer;
private System.Windows.Forms.RichTextBox rtbxServer;
private System.Windows.Forms.TextBox tbxListenAddress;
private System.Windows.Forms.TextBox tbxListenPort;
private System.Windows.Forms.TextBox tbxServerPort;
private System.Windows.Forms.TextBox tbxServerAddress;
private System.Windows.Forms.Label lblListenAddress;
private System.Windows.Forms.Label lblListenPort;
private System.Windows.Forms.Label lblServerPort;
private System.Windows.Forms.Label lblServerAddress;
private System.Windows.Forms.Button btnConnect;
private System.Windows.Forms.Panel plClientTop;
private System.Windows.Forms.Panel plClientMessage;
private System.Windows.Forms.TextBox tbxMessage;
private System.Windows.Forms.Button btnSend;
private System.Windows.Forms.GroupBox gpbxClient;
private System.Windows.Forms.RichTextBox rtbxClient;
private TcpListener tcpListen;
private Thread thdListen;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public frmMain()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.plServerTop = new System.Windows.Forms.Panel();
this.btnListen = new System.Windows.Forms.Button();
this.tbxListenPort = new System.Windows.Forms.TextBox();
this.lblListenPort = new System.Windows.Forms.Label();
this.tbxListenAddress = new System.Windows.Forms.TextBox();
this.lblListenAddress = new System.Windows.Forms.Label();
this.gpbxServer = new System.Windows.Forms.GroupBox();
this.rtbxServer = new System.Windows.Forms.RichTextBox();
this.plClientTop = new System.Windows.Forms.Panel();
this.btnConnect = new System.Windows.Forms.Button();
this.tbxServerPort = new System.Windows.Forms.TextBox();
this.lblServerPort = new System.Windows.Forms.Label();
this.tbxServerAddress = new System.Windows.Forms.TextBox();
this.lblServerAddress = new System.Windows.Forms.Label();
this.plClientMessage = new System.Windows.Forms.Panel();
this.btnSend = new System.Windows.Forms.Button();
this.tbxMessage = new System.Windows.Forms.TextBox();
this.gpbxClient = new System.Windows.Forms.GroupBox();
this.rtbxClient = new System.Windows.Forms.RichTextBox();
this.plServerTop.SuspendLayout();
this.gpbxServer.SuspendLayout();
this.plClientTop.SuspendLayout();
this.plClientMessage.SuspendLayout();
this.gpbxClient.SuspendLayout();
this.SuspendLayout();
//
// plServerTop
//
this.plServerTop.Controls.AddRange(new System.Windows.Forms.Control[] {
this.btnListen,
this.tbxListenPort,
this.lblListenPort,
this.tbxListenAddress,
this.lblListenAddress});
this.plServerTop.Dock = System.Windows.Forms.DockStyle.Top;
this.plServerTop.Name = "plServerTop";
this.plServerTop.Size = new System.Drawing.Size(448, 32);
this.plServerTop.TabIndex = 0;
//
// btnListen
//
this.btnListen.Location = new System.Drawing.Point(363, 4);
this.btnListen.Name = "btnListen";
this.btnListen.TabIndex = 4;
this.btnListen.Text = "侦听";
this.btnListen.Click += new System.EventHandler(this.btnListen_Click);
//
// tbxListenPort
//
this.tbxListenPort.Location = new System.Drawing.Point(251, 5);
this.tbxListenPort.Name = "tbxListenPort";
this.tbxListenPort.TabIndex = 3;
this.tbxListenPort.Text = "7778";
//
// lblListenPort
//
this.lblListenPort.AutoSize = true;
this.lblListenPort.Location = new System.Drawing.Point(188, 8);
this.lblListenPort.Name = "lblListenPort";
this.lblListenPort.Size = new System.Drawing.Size(66, 14);
this.lblListenPort.TabIndex = 2;
this.lblListenPort.Text = "侦听端口:";
//
// tbxListenAddress
//
this.tbxListenAddress.Location = new System.Drawing.Point(72, 5);
this.tbxListenAddress.Name = "tbxListenAddress";
this.tbxListenAddress.TabIndex = 1;
this.tbxListenAddress.Text = "127.0.0.1";
//
// lblListenAddress
//
this.lblListenAddress.AutoSize = true;
this.lblListenAddress.Location = new System.Drawing.Point(8, 8);
this.lblListenAddress.Name = "lblListenAddress";
this.lblListenAddress.Size = new System.Drawing.Size(66, 14);
this.lblListenAddress.TabIndex = 0;
this.lblListenAddress.Text = "侦听地址:";
//
// gpbxServer
//
this.gpbxServer.Controls.AddRange(new System.Windows.Forms.Control[] {
this.rtbxServer});
this.gpbxServer.Dock = System.Windows.Forms.DockStyle.Top;
this.gpbxServer.Location = new System.Drawing.Point(0, 32);
this.gpbxServer.Name = "gpbxServer";
this.gpbxServer.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.gpbxServer.Size = new System.Drawing.Size(448, 144);
this.gpbxServer.TabIndex = 1;
this.gpbxServer.TabStop = false;
this.gpbxServer.Text = "服务端消息";
//
// rtbxServer
//
this.rtbxServer.Dock = System.Windows.Forms.DockStyle.Fill;
this.rtbxServer.Location = new System.Drawing.Point(3, 17);
this.rtbxServer.Name = "rtbxServer";
this.rtbxServer.Size = new System.Drawing.Size(442, 124);
this.rtbxServer.TabIndex = 0;
this.rtbxServer.Text = "";
//
// plClientTop
//
this.plClientTop.Controls.AddRange(new System.Windows.Forms.Control[] {
this.btnConnect,
this.tbxServerPort,
this.lblServerPort,
this.tbxServerAddress,
this.lblServerAddress});
this.plClientTop.Dock = System.Windows.Forms.DockStyle.Top;
this.plClientTop.Location = new System.Drawing.Point(0, 176);
this.plClientTop.Name = "plClientTop";
this.plClientTop.Size = new System.Drawing.Size(448, 32);
this.plClientTop.TabIndex = 2;
//
// btnConnect
//
this.btnConnect.Enabled = false;
this.btnConnect.Location = new System.Drawing.Point(363, 4);
this.btnConnect.Name = "btnConnect";
this.btnConnect.TabIndex = 4;
this.btnConnect.Text = "连接";
//
// tbxServerPort
//
this.tbxServerPort.Location = new System.Drawing.Point(251, 5);
this.tbxServerPort.Name = "tbxServerPort";
this.tbxServerPort.TabIndex = 3;
this.tbxServerPort.Text = "7778";
//
// lblServerPort
//
this.lblServerPort.AutoSize = true;
this.lblServerPort.Location = new System.Drawing.Point(206, 8);
this.lblServerPort.Name = "lblServerPort";
this.lblServerPort.Size = new System.Drawing.Size(42, 14);
this.lblServerPort.TabIndex = 2;
this.lblServerPort.Text = "端口:";
//
// tbxServerAddress
//
this.tbxServerAddress.Location = new System.Drawing.Point(83, 5);
this.tbxServerAddress.Name = "tbxServerAddress";
this.tbxServerAddress.TabIndex = 1;
this.tbxServerAddress.Text = "127.0.0.1";
//
// lblServerAddress
//
this.lblServerAddress.AutoSize = true;
this.lblServerAddress.Location = new System.Drawing.Point(8, 8);
this.lblServerAddress.Name = "lblServerAddress";
this.lblServerAddress.Size = new System.Drawing.Size(79, 14);
this.lblServerAddress.TabIndex = 0;
this.lblServerAddress.Text = "服务端地址:";
//
// plClientMessage
//
this.plClientMessage.Controls.AddRange(new System.Windows.Forms.Control[] {
this.btnSend,
this.tbxMessage});
this.plClientMessage.Dock = System.Windows.Forms.DockStyle.Top;
this.plClientMessage.Location = new System.Drawing.Point(0, 208);
this.plClientMessage.Name = "plClientMessage";
this.plClientMessage.Size = new System.Drawing.Size(448, 32);
this.plClientMessage.TabIndex = 3;
//
// btnSend
//
this.btnSend.Location = new System.Drawing.Point(363, 4);
this.btnSend.Name = "btnSend";
this.btnSend.TabIndex = 1;
this.btnSend.Text = "发送";
this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
//
// tbxMessage
//
this.tbxMessage.Location = new System.Drawing.Point(8, 5);
this.tbxMessage.Name = "tbxMessage";
this.tbxMessage.Size = new System.Drawing.Size(344, 21);
this.tbxMessage.TabIndex = 0;
this.tbxMessage.Text = "";
//
// gpbxClient
//
this.gpbxClient.Controls.AddRange(new System.Windows.Forms.Control[] {
this.rtbxClient});
this.gpbxClient.Dock = System.Windows.Forms.DockStyle.Fill;
this.gpbxClient.Location = new System.Drawing.Point(0, 240);
this.gpbxClient.Name = "gpbxClient";
this.gpbxClient.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.gpbxClient.Size = new System.Drawing.Size(448, 157);
this.gpbxClient.TabIndex = 4;
this.gpbxClient.TabStop = false;
this.gpbxClient.Text = "客户端消息";
//
// rtbxClient
//
this.rtbxClient.Dock = System.Windows.Forms.DockStyle.Fill;
this.rtbxClient.Location = new System.Drawing.Point(3, 17);
this.rtbxClient.Name = "rtbxClient";
this.rtbxClient.Size = new System.Drawing.Size(442, 137);
this.rtbxClient.TabIndex = 0;
this.rtbxClient.Text = "";
//
// frmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(448, 397);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.gpbxClient,
this.plClientMessage,
this.plClientTop,
this.gpbxServer,
this.plServerTop});
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "frmMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Socket测试";
this.Closed += new System.EventHandler(this.frmMain_Closed);
this.plServerTop.ResumeLayout(false);
this.gpbxServer.ResumeLayout(false);
this.plClientTop.ResumeLayout(false);
this.plClientMessage.ResumeLayout(false);
this.gpbxClient.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmMain());
}
private void btnListen_Click(object sender, System.EventArgs e)
{
// 把IP地址转换为IpAddress实例
IPAddress ipListen = IPAddress.Parse(tbxListenAddress.Text);
// 初始化监听器
tcpListen = new TcpListener(ipListen, int.Parse(tbxListenPort.Text) );
// 开始监听指定端口
tcpListen.Start();
// 输出服务器启动消息
AddServerMessage("在" + tbxListenPort.Text + "端口启动服务 ……"
;
AddServerMessage("本地节点" + tcpListen.LocalEndpoint);
AddServerMessage("等待连接 ……"
;
btnListen.Enabled = false;
thdListen = new Thread(new ThreadStart(Listen));
thdListen.Start();
}
private void AddServerMessage(string sMessage)
{
rtbxServer.Text += DateTime.Now.ToShortTimeString() + ": " + sMessage + "/n";
}
private void Listen()
{
while (true)
{
Socket serverSocket = tcpListen.AcceptSocket();
AddServerMessage("连接来自 " + serverSocket.RemoteEndPoint);
NetworkStream nwStr = new NetworkStream(serverSocket);
BinaryFormatter binFormat = new BinaryFormatter();
// 反序列化
ArrayList al = (ArrayList)binFormat.Deserialize(nwStr);
reqHead req_head = (reqHead)al[0];
reqBody9021 req_body = (reqBody9021)al[1];
AddServerMessage(req_head.MainId.ToString());
AddServerMessage(req_body.newPwd);
// 返回应答
ansHead ans_head = new ansHead();
ans_head.MainId = 9;
ansBody9021 ans_body = new ansBody9021();
ans_body.errorMsg = "测试阿测试";
ArrayList alSend = new ArrayList();
alSend.Add(ans_head);
alSend.Add(ans_body);
req9021 req_9021 = new req9021();
req_9021.req_body.oldPwd = "天哪";
nwStr.Flush();
binFormat.Serialize(nwStr, req_9021);
nwStr.Flush();
}
}
private void frmMain_Closed(object sender, System.EventArgs e)
{
try
{
thdListen.Abort();
tcpListen.Stop();
}
catch
{
}
}
private void btnSend_Click(object sender, System.EventArgs e)
{
TcpClient tcpClient = new TcpClient();
try
{
tcpClient.Connect(tbxServerAddress.Text, int.Parse(tbxServerPort.Text));
NetworkStream nwStr = tcpClient.GetStream();
reqHead req_head = new reqHead();
req_head.MainId = 2;
reqBody9021 req_body = new reqBody9021();
req_body.newPwd = "123456";
ArrayList al = new ArrayList();
al.Add(req_head);
al.Add(req_body);
// 序列化,发送
BinaryFormatter binFormat = new BinaryFormatter();
binFormat.Serialize(nwStr, al);
nwStr.Flush();
// 反序列化,接收
/*
ArrayList alReceive = (ArrayList)binFormat.Deserialize(nwStr);
ansHead ans_head = (ansHead)alReceive[0];
ansBody9021 ans_body = (ansBody9021)alReceive[1];
AddClientMessage(ans_head.MainId.ToString());
AddClientMessage(ans_body.errorMsg);
*/
req9021 req_9021 = (req9021)binFormat.Deserialize(nwStr);
AddClientMessage(req_9021.req_body.oldPwd.ToString());
// 采用短连接socket通讯,关闭套接字连接
nwStr.Close();
tcpClient.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void AddClientMessage(string sMessage)
{
rtbxClient.Text += DateTime.Now.ToShortTimeString() + ": " + sMessage + "/n";
}
}
}