.net平台下C#socket通信(中)

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

上篇.net平台下C#socket通信(上)介绍了socket通信的基本原理及最基本的通信方式。本文在此基础上就socket通信时经常遇到的问题做一个简单总结,都是项目中的一些小问题,拿来此处便于下次使用,同时对在使用socket时出现些许问题的同仁们多一个粗浅建议。不足之处请提出,谢谢。

本文主要讲述:

1、正常通信中握手建立

2、一对多的通信

3、发送接收数据格式转换

4、资源释放

5、开启并保持服务监听

1、握手建立正常的通信通道

项目需要通信的双方(假设是一个上位机、一个下位机)之间需要建立一个稳定的通道,以便进行通信。本项目中具体操作是:上位机作为服务器,下位机作为客户端,同时制定通信协议。上位机首先打开监听等待建立通道,下位机主动连接上位机后发送连接成功的信息到上位机,上位机根据通信协议发送数据到下位机,此时通道已经建立。但为了保险起见(同时遵循三次握手),客户端再次发送数据到上位机告知通道建立完毕。

2、一对多通信

项目需求是一个上位机多个下位机,这就确定了上位机做为服务器端,下位机作为客户端主动连接服务器。一对一通信时只有一个socket通道,因此无论是上位机还是下位机在发送和接收数据的时候都不会存在数据乱发乱收的情况。一对多意味着上位机和下位机会建立起多个通道,因此在发送数据时需要记录哪一个下位机处于哪个socket通道中,以便进行逻辑处理。本文处理一对多通信的过程是:

1)首先建立一个对话类session:

publicclassSession

{publicSocket ClientSocket {get;set; }//客户端的socketpublicstringip;//客户端的ippublicSession(Socket clientSocket)

{this.ClientSocket =clientSocket;this.IP =GetIPString();

}publicstringGetIPString()

{stringresult =((IPEndPoint)ClientSocket.RemoteEndPoint).Address.ToString();returnresult;

}

}

2)在服务端socket监听时:

IPEndPoint loaclEndPoint =newIPEndPoint(IPAddress.Any, Port);

SocketLister=newSocket(AddressFamily.InterNetwork, SocketType.Stream,PRotocolType.Tcp);

SocketLister.Bind(loaclEndPoint);try{

SocketLister.Listen(MaxConnection);while(IsRunning)

{

ClientSocket=SocketLister.Accept();//保存socketSession newSession =newSession(ClientSocket);lock(sessionLock)

{

sessionTable.Add(newSession.IP, newSession);

}

SocketConnection socketConnection=newSocketConnection(ClientSocket);

socketConnection.ReceiveDatagram();//接收数据}

}catch(SocketException ex)

{

}

//声明

public Hashtable sessionTable = new Hashtable ();//包括客户端会话

private object sessionLock = new object();

为了便于理解,把整个服务端socket的建立都写在上面。

3)发送数据到不同的客户端

Hashtable ht =serverSocket.sessionTable;foreach(Session sessioninht.Values)

{if(session.IP =="127.0.0.1")//example{

SocketConnection socketConnection=newSocketConnection(session.ClientSocket);stringstr ="C300010002D2";byte[] sendBytes =StrToHexByte(str);

socketConnection.Send(sendBytes);

}

}

SocketConnection类已经被使用多次,写在下面:

publicclassSocketConnection:IDisposable

