在做网络通讯方面的程序时,必不可少的是Socket通讯。
那么我们需要有一套既定的,简易的通讯流程。
如下:
- <pre name="code" class="csharp">public class PublicSocket
- {
- public const string DOWNLOAD_STATUS_WAIT = "1";
- public const string DOWNLOAD_STATUS_PAUSE = "2";
- public const string DOWNLOAD_STATUS_DOWNLOADING = "3";
- public const string DOWNLOAD_STATUS_DOWNLOADED = "4";
- public const string DOWNLOAD_STATUS_ERROR = "5";
- //private XmlHelper xmlhelper = new XmlHelper();
- private TcpClient tcpMethod = new TcpClient();
- public static string LoginUserName = "";
- public static Socket HeadClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- private static string iAddress = "192.168.1.1";
- private static int iPort = 7888;
- private static IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse(iAddress), iPort);
- public const int LOGIN_NAMEERROR = 100000;
- public const int LOGIN_CODEERROR = 100001;
- public const int LOGIN_ERROR = 100002;
- public const int LOGIN_OK = 100003;
- public static bool AllowUpdate = true;
- public static void SocketConnect()
- {
- try
- {
- if (!HeadClient.Connected)
- {
- HeadClient.Connect(endpoint);
- }
- }
- catch (Exception ex)
- { }
- }
- public static void SocketClose()
- {
- if (HeadClient.Connected)
- {
- HeadClient.Close();
- }
- }
- public static void ThreadGetSingleDownloadedFile(object data) //object参数可以实现界面传入数据进行使用
- {
- string retMD5 = string.Empty;
- try
- {
- HanleListItemClass formInter = data as HanleListItemClass;
- XmlDocument aa = XmlHelper.BuildXmlDoc("CMD", "1", "1",
- new List<HollXmlNode> { new HollXmlNode("ID","2",null),
- new HollXmlNode("STATE","127",null) });
- //主要为了构建XML文件进行Socket的send操作
- //构造后如下 <?xml version='1.0' encoding='utf-8'?><CMD code='1' index='1'>
- // <ID>2</ID>
- // <STATE>127</STATE></CMD>
- byte[] sendaskbyte = Encoding.UTF8.GetBytes(aa.InnerXml.ToString());
- int sendcount = 0;
- try
- {
- sendcount = HeadClient.Send(sendaskbyte, sendaskbyte.Length, 0);
- }
- catch (Exception ex)
- {
- // return LOGIN_ERROR;
- }
- if (sendcount == 0)
- {
- //return LOGIN_ERROR;
- }
- int ret = SingleDownloadedFileWaitBack(formInter);
- }
- catch
- {
- //return LOGIN_ERROR;
- }
- }
- public static int SingleDownloadedFileWaitBack(HanleListItemClass tItemclass)
- {
- byte[] RetData = new byte[36 * 1024];
- Array.Clear(RetData, 0, RetData.Length);
- int datalen = 0;
- try
- {
- datalen = HeadClient.Receive(RetData);
- if (datalen == 0)
- {
- return -1;
- }
- //接收到服务器返回的信息
- string tcpdatastring = Encoding.UTF8.GetString(RetData).Replace("\0", " "); ;
- //此处可以根据自己需求处理返回的信息 http://www.cnblogs.com/sosoft/
- return 0;
- }
- catch (Exception ex)
- {
- return 0;
- }
- }
- }
复制代码
然后再界面调用的时候,我们只需要在事件中这么使用即可
- private void button1_Click(object sender, EventArgs e)
- { PublicSocket.SocketConnect();
- ThreadPool.QueueUserWorkItem(new WaitCallback(SocketMethod.ThreadGetTotalState), this);
- }
复制代码
同时也把构建XML的类分享出来,使得编码更加简便
- public class XmlHelper
- {
- private static string xmlinit = "<?xml version='1.0' encoding='utf-8'?><{0} code='{1}' index='{2}'></{0}>";
- public static XmlDocument BuildXmlDoc(string xmltype, string code, string index, List<HollXmlNode> xmlnodes)
- {
- XmlDocument root = new XmlDocument();
- root.LoadXml(string.Format(xmlinit, xmltype, code, index));
- if (xmlnodes != null)
- {
- for (int i = 0; i < xmlnodes.Count; i++)
- {
- XmlElement parentNode = root.CreateElement(xmlnodes[i].NodeName);
- XmlText descText = root.CreateTextNode(xmlnodes[i].NodeValue);
- parentNode.AppendChild(descText);
- if (xmlnodes[i].NodeAtt != null)
- {
- if (xmlnodes[i].NodeAtt.Count > 0)
- {
- for (int j = 0; j < xmlnodes[i].NodeAtt.Count; j++)
- {
- parentNode.SetAttribute(xmlnodes[i].NodeAtt[j].AttName, xmlnodes[i].NodeAtt[j].AttValue);
- }
- }
- }
- root.LastChild.AppendChild(parentNode);
- }
- }
- return root;
- }
- }
复制代码- public class HollXmlNode
- {
- private string _nodename;
- private string _nodevalue;
- private List<HollXmlAttribute> _nodeatt;
- public HollXmlNode(string tnodename, string tnodevalue, List<HollXmlAttribute> tnodeatt)
- {
- _nodename = tnodename;
- _nodevalue = tnodevalue;
- _nodeatt = tnodeatt;
- }
- public string NodeName
- {
- get { return _nodename; }
- set { _nodename = value; }
- }
- public string NodeValue
- {
- get { return _nodevalue; }
- set { _nodevalue = value; }
- }
- public List<HollXmlAttribute> NodeAtt
- {
- get { return _nodeatt; }
- set { _nodeatt = value; }
- }
- }
- public class HollXmlAttribute
- {
- private string _attname;
- private string _attvalue;
- public HollXmlAttribute(string tname, string tvalue)
- {
- _attname = tname;
- _attvalue = tvalue;
- }
- public string AttName
- {
- get { return _attname; }
- set { _attname = value; }
- }
- public string AttValue
- {
- get { return _attvalue; }
- set { _attvalue = value; }
- }
- }
复制代码
|
|