关于TCP通信的问题,想请大家指点
首先,问题是这样的,我从网上COPY的代码已经可以实现客户端发送数据,服务器接收数据的基本功能,但是跟我理解的有点不同,我理解的TCP通信应该是服务器监听一个端口,一台客户端上线后,连接就建议,并与之通信,而监听的进程还在继续监听,但我总感觉实际不是这样,于是,有以下几个问题:
1.TcpListener可以用来监听,Socket也可以用来监听,两者有何区别。
2.如果用TcpListener监听,然后由Socket去处理,是不是TcpListener监听到之后就不在管了,剩下通信就是在Socket与客户端进行,好像打电话,TcpListener负责拨号,拨通了之后由Socket进行对话,TcpListener再去等待下一拨号。
求大家帮助,第一次接触TCP,问题可能有点弱,呵呵
从网上COPY一段代码如下:
1.窗体加载时启动监听线程:
this.tcpListener = new TcpListener(IPAddress.Any, 8899);
this.listenThread = new Thread(new ThreadStart(ListenForClients));
this.listenThread.Start();
2.监听线程里循环监听:
private void ListenForClients()
{
this.tcpListener.Start();
while (true)
{
//blocks until a client has connected to the server
TcpClient client = this.tcpListener.AcceptTcpClient();
//create a thread to handle communication
//with connected client
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
clientThread.Start(client);
}
}
3.处理通信内容:
private void HandleClientComm(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
byte[] message = new byte[4096];
int bytesRead;
while (true)
{
bytesRead = 0;
try
{
//blocks until a client sends a message
&