{publicServerSocket Server {get;set; }publicByte[] MsgBuffer =null;privateinttotalLength =0;publicintCurrentBufferLength;privateSocket _ClientSocket =null;publicSocket ClientSock

{get{returnthis._ClientSocket; }

}publicSocketConnectionType Type {get;privateset; }#regionConstructorpublicSocketConnection(ServerSocket server, Socket sock)

{this.Server =server;this._ClientSocket =sock;this.Type =SocketConnectionType.Server;

}publicSocketConnection(Socket sock)

{this._ClientSocket =sock;this.Type =SocketConnectionType.Client;

}#endregion#regionEventspublicSocketConnectionDelegate OnConnect =null;//是否连接publicSocketConnectionDelegate OnLostConnect =null;//中断连接publicReceiveDataDelegate OnReceiveData =null;//接收数据#endregion#regionConnectpublicvoidConnect(IPAddress ip,intport)

{this.ClientSock.BeginConnect(ip, port, ConnectCallback,this.ClientSock);

}privatevoidConnectCallback(IAsyncResult ar)

{try{

Socket handler=(Socket)ar.AsyncState;

handler.EndConnect(ar);if(OnConnect !=null)

{

OnConnect(this);

}

ReceiveDatagram();

}catch(SocketException ex)

{

}

}#endregion#regionSendpublicvoidSend(stringdata)

{

Send(System.Text.Encoding.UTF8.GetBytes(data));

}publicvoidSend(byte[] byteData)

{try{intlength =byteData.Length;byte[] head =BitConverter.GetBytes(length);byte[] data =newbyte[head.Length +byteData.Length];

Array.Copy(head, data, head.Length);

Array.Copy(byteData,0, data, head.Length, byteData.Length);this.ClientSock.BeginSend(data,0, data.Length,0,newAsyncCallback(SendCallback),this.ClientSock);

}catch(SocketException ex)

{

}

}privatevoidSendCallback(IAsyncResult ar)

{try{

Socket handler=(Socket)ar.AsyncState;

handler.EndSend(ar);

}catch(SocketException ex)

{

}

}#endregion#regionReceiveDatagrampublicvoidReceiveDatagram()

{

SocketStateObject state=newSocketStateObject();

state.workSocket=_ClientSocket;

_ClientSocket.BeginReceive(state.buffer,0, SocketStateObject.BufferSize,0,newAsyncCallback(ReceiveCallback), state);

}privatevoidReceiveCallback(IAsyncResult ar)

{

SocketStateObject state=(SocketStateObject)ar.AsyncState;

Socket handler=state.workSocket;if(handler.Connected)

{try{

state.bytesRead=handler.EndReceive(ar);if(state.bytesRead >0)

{

OnDataRecivedCallback(state.buffer, state.bytesRead);

Array.Clear(state.buffer,0, state.buffer.Length);

state.bytesRead=0;

handler.BeginReceive(state.buffer,0, SocketStateObject.BufferSize,0,newAsyncCallback(ReceiveCallback), state);

}else{//if (OnDisconnect != null)//{//OnDisconnect(this);//}Dispose();

}

}catch(SocketException ex)

{ }

}

}privatevoidReceiveCallBack00(IAsyncResult ar)

{try{intREnd =_ClientSocket.EndReceive(ar);if(REnd >0)

{

OnDataRecivedCallback(MsgBuffer, REnd );

Array.Clear(MsgBuffer,0, MsgBuffer.Length);

REnd=0;

_ClientSocket.BeginReceive(MsgBuffer,0, MsgBuffer.Length,0,newAsyncCallback(ReceiveCallBack00),null);

}else{if(OnLostConnect !=null)

{

OnLostConnect(this);

}

Dispose();

}

}catch(Exception ex)

{

}

}privatevoidOnDataRecivedCallback(byte[] data,intlength)

{if(length >0)

{if(this.MsgBuffer ==null)

{byte[] bytelength =newbyte[4];

Array.Copy(data, bytelength,4);this.totalLength = BitConverter.ToInt32(bytelength,0);this.MsgBuffer =newbyte[this.totalLength];this.CurrentBufferLength =0;

}if(this.totalLength >0)

{intoffset =0;if(CurrentBufferLength ==0)

{

offset=4;

}if(length +this.CurrentBufferLength >=this.totalLength +offset)

{intlen =this.totalLength -CurrentBufferLength;

Array.Copy(data, offset,this.MsgBuffer,this.CurrentBufferLength, len);byte[] tmp =this.MsgBuffer.Clone()asbyte[];

OnReceiveData(newMessageData(this, tmp));this.MsgBuffer =null;if(length - len - offset >0)

{

tmp=newbyte[length - len -offset];

Array.Copy(data, offset+ len, tmp,0, tmp.Length);

OnDataRecivedCallback(tmp, tmp.Length);

}

}else{

Array.Copy(data, offset,this.MsgBuffer,this.CurrentBufferLength, length -offset);this.CurrentBufferLength += length -offset;

}

}else{

}

}

}publicvoidDispose()

{try{this.ClientSock.Shutdown(SocketShutdown.Both);this.ClientSock.Close();this.Server =null;//if (OnLostConnect != null)//{//OnLostConnect(this);//}}catch{

}

}#endregion}

View Code

3、处理需要发送和接收到的数据

项目需要是上位机获取数据进行逻辑处理,然后通过tcp/ip协议发送给下位机,下位机在接收到数据的同时发送确认信息到上位机。项目过程中,上位机在开发过程中需要调试其发送数据、接收数据是否成功,此处借助于USR-TCP232小工具。但是涉及到一个问题,下位机发送和接收都是byte字节数组,那么开发的上位机应该如何发送和接收数据?在.net平台下C#socket通信(上),有服务器端的发送和接收函数,发送数据时将要发送的字符串转换为byte数组,接收时再将字节数组转换为16进制字符串。如下:

//字节数组转换为16进制字符串publicstringByteToHexStr(byte[] bytes)

{stringstr ="";if(bytes !=null)

{for(inti =0; i < bytes.Length; i++)

{

str+= bytes[i].ToString("X2");

}

}returnstr;

}//字符串转换为16进制byte数组publicbyte[] StrToHexByte(stringdata)

{

data= data.Replace("","");if((data.Length %2) !=0)

{

data+="";

}byte[] bytes =newbyte[data.Length /2];for(inti =0; i < bytes.Length; i++)

{

bytes[i]= Convert .ToByte (data.Substring (i *2,2),16);

}returnbytes;

}

4、资源释放

开发项目使用平台是.net,工具vs2010,语言是C#,因为.net有垃圾回收机制,因此在实际开发中产生的托管资源都是系统自动释放完成。在做本项目时采用winform进行开发的,在此过程中发现一个问题:在关闭Form窗体是运行的系统并没有完全关闭。查找原因,应该是有资源没有被释放。而socket套接字产生的资源恰好是非托管资源,此现象表明系统中有socket资源没有被完全释放掉。因此写了一个资源释放函数:

publicvoidDispose()

{try{this.ClientSocket.Shutdown(SocketShutdown.Both);this.ClientSocket.Dispose();this.ClientSocket.Close();this.ClientSocket =null;

}catch{

}

}

上述函数的功能就是释放socket所产生的资源,调用后发现还是存在此问题,几经调试发现虽然把产生socket通道的监听客户端资源释放完毕,服务器端的serversocket并没有被释放,于是有了下一个函数:

publicvoidCloseSocket()

{if(serverSocket !=null)

{

serverSocket.SocketLister.Dispose();

serverSocket.SocketLister=null;

serverSocket.Dispose();//调用的上一个函数

serverSocket=null;

}

}

在上述函数完成后,套接字socket所产生的资源确实被释放完毕,系统在form关闭后能真正关闭。到此资源好像已经被释放掉,但紧接着新的问题产生了:

在什么时候什么地方调用释放资源的函数?

个人简单看法:

1)系统中止时调用

