Esempio n. 1
0
        public void SendRequest(string toClient, int n)
        {
            Message messageSendRequest = new Message("301", UserInfo.UserID, toClient, n);

            byte[] bufferSend = SerializationUnit.SerializeObject(messageSendRequest);
            socketLine.Send(bufferSend);
        }
Esempio n. 2
0
        /// <summary>
        /// 向服务端发送命令
        /// </summary>
        /// <param name="structData"></param>
        public static void SendCommand(StructData structData)
        {
            if (clientSocket != null && clientSocket.Connected)
            {
                byte[] sendBytes = seru.SerializeObject(structData);
                if (sendBytes.Length < 2)
                {
                    return;
                }

                byte[] sendBuf  = new byte[sendBytes.Length + 8];
                byte[] sendkey1 = System.BitConverter.GetBytes(543215);
                byte[] sendkey2 = System.BitConverter.GetBytes(567890);

                try {
                    sendBytes.CopyTo(sendBuf, 4);
                    sendkey1.CopyTo(sendBuf, sendBytes.Length + 4);
                    sendkey2.CopyTo(sendBuf, 0);

                    int n = clientSocket.Send(sendBuf, sendBuf.Length, 0);

                    if (n < 1)
                    {
                        n = clientSocket.Send(sendBuf, sendBuf.Length, 0);
                    }
                    if (n < 1)
                    {
                        n = clientSocket.Send(sendBuf, sendBuf.Length, 0);
                    }
                } catch (SocketException) { CloseClientSocket(); } catch (ObjectDisposedException) { CloseClientSocket(); } catch { /** 其他非socket异常,不做处理 */ }
            }
        }
Esempio n. 3
0
        private void BtnSendMsg_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.cmbSocket.Text.Trim() == "")
                {
                    MessageBox.Show("请选择目标EndPoint");
                    return;
                }

                string ipSend = this.cmbSocket.Text.Trim();
                #region 对消息的处理
                string  strMsg      = txtMsg.Text.Trim().ToString();//得到需要发送的消息
                Message messageSend = new Message("000", "127.0.0.1:6666", ipSend, strMsg);
                byte[]  bufferSend  = SerializationUnit.SerializeObject(messageSend);
                dicSocket[ipSend].Send(bufferSend);
                //发送完成,清空文本输入窗口
                txtMsg.Text = "";
                WriteLog(string.Format("{0} To {1} 发送:{2} 成功", messageSend.FromClient, ipSend, messageSend.Msg));
                #endregion
            }
            catch
            {
                MessageBox.Show("给客户端发送消息错误.");
            }
        }
Esempio n. 4
0
        //在窗口加载的时候,像服务器发送第一次数据
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            string  userId      = UserInfo.UserID;
            Message messageSend = new Message("001", userId, socketLine.LocalEndPoint.ToString(), "绑定用户ip地址");

            byte[] bufferSend = SerializationUnit.SerializeObject(messageSend);
            socketLine.Send(bufferSend);
        }
Esempio n. 5
0
        public void SendFile(string toClient, byte[] buffer, int r)
        {
            //包装消息
            Message messageSendFile = new Message("201", UserInfo.UserID, toClient, buffer);

            byte[] bufferSend = SerializationUnit.SerializeObject(messageSendFile);
            socketLine.Send(bufferSend);
        }
Esempio n. 6
0
        public void SendMessage(string toClient, string msg)
        {
            //包装消息
            Message messageSend = new Message("101", UserInfo.UserID, toClient, msg);

            byte[] bufferSend = SerializationUnit.SerializeObject(messageSend);
            socketLine.Send(bufferSend);
        }
