Example #1
0
        public void YM_SendMessage(List <Object> infolist)
        {
            //获取socket
            Socket sendSocket = GetUdpSocket();
            //获取对方 ip port
            EndPoint youPoint = GetYouUdpPoint() as EndPoint;

            //将数据转换为byte[]
            byte[] dataSe = ClassSerializers.Serializebinary(infolist).ToArray();
            //发送数据
            sendSocket.SendTo(dataSe, youPoint);
            //关闭Socket
            sendSocket.Close();
        }
Example #2
0
 public List <Object> LR_ActionSend_Receive(List <Object> infolist)
 {
     try
     {
         Byte[]        dataReceive = new Byte[1024]; //存放接收数据
         List <Object> dataRece    = null;           //反序列化后的数据
         client.Connect(GetTcpPoint());              //连接服务器
         if (client.Connected)
         {
             byte[] data = ClassSerializers.Serializebinary(infolist).ToArray();
             client.Send(data, data.Length, 0);
             client.Receive(dataReceive, dataReceive.Length, 0);   //接收数据    //在这等着呢 -_-
             dataRece = ClassSerializers.DeSerializebinary(new MemoryStream(dataReceive)) as List <Object>;
         }
         client.Close();
         return(dataRece);
     }
     catch
     {
         return(null);
     }
 }
Example #3
0
        public void YM_ReceiveMessage(RichTextBox richTextBox)
        {
            //获取socket
            Socket receiveSocket = GetUdpSocket();
            //获取本地 ip port
            IPEndPoint mePoint = GetMeUdpPoint();

            //绑定本地ip port
            receiveSocket.Bind(mePoint);
            //获取对方 ip port
            EndPoint youPoint = GetYouUdpPoint() as EndPoint;

            //用于存放信息
            byte[] dataRe = new byte[1024];
            while (true)
            {
                try
                {
                    //在这儿的时候有一个异常(一个封锁操作被对 WSACancelBlockingCall 的调用中断。)
                    //try catch 获取异常但是不管它 直接返回
                    int datalength = receiveSocket.ReceiveFrom(dataRe, ref youPoint);

                    List <Object> listinfo = (ClassSerializers.DeSerializebinary(new MemoryStream(dataRe))) as List <Object>;
                    richTextBox.Invoke(
                        (MethodInvoker)
                        (() =>
                    {
                        richTextBox.AppendText(InfoTransToString(listinfo));
                        richTextBox.ScrollToCaret();
                    }));
                }
                catch
                {
                    break;
                    return;
                }
            }
        }