wince 通信方式

wince通过gprs联网  ,通过socket或者是webservice和pc通信。web服务器端程序是其他人用java做的,发现无论是用socket还是webservice来通信服务器接收数据很慢都是同样的效果。然后我就把服务器端程序换成用C#做的窗体应用程序,并且开了个线程来不断的接收设备发来的数据。这样就能很快接收到数据了。

客户端程序:

 public class ClassTongXin
    {
       public static int ServerPort;//服务器端口

       public static string ServerIp;//服务器ip地址
     

       public static TcpClient TcpClient = null;

       public static IPAddress ServerAddress = null;

       public  string m_Error;//错误信息
       ///


       /// 检查Server的地址是否存在
       ///

       ///
       public bool GetServerAddress()
       {
           if (string.IsNullOrEmpty(ServerIp))
           {
               m_Error = "服务器不存在";
               return false;

           }
           else
           {
               try
               {
                   ServerAddress = IPAddress.Parse(ServerIp);
                   return true;
               }
               catch (Exception ex)
               {
                   m_Error = "检查服务器异常:" + ex.Message;
                   return false;
               }
           }
       }
       ///


       /// 检查Server是否开启
       ///

       ///
       public bool GetServerIsExist()
       {
           if (string.IsNullOrEmpty(ServerIp))
           {
               m_Error = "服务器不存在";
               return false;
           }
           else
           {
               IPHostEntry remoteHost = null;
               try
               {
                   remoteHost = Dns.GetHostEntry(ServerIp);
                   return true;
               }
               catch (SocketException socketEx)
               {
                   m_Error = "服务器未开启:" + socketEx.Message;
                   return false;
               }
           }
       }

       ///


       /// 建立连接
       ///

       ///
       public bool CreateConnection()
       {
           TcpClient = new TcpClient();
           try
           {
              
               TcpClient.Connect(ServerAddress,ServerPort);
               return true;
           }
           catch (Exception odEx)
           {
               m_Error = "与服务器建立连接失败:" + odEx.Message;
               return false;
           }
       }

       ///


       /// 发送消息
       ///

       ///
       public bool SendMessage(string msg)
       {
           try
           {
               byte[] sendMsg = System.Text.Encoding.GetEncoding("GB2312").GetBytes(msg);

               TcpClient.Client.Send(sendMsg, SocketFlags.None);
               return true;
           }
           catch (Exception e)
           {
               m_Error = "向服务器发送消息失败" + e.Message;
               return false;
           }
       }

       ///


       /// 接收数据
       ///

       ///
       ///
       public string RcvMessage()
       {
           string msg = "";
           try
           {
               byte[] receiveMsg = new byte[8192];

               int rflags = TcpClient.Client.Receive(receiveMsg, SocketFlags.None);

               msg = System.Text.Encoding.GetEncoding("GB2312").GetString(receiveMsg, 0, rflags);
              
               return msg;
           }
           catch (Exception e)
           {
               m_Error = "接收服务器数据失败:" + e.Message;
               return "";
           }
       }

       public  void Disconnect()
       {
           if (TcpClient != null)
           {
               TcpClient.Client.Shutdown(SocketShutdown.Both);
               TcpClient.Close();
           }
       }

你可能感兴趣的:(wince开发问题)