Example #1
0
 //Send a text message to the server
 //向服务器发送文本消息
 void BtnSendMsgClick(object sender, EventArgs e)
 {
     if (string.IsNullOrEmpty(listMember.Text))
     {
         MessageBox.Show("Please select a friend to send this message.");
     }
     else
     {
         StrMessage strMsg = new StrMessage();
         strMsg.ToMessage = txtMsgSend.Text.Trim();
         strMsg.ToTopic   = txtTpcSend.Text.Trim();
         strMsg.ToName    = listMember.Text;
         byte[] arrMsg     = SerObject.SerializeObject(strMsg);
         byte[] arrMsgSend = new byte[arrMsg.Length + 1];
         arrMsgSend[0] = 0;//设置标识位,0代表发送的是文字
         Buffer.BlockCopy(arrMsg, 0, arrMsgSend, 1, arrMsg.Length);
         try
         {
             socketClient.Send(arrMsgSend);
             ShowMsg(string.Format("#{0}# I speak to {1}:{2}", strMsg.ToTopic, strMsg.ToName, strMsg.ToMessage));
             //清空发送消息文本框中的消息
             this.txtMsgSend.Text = "";
         }
         catch (SocketException ex)
         {
             ShowMsg("SocketException:" + ex.Message);
         }
         catch (Exception ex)
         {
             ShowMsg("Exception:" + ex.Message);
         }
     }
 }
Example #2
0
        //Send a message to each client
        //群发消息给每个客户端
        void BtnSendToAllClick(object sender, EventArgs e)
        {
            StrMessage strMsg = new StrMessage();

            strMsg.ToMessage = txtMsgSend.Text.Trim();
            strMsg.ToTopic   = txtTpcSend.Text.Trim();
            strMsg.ToName    = "The server end";
            byte[] arrMsg     = SerObject.SerializeObject(strMsg);
            byte[] arrMsgSend = new byte[arrMsg.Length + 1];
            arrMsgSend[0] = 0;    //Set the flag, 0 on behalf of the text sent 设置标识位,0代表发送的是文字
            Buffer.BlockCopy(arrMsg, 0, arrMsgSend, 1, arrMsg.Length);
            for (int i = 0; i < dict.Size(); i++)
            {
                try
                {
                    Socket s = dict[i];
                    s.Send(arrMsgSend);
                    ShowMsg(string.Format("#{0}# I speak to All : {1}", strMsg.ToTopic, strMsg.ToMessage));
                }
                catch (SocketException se)
                {
                    ShowMsg("SocketException:" + se.Message);
                    break;
                }
                catch (Exception ex)
                {
                    ShowMsg("SocketException:" + ex.Message);
                    break;
                }
            }
        }
Example #3
0
        void Protocol_MessageReceived2(object sender, FmdcEventArgs e)
        {
            StrMessage msg = e.Data as StrMessage;

            if (msg != null)
            {
                System.Console.WriteLine("IN:" + msg.Raw);
            }
        }
        void Protocol_MessageReceived(object sender, FlowLib.Events.FmdcEventArgs e)
        {
            StrMessage msg = e.Data as StrMessage;

            if (msg != null)
            {
                System.Console.WriteLine(string.Format("IN: {0}", msg.Raw));
            }
        }
Example #5
0
        void Protocol_MessageReceived(object sender, FmdcEventArgs e)
        {
            StrMessage msg = e.Data as StrMessage;

            if (msg != null)
            {
                msgList.Add(string.Format("IN: {0}", msg.Raw));
                listMainchat.Show();
            }
        }
Example #6
0
        void Trans_Protocol_MessageReceived(object sender, FmdcEventArgs e)
        {
            StrMessage msg = e.Data as StrMessage;

            if (msg != null)
            {
                Program.Write(string.Format("[{0}] TRA REC: {1}\r\n",
                                            System.DateTime.Now.ToLongTimeString(),
                                            msg.Raw));
            }
        }
Example #7
0
        void Protocol_MessageToSend(object sender, FmdcEventArgs e)
        {
            StrMessage msg = e.Data as StrMessage;

            if (msg != null)
            {
                Program.Write(string.Format("[{0}] HUB SEN: {1}\r\n",
                                            System.DateTime.Now.ToLongTimeString(),
                                            msg.Raw));
            }
        }
