我想承认我在c#方面不是最强的,我是通过查看几个教程开发这个程序的,如果你能在你的回答中详细说明,我将不胜感激。
我希望我的 TCP 服务器始终监听传入连接,并且当新的 TCP 客户端连接时,我希望它丢弃旧连接并使用新连接。
我已经尝试实现这个答案; https://stackoverflow.com/a/19387431/3540143
但我的问题是当我模拟一个 TCP 客户端时,但不知何故上面的答案只会收到一条消息(我当前的代码接收所有发送的消息),我也尝试转换数据所以我收到它与下面的代码相同,但没有任何成功。
另外,我认为上面的代码只是接受新客户端,而不丢弃以前连接的客户端。
我当前的代码,它可以处理连接并在断开连接后搜索新连接,我想做到这一点,所以我一直在寻找新连接,如果新客户端想要连接,我将丢弃电流让新的通过
public class TCPListener
{
public static void Listener()
{
TcpListener server = null;
try
{
// Set the TcpListener on carPort.
Int32 port = 5002;
// TcpListener server = new TcpListener(port);
server = new TcpListener(IPAddress.Any, port);
// Start listening for client requests.
server.Start();
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
// Enter the listening loop.
while (true)
{
Console.Write("Waiting for a connection... ");
// Perform a blocking call to accept requests.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
// Get a stream object for reading
NetworkStream stream = client.GetStream();
int i;
// Loop to receive all the data sent by the client.
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
}
// Shutdown and end connection
Console.WriteLine("Client close");
client.Close();
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
Console.WriteLine("Stop listening for new clients.");
server.Stop();
}
}
}
编辑:如果有人需要我的问题的解决方案,那么这就是我应用 Cecilio Pardo 建议时代码的样子,效果非常好!
public class TCPListener
{
Form form = new Form();
public static void Listener()
{
TcpListener server = null;
try
{
// Set the TcpListener on carPort.
Int32 port = 5002;
// TcpListener server = new TcpListener(port);
server = new TcpListener(IPAddress.Any, port);
// Start listening for client requests.
server.Start();
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
// Enter the listening loop.
while (true)
{
Console.Write("Waiting for a connection... ");
// Perform a blocking call to accept requests.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
data = null;
bool dataAvailable = false;
// Get a stream object for reading
NetworkStream stream = client.GetStream();
int i;
while (true)
{
if (!dataAvailable)
{
dataAvailable = stream.DataAvailable;
//Console.WriteLine("Data Available: "+dataAvailable);
if (server.Pending())
{
Console.WriteLine("found new client");
break;
}
}
if (dataAvailable)
{
// Loop to receive all the data sent by the client.
i = stream.Read(bytes, 0, bytes.Length);
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
dataAvailable = false;
}
if (server.Pending())
{
Console.WriteLine("found new client");
break;
}
}
Console.WriteLine("Client close");
// Shutdown and end connection
client.Close();
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
Console.WriteLine("Stop listening for new clients.");
server.Stop();
}
}
}
最佳答案
当您处于内部 while
循环中时,您无法检查新连接。所以你必须在循环中添加这样的东西:
if ( server.Pending() ) break;
一旦另一个连接正在等待,这将退出您的循环。
另一个问题是 stream.Read
会阻塞,直到有一些数据可用,所以如果事件连接空闲,则不会处理新连接。所以你必须改变它,除非有一些数据,否则不要调用 Read
,使用 stream.DataAvailable
关于c# - 如何将 TCPListener 设置为始终监听以及新连接何时丢弃当前连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35322605/