2)socket通道中断时调用

补充:

5、开启并保持服务监听

在socket通信中,服务端的socket监听其实是需要一直打开并且保持的,只有这样才能随时监听连接的客户端。项目中示例:

privatevoidbutton1_Click(objectsender, RoutedEventArgs e)

{

Thread thread=newThread(newThreadStart(StartServer));

thread.Start();

}publicvoidStartServer()

{intport = Convert.ToInt32(GetText(this.tbPort));stringipStr = GetText (this.tbServerIPStr);if(serverSocket ==null)

{

serverSocket=newServerSocket(port);

serverSocket.Start(ipStr);//

}else{

MessageBox.Show("监听已开启");

}

}publicvoidStart(stringipStr)

{

IPEndPoint localEndPoint=newIPEndPoint(IPAddress.Any, Port);//IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse(ipStr), Port);SocketLister =newSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

SocketLister.Bind(localEndPoint);try{

SocketLister.Listen(MaxConnection);while(IsRunning)

{

ClientSocket=SocketLister.Accept();//保存socketSession newSession =newSession(ClientSocket);lock(sessionLock)

{

sessionTable.Add(newSession.IP, newSession);

}

SocketConnection socketConnection=newSocketConnection(ClientSocket);

socketConnection.ReceiveDatagram();

}

}catch(SocketException ex)

{

}

}

解释:点击按钮开启新的线程thread,执行方法StartServer,StartServer调用方法Start,Start方法中使用死循环开启并保持监听。

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航