Client端的代码:
private void F_logon_Load(object sender, EventArgs e)
{
Publec_Class.ServerIP = "127.0.0.1";
Publec_Class.ServerPort = "11000";
udpSocket1.Active = true; //启动自定义的udpSocket1控件
}
private void read_Click(object sender, EventArgs e)
{
if (Publec_Class.ServerPort != "" && Publec_Class.ServerIP != "") //当读取到服务器的IP和端口号时
{
RegisterMsg registermsg = new RegisterMsg();
registermsg.name = txt_name.Text;
registermsg.psw = txt_psw.Text;
byte[] registerData = new ClassSerializers().SerializeBinary(registermsg).ToArray();
ClassMsg msg = new ClassMsg();
msg.SendKind = SendKind.SendCommand;
msg.msgCommand = MsgCommand.Logining;
msg.Data = registerData;
udpSocket1.Send(IPAddress.Parse(Publec_Class.ServerIP), Convert.ToInt32(Publec_Class.ServerPort), new ClassSerializers().SerializeBinary(msg).ToArray());
Publec_Class.UserName = txt_name.Text;
}
}
private void udpSocket1_DataArrival(byte[] Data, IPAddress Ip, int Port)
{
DataArrivaldelegate outdelegate = new DataArrivaldelegate(DataArrival);
this.BeginInvoke(outdelegate, new object[] { Data, Ip, Port });
}
private delegate void DataArrivaldelegate(byte[] Data, System.Net.IPAddress Ip, int Port);
private void DataArrival(byte[] Data, System.Net.IPAddress Ip, int Port) //当有数据到达后的处理进程
{
try
{
ClassMsg msg = new ClassSerializers().DeSerializeBinary((new System.IO.MemoryStream(Data))) as ClassMsg;
switch (msg.msgCommand)
{
case MsgCommand.Logined://登录成功
Publec_Class.UserID = msg.SID;
DialogResult = DialogResult.OK;
break;
}
}
catch { }
}
|