日期:2014-05-18  浏览次数:20767 次

一个Socket的连接问题
我的代码中有以下一个函数:
              ///   <summary>
                ///   发送文本消息
                ///   </summary>
                ///   <param   name= "IP "> 目标IP </param>
                ///   <param   name= "msg "> 消息内容 </param>
                ///   <returns> 是否发送成功 </returns>
                public   static   bool   Send(IPAddress   IP,   string   msg)
                {
                        bool   tag   =   false;
                        _IPEP   =   new   IPEndPoint(IP,   _port);
                        if   (!_sendSocket.Connected)
                        {
                                _sendSocket.Connect(_IPEP);
                        }
                                if   (_sendSocket.Connected)
                                {
                                        MessageBox.Show( "Connection   Successful! ");
                                        byte[]   data   =   Encoding.ASCII.GetBytes(msg);
                                        byte[]   sizeinfo   =   new   byte[4];
                                        //could   optionally   call   BitConverter.GetBytes(data.length);
                                        sizeinfo[0]   =   (byte)data.Length;
                                        sizeinfo[1]   =   (byte)(data.Length   > >   8);
                                        sizeinfo[2]   =   (byte)(data.Length   > >   16);
                                        sizeinfo[3]   =   (byte)(data.Length   > >   24);
    &n