专注

导航

Socket通信实例

   1:          /// <summary>
   2:          /// 通过Socket与通信服务器通信
   3:          /// </summary>
   4:          /// <param name="GroupServerIPAddr">输入参数:通信服务器地址</param>
   5:          /// <param name="GroupServerIPPort">输入参数:通信服务器端口</param>
   6:          /// <param name="SendData">输入参数:注册服务器信息</param>
   7:          /// <returns>返回:ReceiveDataFromSocket数据实体</returns>
   8:          public static Model.ReceiveDataFromSocket CommunicateToCrossServer(string GroupServerIPAddr, int GroupServerIPPort, byte[] SendData)
   9:          {
  10:              IPHostEntry ipHostInfo = Dns.Resolve(GroupServerIPAddr);
  11:              IPAddress ipAddress = ipHostInfo.AddressList[0];
  12:              IPEndPoint remoteEP = new IPEndPoint(ipAddress, GroupServerIPPort);
  13:              byte[] ReceiveData = new byte[0];
  14:              int ReceiveDataLen=0;
  15:              string errorStr = "";
  16:              //创建一个基于TCP的Socket
  17:              Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  18:              try
  19:              {
  20:                  //尝试连接 
  21:                  client.Connect(remoteEP);
  22:              }
  23:              catch (Exception se)
  24:              {
  25:                  errorStr = "连接错误" + se.Message;
  26:                  return null;
  27:              }
  28:              try
  29:              {
  30:                  //向主机发送请求 
  31:                  client.Send(SendData, SendData.Length, 0);
  32:              }
  33:              catch (Exception ce)
  34:              {
  35:                  errorStr = "发送错误:" + ce.Message;
  36:              }
  37:              int bytes = 0;
  38:              try
  39:              {
  40:                  List<byte[]> tempList = new List<byte[]>();
  41:                  while (true)
  42:                  {
  43:                      byte[] temp = new byte[1024];
  44:                      //每次读取1024
  45:                      bytes = client.Receive(temp, 1024, 0);
  46:                      if (bytes <= 0)
  47:                      {
  48:                          if (ReceiveDataLen != 0)
  49:                          {
  50:                              ReceiveData = new byte[ReceiveDataLen];
  51:                              int TempLen = 0;
  52:                              //合并数组
  53:                              for (int i = 0; i <= tempList.Count - 1; i++)
  54:                              {
  55:                                  tempList[i].CopyTo(ReceiveData, TempLen);
  56:                                  TempLen = TempLen + tempList[i].Length;
  57:                              }
  58:                          }
  59:                          break;
  60:                      }
  61:                      else
  62:                      {
  63:                          if (bytes == 1024)
  64:                          {
  65:                              tempList.Add(temp);
  66:                          }
  67:                          else
  68:                          {
  69:                              byte[] temp2 = new byte[bytes];
  70:                              Array.Copy(temp, 0, temp2, 0, bytes);
  71:                              tempList.Add(temp2);
  72:   
  73:                          }
  74:                          ReceiveDataLen = bytes + ReceiveDataLen;
  75:                      }
  76:                  }
  77:              }
  78:              catch (Exception ce)
  79:              {
  80:                  errorStr = "接收错误" + ce.Message;
  81:                  //禁用Socket 
  82:                  client.Shutdown(SocketShutdown.Both);
  83:                  //关闭Socket 
  84:                  client.Close();
  85:                  return null;
  86:              }
  87:              Model.ReceiveDataFromSocket myReceiveData = new RailwayCross2011.Model.ReceiveDataFromSocket();
  88:              myReceiveData.ReceiveData = ReceiveData;
  89:              myReceiveData.ReceiveDataLen = ReceiveDataLen;
  90:              myReceiveData.errorStr = errorStr;
  91:              //禁用Socket 
  92:              client.Shutdown(SocketShutdown.Both);
  93:              //关闭Socket 
  94:              client.Close();
  95:              return myReceiveData;
  96:          }

流程图说明:

12

从Socket接收的数据实体说明(由于需要通过WebService调用这个方法,所以一定要系列化)

   1:      [Serializable]
   2:      public class ReceiveDataFromSocket
   3:      {
   4:          /// <summary>
   5:          /// 从Sokcet接收的数据
   6:          /// </summary>
   7:          public byte[] ReceiveData
   8:          {
   9:              get;
  10:              set;
  11:          }
  12:          /// <summary>
  13:          /// 数据长度
  14:          /// </summary>
  15:          public int ReceiveDataLen
  16:          {
  17:              get;
  18:              set;
  19:          }
  20:          /// <summary>
  21:          /// 出错信息
  22:          /// </summary>
  23:          public string errorStr
  24:          {
  25:              get;
  26:              set;
  27:          }
  28:      }

posted on 2011-11-14 12:19  陈啊M  阅读(368)  评论(0编辑  收藏  举报