Example #8
0
        /// <summary>
        /// Listen to the message sent by the server监听服务端发来的消息
        /// </summary>
        void ReceiveMsg()//0 is an ordinary message, 1 is a user list 0是普通消息,1是用户列表
        {
            while (true)
            {
                //Define a receive byte array buffer (2M size)
                //定义一个接收消息用的字节数组缓冲区(2M大小)
                byte[] arrMsgRev = new byte[1024 * 1024 * 2];
                //Save the received data to arrMsgRev and return the length of the data actually received
                //将接收到的数据存入arrMsgRev,并返回真正接收到数据的长度
                int length = -1;
                try
                {
                    length = socketClient.Receive(arrMsgRev);
                }
                catch (SocketException ex)
                {
                    ShowMsg("SocketException:" + ex.Message);
                    break;
                }
                catch (Exception ex)
                {
                    ShowMsg("Exception:" + ex.Message);
                    break;
                }
                if (arrMsgRev[0] == 0) //Judge the client sent the first element of the data is 0, on behalf of the text 判断客户端发送过来数据的第一位元素是0,代表文字
                {
                    //At this point all the elements of the array (each byte) are converted to a string, and really received only the server sent a few characters
                    //此时是将数组的所有元素(每个字节)都转成字符串,而真正接收到只有服务端发来的几个字符
                    //string strMsgReceive = Encoding.UTF8.GetString(arrMsgRev, 1, length - 1);
                    byte[] strMsgRev = new byte[arrMsgRev.Length - 1];
                    Buffer.BlockCopy(arrMsgRev, 1, strMsgRev, 0, arrMsgRev.Length - 1);
                    StrMessage strMsgReceive = (StrMessage)SerObject.DeserializeObject(strMsgRev);
                    ShowMsg(string.Format("#{0}# {1} speak to me {2}", strMsgReceive.ToTopic, strMsgReceive.ToName, strMsgReceive.ToMessage));
                }
                else if (arrMsgRev[0] == 1)//1 is the online user list 1是在线用户列表
                {
                    byte[] strMsgRev = new byte[arrMsgRev.Length - 1];
                    Buffer.BlockCopy(arrMsgRev, 1, strMsgRev, 0, arrMsgRev.Length - 1);
                    List <string> listname = (List <String>)SerObject.DeserializeObject(strMsgRev);

                    listMember.Items.Clear(); //Empty the text 清空文本
                    foreach (string name in listname)
                    {
                        listMember.Items.Add(name);
                    }
                }
            }
        }
Example #9
0
        //Send a message to the client
        //发送消息到客户端
        void BtnSendClick(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(listMember.Text))
            {
                MessageBox.Show("Please select a friend to send the message.");
            }
            else
            {
                StrMessage strMsg = new StrMessage();
                strMsg.ToMessage = txtMsgSend.Text.Trim();
                strMsg.ToTopic   = txtTpcSend.Text.Trim();
                strMsg.ToName    = "The server end";
                //set the string which to be sent into UTF-8 corresponding byte array
                //将要发送的字符串转成UTF-8对应的字节数组
                byte[] arrMsg = SerObject.SerializeObject(strMsg);
                //noname================================================================================
                byte[] arrMsgSend = new byte[arrMsg.Length + 1];
                arrMsgSend[0] = 0;    //设置标识位,0代表发送的是文字
                Buffer.BlockCopy(arrMsg, 0, arrMsgSend, 1, arrMsg.Length);
                //Get the list of selected remote IP Key
                //获得列表中选中的远程IP的Key
                String strCli       = listMember.Text;
                string strClientKey = clientlist.toK(strCli);
                try
                {
                    //Through the key to find the dictionary set corresponding to a client communication socket Send method to send data to each other
                    //通过key找到字典集合中对应的某个客户端通信的套接字,用Send方法发送数据给对方
                    Socket s = dict[strClientKey];
                    s.Send(arrMsgSend);

                    ShowMsg(string.Format("#{0}#I speak to {1} : {2}", strMsg.ToTopic, listMember.Text, strMsg.ToMessage));
                    //Clear the message in the send box
                    //清空发送框中的消息
                    this.txtMsgSend.Text = "";
                }
                catch (SocketException ex)
                {
                    ShowMsg("SocketException:" + ex.Message);
                }
                catch (Exception ex)
                {
                    ShowMsg("Exception:" + ex.Message);
                }
            }
        }
