本文介绍了收听TCP客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个TCP客户端,我需要监听并发送响应。使用c#实现这一目标的最佳方法是什么? 我过去做过一些事情,但我是客户端,在这种情况下我需要成为服务器。我一直在寻找通过给定端口连接到客户端并监听/发送数据的方法......但没有结果...... 这是我的代码到目前为止....
private void ListeningThread() // 让我们在另一个线程中监听!! { int port = 12345 ; // 根据需要更改 this .listener = new TcpListener(IPAddress.Any,port); 尝试 { 此 .listener。开始(); } catch (例外e){MessageBox.Show( 无法绑定到端口 + port + - > + e.Message); 返回; } while ( true ) { Thread.Sleep( 30 ); } }现在我需要在while cicle中接收数据并将其转换为字符串...
解决方案我猜你在谈论TcpListener类 [ ^ ]。 也可参考 - 用C#编写的TCP / IP服务器 [ ^ ]。
I have a TCP client that I need to Listen and send responses. What’s the best way to achieve this with c#? I’ve done something in the past but I was the client, in this situation I need to be the server. I’ve been searching a way to connect to the client through a given port and listen/send data….but with no results…. Here's my code so far....
private void ListeningThread() // let's listen in another thread instead!! { int port = 12345; // change as required this.listener = new TcpListener(IPAddress.Any, port); try { this.listener.Start(); } catch (Exception e) { MessageBox.Show("couldn't bind to port " + port + " -> " + e.Message); return; } while (true) { Thread.Sleep(30); } }Now I need, in the while cicle, to receive data and convert it to a string...
解决方案 I guess you are talking about TcpListener Class[^]. Also refer - A TCP/IP Server written in C#[^].更多推荐
收听TCP客户端
发布评论