Esempio n. 7
0
        private void Receive(Object o)
        {
            Socket socketReceive = o as Socket;
            string userid        = "";

            try
            {
                while (true)
                {
                    //将接收到额buffer转为Message
                    byte[] buffer = new byte[1024 * 1024 * 2];
                    int    len    = socketReceive.Receive(buffer);

                    if (len == 0)
                    {
                        break;
                    }
                    else
                    {
                        //反序列化,将buffer转化为类
                        WriteLog(string.Format("接收到来自客户端:{0} 的数据", socketReceive.RemoteEndPoint));

                        byte[] bufferReceive = new byte[len];
                        for (int i = 0; i < len; i++)
                        {
                            bufferReceive[0] = buffer[0];
                        }
                        Message messageReceive = SerializationUnit.DeserializeObject(buffer) as Message;
                        string  fromClient     = messageReceive.FromClient; //从哪里发过来
                        string  toClient       = messageReceive.ToClient;   //要发到哪去
                        userid = messageReceive.Sign;                       //做一个标记

                        #region 根据协议处理数据
                        switch (messageReceive.Sign)
                        {
                            #region 001 完成用户ID与端口的绑定
                        case "001":
                            //数据逻辑处理
                            dicSocket.Add(messageReceive.FromClient, socketReceive);    //将用户id与ip地址绑定

                            //UI处理以及日志
                            this.txtLog.Dispatcher.Invoke(receiveCallBack, string.Format("登录用户ID:{0}\n登录的ip地址:{1}", fromClient, socketReceive.RemoteEndPoint.ToString()));
                            //this.cmbSocket.Dispatcher.Invoke(setCmbCallBack, messageReceive.FromClient);//ui控制
                            WriteLog(string.Format("登录用户ID:{0}\n登录的ip地址:{1}", fromClient, socketReceive.RemoteEndPoint.ToString()));
                            break;

                            #endregion
                            #region 101 完成客户端数据的转发
                        case "101":
                            if (dicSocket.ContainsKey(fromClient))
                            {
                                //将对象重新序列化
                                byte[] sendByte = SerializationUnit.SerializeObject(messageReceive);
                                dicSocket[toClient].Send(sendByte);
                                //写日志
                                this.txtLog.Dispatcher.Invoke(receiveCallBack,
                                                              string.Format("fromClient:{0}\ntoClient:{1}\nMessage:{2}\n",
                                                                            fromClient, toClient, messageReceive.Msg));
                                WriteLog(string.Format("fromClient:{0}\ntoClient:{1}\nMessage:{2}\n",
                                                       fromClient, toClient, messageReceive.Msg));
                            }
                            else
                            {
                                MessageBox.Show("服务器找不到该用户");
                            }
                            break;

                            #endregion
                            #region 201 发送文件的事件
                        case "201":
                            if (dicSocket.ContainsKey(fromClient))
                            {
                                //将对象重新序列化
                                byte[] sendByte = SerializationUnit.SerializeObject(messageReceive);
                                dicSocket[toClient].Send(sendByte);
                                //写日志
                                this.txtLog.Dispatcher.Invoke(receiveCallBack,
                                                              string.Format("fromClient:{0}\ntoClient:{1}\nMessage:{2}\n",
                                                                            fromClient, toClient, "转发文件成功"));
                                WriteLog(string.Format("fromClient:{0}\ntoClient:{1}\nMessage:{2}\n",
                                                       fromClient, toClient, "转发文件成功"));
                            }
                            else
                            {
                                MessageBox.Show("服务器找不到该用户");
                            }
                            break;

                            #endregion
                        case "301":
                            if (dicSocket.ContainsKey(fromClient))
                            {
                                //将对象重新序列化
                                byte[] sendByte = SerializationUnit.SerializeObject(messageReceive);
                                //转发数据
                                dicSocket[toClient].Send(sendByte);
                                //写日志
                                this.txtLog.Dispatcher.Invoke(receiveCallBack,
                                                              string.Format("fromClient:{0}\ntoClient:{1}\nRequest:{2}\n",
                                                                            fromClient, toClient, messageReceive.Request));
                                WriteLog(string.Format("fromClient:{0}\ntoClient:{1}\nRequest:{2}\n",
                                                       fromClient, toClient, messageReceive.Request));
                            }
                            else
                            {
                                MessageBox.Show("服务器找不到该用户");
                            }
                            break;

                        default:
                            break;
                        }
                        #endregion

                        //进行内存回收
                        GC.Collect();
                    }
                }
            }
            catch (Exception ex)
            {
                dicSocket.Remove(userid);
                //MessageBox.Show("接收服务端发送的消息出错:" + ex.ToString());
            }
        }