Example #10
0
        /// Server-side monitoring client-side sends data 服务端监听客户端发来的数据
        /// </summary>
        void ReceiveMsg(object socketClientPara)    //0 is normal message, 1 is the user's nickname 0普通消息,1是用户昵称
        {
            Socket socketClient = socketClientPara as Socket;

            while (true)
            {
                //Define a receive byte array buffer (2M size)
                //定义一个接收消息用的字节数组缓冲区(2M大小)
                byte[] arrMsgRev = new byte[1024 * 1024 * 2];
                //Save the received data to arrMsgRev and return the length of the data actually received
                //将接收到的数据存入arrMsgRev,并返回真正接收到数据的长度
                int length = -1;
                try
                {
                    length = socketClient.Receive(arrMsgRev);
                }
                catch (SocketException ex)
                {
                    ShowMsg("Exception:" + ex.Message + "RemoteEndPoint: " + socketClient.RemoteEndPoint.ToString());
                    clientlist.Remove(socketClient.RemoteEndPoint.ToString());
                    sendNewClientList();
                    //Removes the communication socket that was disconnected from the communication socket combination
                    //从通信套接字结合中删除被中断连接的通信套接字
                    dict.Remove(socketClient.RemoteEndPoint.ToString());
                    //Removes the disconnected communication thread object from the set of communication threads
                    //从通信线程集合中删除被中断连接的通信线程对象
                    dictThread.Remove(socketClient.RemoteEndPoint.ToString());
                    //Remove the disconnected IP: Port from the display list
                    //从显示列表中移除被中断连接的IP:Port
                    listMember.Items.Remove(socketClient.RemoteEndPoint.ToString());
                    break;
                }
                catch (Exception ex)
                {
                    ShowMsg("Exception:" + ex.Message);
                    break;
                }
                if (arrMsgRev[0] == 0)     //Judge the client sent the first element of the data is 0, on behalf of the text 判断客户端发送过来数据的第一位元素是0,代表文字
                {
                    //At this point all the elements of the array (each byte) are converted to a string, and really received only the server sent a few characters
                    //此时是将数组的所有元素(每个字节)都转成字符串,而真正接收到只有服务端发来的几个字符
                    //string strMsgReceive = Encoding.UTF8.GetString(arrMsgRev, 1, length - 1);
                    byte[] strMsgRev = new byte[arrMsgRev.Length - 1];
                    Buffer.BlockCopy(arrMsgRev, 1, strMsgRev, 0, arrMsgRev.Length - 1);
                    StrMessage strMsgReceive = (StrMessage)SerObject.DeserializeObject(strMsgRev);

                    if (strMsgReceive.ToName == "The server end")
                    {
                        ShowMsg(string.Format("#{0}# {1} speak to me :{2}", strMsgReceive.ToTopic, clientlist[socketClient.RemoteEndPoint.ToString()], strMsgReceive.ToMessage));
                    }
                    else
                    {
                        StrMessage strMsg = new StrMessage();
                        strMsg.ToMessage = strMsgReceive.ToMessage;
                        strMsg.ToTopic   = strMsgReceive.ToTopic;
                        strMsg.ToName    = clientlist[socketClient.RemoteEndPoint.ToString()];
                        //set the string which to be sent into UTF-8 corresponding byte array
                        //将要发送的字符串转成UTF-8对应的字节数组
                        byte[] arrMsg     = SerObject.SerializeObject(strMsg);
                        byte[] arrMsgSend = new byte[arrMsg.Length + 1];
                        arrMsgSend[0] = 0;//Set the flag, 0 on behalf of the text sent 设置标识位,0代表发送的是文字
                        Buffer.BlockCopy(arrMsg, 0, arrMsgSend, 1, arrMsg.Length);
                        //Get the list of selected remote IP Key
                        //获得列表中选中的远程IP的Key
                        String strCli       = strMsgReceive.ToName;
                        string strClientKey = clientlist.toK(strCli);
                        try
                        {
                            //Through the key to find the dictionary set corresponding to a client communication socket Send method to send data to each other
                            //通过key找到字典集合中对应的某个客户端通信的套接字,用Send方法发送数据给对方
                            Socket s = dict[strClientKey];
                            s.Send(arrMsgSend);

                            ShowMsg(string.Format("#{0}# {1} speak to {2} :{3}", strMsgReceive.ToTopic, strMsgReceive.ToName, clientlist[socketClient.RemoteEndPoint.ToString()], strMsgReceive.ToMessage));
                        }
                        catch (SocketException ex)
                        {
                            ShowMsg("SocketException:" + ex.Message);
                        }
                        catch (Exception ex)
                        {
                            ShowMsg("Exception:" + ex.Message);
                        }
                    }
                }
                else if (arrMsgRev[0] == 1)    //1 for the nickname 1代表昵称
                {
                    String nickname = Encoding.UTF8.GetString(arrMsgRev, 1, length - 1);
                    clientlist[socketClient.RemoteEndPoint.ToString()] = nickname;
                    listMember.Items.Clear();     //Empty the text 清空文本
                    for (int i = 0; i < clientlist.Size(); i++)
                    {
                        listMember.Items.Add(clientlist[i]);
                    }

                    sendNewClientList();
                }
            }
        }