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;//错误信息
       /// <summary>
       /// 检查Server的地址是否存在
       /// </summary>
       /// <returns></returns>
       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;
               }
           }
       }
       /// <summary>
       /// 检查Server是否开启
       /// </summary>
       /// <returns></returns>
       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;
               }
           }
       }

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

       /// <summary>
       /// 发送消息
       /// </summary>
       /// <param name="msg"></param>
       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;
           }
       }

       /// <summary>
       /// 接收数据
       /// </summary>
       /// <param name="msg"></param>
       /// <returns></returns>
       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();
           }
       }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值