//退出按钮点击事件 private void buttonClose_Click(object sender, EventArgs e) { //如果还未建立连接 if (socketClient == null) { this.Close(); return; } //如果已经建立连接 try { byte[] arrClientSendMsg = Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(userName, 0, userName.Length) + "退出了聊天室"); commum.Message msg = new commum.Message(); msg.message = arrClientSendMsg; msg.ipAddress = userName; arrClientSendMsg = PackCodec.Serialize <commum.Message>(msg); SendMsg(arrClientSendMsg); socketClient.Shutdown(SocketShutdown.Both); this.Close(); } catch (SocketException) { txtMsg.AppendText("当前未连接\r\n"); this.Close(); return; } catch (NullReferenceException) { return; } }
/// <summary> /// 接收指定客户端Socket的消息 /// </summary> /// <param name="clientSocket"></param> private static void RecieveMessage(object clientSocket) { Socket mClientSocket = (Socket)clientSocket; while (true) { try { int receiveNumber = mClientSocket.Receive(result); Console.WriteLine("接收客户端{0}消息, 长度为{1}", mClientSocket.RemoteEndPoint.ToString(), receiveNumber); ByteBuffer buff = new ByteBuffer(result); int datalength = buff.ReadShort(); int typeId = buff.ReadInt(); byte[] pbdata = buff.ReadBytes(); //通过协议号判断选择的解析类 if (typeId == (int)EnmCmdID.CS_LOGIN_REQ) { CSLoginReq clientReq = PackCodec.Deserialize <CSLoginReq>(pbdata); string user_name = clientReq.LoginInfo.UserName; string pass_word = clientReq.LoginInfo.Password; Console.WriteLine("数据内容:UserName={0},Password={1}", user_name, pass_word); } } catch (Exception ex) { Console.WriteLine(ex.Message); mClientSocket.Shutdown(SocketShutdown.Both); mClientSocket.Close(); break; } } }
//发送按钮 private void button1_Click(object sender, EventArgs e) { byte[] arrClientSendMsg = Encoding.UTF8.GetBytes(txtCMsg.Text.Trim()); commum.Message msg = new commum.Message(); msg.message = arrClientSendMsg; msg.ipAddress = userName; arrClientSendMsg = PackCodec.Serialize <commum.Message>(msg); SendMsg(arrClientSendMsg); }
public byte[] Send(Google.Protobuf.IMessage pbData) { NetWriter writer = NetWriter.Instance; SetActionHead(writer, pbData); writer.SetBodyData(PackCodec.Serialize(pbData)); return(writer.PostData()); }
private void buttonLogin_Click(object sender, EventArgs e) { byte[] arrClientSendMsg = Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(userName, 0, userName.Length) + "进入了聊天室"); commum.Message msg = new commum.Message(); msg.message = arrClientSendMsg; msg.ipAddress = userName; arrClientSendMsg = PackCodec.Serialize <commum.Message>(msg); SendMsg(arrClientSendMsg); btnSend.Enabled = true; buttonLogin.Enabled = false; }
/// <summary> /// 接收服务端发来信息的方法 /// </summary> private void RecMsg() { byte[] arrRecMsg = new byte[1024]; while (true) //持续监听服务端发来的消息 { commum.Message msg = new commum.Message(); //将客户端套接字接收到的数据存入内存缓冲区, 并获取其长度 try { Array.Clear(arrRecMsg, 0, 1024); socketClient.Receive(arrRecMsg); } catch (SocketException) { string str = "网络错误,请退出聊天室"; txtMsg.AppendText("我:" + GetCurrentTime() + "\r\n" + str + "\r\n"); return; } lock (objLock) { //获取传输的数据中数据的位数 for (length = 0; arrRecMsg[length] != '\0' || length > 1024; length++) { } //越界 if (length > 1024) { continue; } byte[] rec = new byte[length]; Array.Clear(rec, 0, length); for (int i = 0; i < length; i++) { rec[i] = arrRecMsg[i]; } msg = PackCodec.Deserialize <commum.Message>(rec); if (msg == null) { continue; } string strRecMsg = Encoding.UTF8.GetString(msg.message, 0, msg.message.Length); string strRecName = Encoding.UTF8.GetString(msg.ipAddress, 0, msg.ipAddress.Length); if (strRecName.Equals("0"))//其他信息 { txtMsg.AppendText(strRecMsg + "\r\n"); } else { txtMsg.AppendText(strRecName + ":" + GetCurrentTime() + "\r\n" + strRecMsg + "\r\n"); } } } }
protected virtual void SetActionHead(NetWriter writer, Google.Protobuf.IMessage pbData) { //writer.writeInt32("actionId", ActionId); byte[] bodyBuffer = PackCodec.Serialize(pbData); ByteBuffer headBuffer = new ByteBuffer(); headBuffer.WriteInt(this.ActionId); headBuffer.WriteInt(bodyBuffer.Length); headBuffer.WriteInt(Head.MsgId); headBuffer.WriteInt(0); //body_check; headBuffer.WriteInt(0); //head_check; writer.SetHeadBuffer(headBuffer.ToBytes()); }
public void OnAddBack(byte[] Byte) { ByteBuffer buffer = new ByteBuffer(Byte); int size = Byte.Length; int typeId = buffer.ReadInt(); s2c_add_friend_reply reply = PackCodec.Deserialize <s2c_add_friend_reply>(buffer.ReadBytes(size - sizeof(Int32))); Int32 ret = reply.result; if (ret == 1) { PanelMgr.instance.OpenPanel <TipPanel>("", "添加成功!"); } else { PanelMgr.instance.OpenPanel <TipPanel>("", "添加失败,用户名id不存在或好友已添加!"); } }
//接收刷新回包数据 public void OnReflashBack(byte[] Byte) { ClearFriendUnit(); ByteBuffer buffer = new ByteBuffer(Byte); int size = Byte.Length; int typeId = buffer.ReadInt(); s2c_friend_list_reply reply = PackCodec.Deserialize <s2c_friend_list_reply>(buffer.ReadBytes(size - sizeof(Int32))); UInt32 num = reply.num; List <friend_info> infos = reply.infos; for (int i = 0; i < num; i++) { UInt32 Id = infos[i].id; string username = infos[i].username; UInt32 image = infos[i].image; UInt32 status = infos[i].status; //生成好友面板 GenerateFriendUnit(i, Id, username, image, status); } }