其实只要用到Socket联接,基本上就得使用Thread,是交叉使用的。
C#封装的Socket用法基本上不算很复杂,只是不知道托管之后的Socket有没有其他性能或者安全上的问题。
在C#里面能找到的最底层的操作也就是socket了,概念不做解释。
程序模型如下:
WinForm程序 : 启动端口侦听;监视Socket联接情况;定期关闭不活动的联接;
Listener:处理Socket的Accept函数,侦听新链接,建立新Thread来处理这些联接(Connection)。
Connection:处理具体的每一个联接的会话。
1:WinForm如何启动一个新的线程来启动Listener:
//start the
server
private
void btn_startServer_Click(object sender, EventArgs e)
{
//this.btn_startServer.Enabled = false;
Thread _createServer = new
Thread(new ThreadStart(WaitForConnect));
_createServer.Start();
}
//wait all
connections
private
void WaitForConnect()
{
SocketListener listener = new
SocketListener(Convert.ToInt32(this.txt_port.Text));
listener.StartListening();
}
因为侦听联接是一个循环等待的函数,所以不可能在WinForm的线程里面直接执行,不然Winform也就是无法继续任何操作了,所以才指定一个新的线程来执行这个函数,启动侦听循环。
这一个新的线程是比较简单的,基本上没有启动的参数,直接指定处理函数就可以了。
2:Listener如何启动循环侦听,并且启动新的带有参数的线程来处理Socket联接会话。
先看如何建立侦听:(StartListening函数)
IPEndPoint localEndPoint = new IPEndPoint(_ipAddress, _port);
// Create
a TCP/IP socket.
Socket
listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the
local endpoint and listen for incoming
connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(20);//20 trucks
// Start listening for connections.
while (true)
{
// here
will be suspended while waiting for a new connection.
Socket
connection = listener.Accept();
Logger.Log("Connect", connection.RemoteEndPoint.ToString());//log
it, new connection
……
}
}……
基本步骤比较简单:
建立本机的IPEndPoint对象,表示以本机为服务器,在指定端口侦听;
然后绑定到一个侦听Socket上;
进入while循环,等待新的联接;
如果有新的联接,那么建立新的socket来对应这个联接的会话。
值得注意的就是这一句联接代码:listener.Accept()。执行这一句的时候,程序就在这个地方等待,直到有新的联检请求的时候程序才会执行下一句。这是同步执行,当然也可以异步执行。
新的联接Socket建立了(Accept之后),对于这些新的socket该怎么办呢?他们依然是一个循环等待,所以依然需要建立新的Thread给这些Socket去处理会话(接收/发送消息),而这个Thread就要接收参数了。
Thread本身是不能接收参数的,为了让它可以接收参数,可以采用定义新类,添加参数作为属性的方法来解决。
因为每一个Socket是一个Connection周期,所以我定义了这么一个类public
class Connection。这个类至少有这样一个构造函数public Connection(Socket socket);
之所以这么做,就是为了把Socket参数传给这个Connection对象,然后好让Listener启动这个Thread的时候,Thread可以知道他正在处理哪一个Socket。
具体处理的方法:(在Listener的StartListening函数,ocket
connection = listener.Accept();之后)
Connection gpsCn = new
Connection(connection);
//each
socket will be wait for data. keep the connection.
Thread
thread = new Thread(new ThreadStart(gpsCn.WaitForSendData));
thread.Name = connection.RemoteEndPoint.ToString();
thread.Start();
如此一来,这个新的socket在Accept之后就在新的Thread中运行了。
3:Connection的会话处理
建立了新的Connection(也就是socket),远程就可以和这个socket进行会话了,无非就是send和receive。
现在先看看怎么写的这个线程运行的Connection.
WaitForSendData函数
while (true)
{
bytes = new byte[1024];
string data = "";
//systm will be waiting the msg of receive
envet. like Accept();
//here will be suspended while waiting for
socket income msg.
int bytesRec =
this._connection.Receive(bytes);
_lastConnectTime = DateTime.Now;