我正在研究Tcp连接以在两台PC之间发送消息它在控制台应用程序案例中正常工作但在Windows窗体中,监听器停止监听它不会连续监听并抛出此异常。
例外:
Not listening. you must call the start() method before calling this method.
这是我的代码。
代码:
public static void TcpListener()
{
TcpListener server = null;
try
{
// Set the TcpListener on port 13000.
Int32 port = 8888;
IPAddress localAddr = IPAddress.Parse(Getlocalip());
// TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port);
// Start listening for client requests.
server.Start();
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
// Enter the listening loop.
Thread t = new Thread(() =>
{
while (true)
{
MessageBox.Show("Waiting for a connection... ");
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
MessageBox.Show("Server Connected!");
data = null;
// Get a stream object for reading and writing
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);
if (data == "pbrush")
{
Runprocess("start " + data);
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data + " opened successfully");
// Send back a response.
stream.Write(msg, 0, msg.Length);
}
if (data == "calc")
{
Runprocess("start " + data);
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data + " opened successfully");
// Send back a response.
stream.Write(msg, 0, msg.Length);
}
if (data.ToString() == "getmac")
{
string result = Runprocess(data, true);
byte[] msg = System.Text.Encoding.ASCII.GetBytes(result);
// Send back a response.
stream.Write(msg, 0, msg.Length);
}
if (data.ToString() == "tree d:")
{
string result = Runprocess(data, true);
byte[] msg = System.Text.Encoding.ASCII.GetBytes(result);
// Send back a response.
stream.Write(msg, 0, msg.Length);
}
if (data.ToString() == "ipconfig")
{
string result = Runprocess(data, true);
byte[] msg = System.Text.Encoding.ASCII.GetBytes(result);
// Send back a response.
stream.Write(msg, 0, msg.Length);
}
if (data.ToString() == "facebook")
{
Runprocess("start " + data);
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data + " opened successfully");
// Send back a response.
stream.Write(msg, 0, msg.Length);
}
// Process the data sent by the client.
data = data.ToUpper();
Console.WriteLine("Sent");
}
// Shutdown and end connection
client.Close();
}
});
t.IsBackground = true;
t.Start();
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
server.Stop();
}
Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
答案 0 :(得分:0)
您的问题非常简单 - 您正在调用finally块中的server.Stop()
。因为在try块中你只是启动一个线程,它会立即跳转到finally块。
您确实在等待控制台中的密钥,但是在您停止服务器之后就已经这样了。
所以,首先你应该在线程中添加一个try-catch块,因为目前你不会捕获那里发生的任何异常。
其次,在Console.Read()
之前移动server.Stop()
。