C# 在我的应用程序关闭后,端口仍被监听是不是很糟糕?
我的应用程序在某个端口侦听套接字消息。我可以看到它正在通过命令行中的“netstat-a”进行监听 当我关闭应用程序时,当我重新运行“netstat-a”时,计算机仍在侦听该端口 这是个问题吗C# 在我的应用程序关闭后,端口仍被监听是不是很糟糕?,c#,sockets,socketserver,C#,Sockets,Socketserver,我的应用程序在某个端口侦听套接字消息。我可以看到它正在通过命令行中的“netstat-a”进行监听 当我关闭应用程序时,当我重新运行“netstat-a”时,计算机仍在侦听该端口 这是个问题吗 看起来可能是这样,因为当我随后再次启动应用程序时,它不光彩地崩溃了 我怎样才能使倾听停止 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System
看起来可能是这样,因为当我随后再次启动应用程序时,它不光彩地崩溃了 我怎样才能使倾听停止
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Diagnostics;
namespace testSocketSendAndReceive_Nutshell
{
public partial class Form1 : Form
{
string sJerrysIPAddr = "10.24.93.110";
string sMyIPAddr = "10.24.93.128";
string sThisAppFileName = string.Empty;
bool bThisInstanceFunctionsAsServer = false;
internal static Form1 MainSocketPairForm = null;
public Form1()
{
InitializeComponent();
MainSocketPairForm = this;
}
private void Form1_Load(object sender, EventArgs e)
{
sThisAppFileName = System.Diagnostics.Process.GetCurrentProcess().ProcessName; // This provides just the app name, appending ".vshost" but NOT ".exe" (testSocketSendAndReceive_Nutshell.vshost)
lblFileName.Text = sThisAppFileName;
// Client and Server code are here combined in one app; however, we want each instance to run as
// just one or the other, so (the .exe functioning as a Server should be renamed with the subString
// "Server" somewhere in the filename):
bThisInstanceFunctionsAsServer = sThisAppFileName.Contains("Server");
if (bThisInstanceFunctionsAsServer)
{
new Thread(Server).Start(); // Run server method concurrently.
Thread.Sleep(500); // Give server time to start.
}
btnSendMsg.Visible = !bThisInstanceFunctionsAsServer;
textBox1.Visible = !bThisInstanceFunctionsAsServer;
}
static void Client()
{
using (TcpClient client = new TcpClient(Form1.MainSocketPairForm.sJerrysIPAddr, 51111)) // err here second time around
using (NetworkStream n = client.GetStream())
{
BinaryWriter w = new BinaryWriter(n);
w.Write(Form1.MainSocketPairForm.textBox1.Text.ToString());
w.Flush();
Form1.MainSocketPairForm.label1.Text = new BinaryReader(n).ReadString();
}
}
static void Server()
{
TcpListener listener = new TcpListener(IPAddress.Any, 51111);
listener.Start();
var shouldExit = false;
while (!shouldExit)
using (TcpClient c = listener.AcceptTcpClient())
{
using (NetworkStream n = c.GetStream())
{
string msg = new BinaryReader(n).ReadString();
if (msg == "exit")
// Client told us to exit...
shouldExit = true;
BinaryWriter w = new BinaryWriter(n);
w.Write(msg + " back atcha!");
w.Flush(); // Must call Flush because we're not disposing the writer.
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Client();
}
private void button2_Click(object sender, EventArgs e)
{
Close();
}
}
}
您的应用程序可能没有实际退出(请检查.exe的任务管理器“进程”选项卡) 您可能正试图通过关闭命令窗口来关闭应用程序。因为您的服务器线程不是一个线程,所以它将继续运行。试试这个家伙在Form_Load中的表现:
if (bThisInstanceFunctionsAsServer)
{
var serverThread = new Thread(Server);
serverThread.IsBackground = true; // Make sure the server thread doesn't keep the app running in the background
serverThread.Start(); // Run server method concurrently.
Thread.Sleep(500); // Give server time to start.
}
您的应用程序可能没有实际退出(请检查.exe的任务管理器“进程”选项卡) 您可能正试图通过关闭命令窗口来关闭应用程序。因为您的服务器线程不是一个线程,所以它将继续运行。试试这个家伙在Form_Load中的表现:
if (bThisInstanceFunctionsAsServer)
{
var serverThread = new Thread(Server);
serverThread.IsBackground = true; // Make sure the server thread doesn't keep the app running in the background
serverThread.Start(); // Run server method concurrently.
Thread.Sleep(500); // Give server time to start.
}
你在监听哪个端口例如。。当它坠毁的时候你怎么这么肯定是因为港口。。你能粘贴一个有时会崩溃的代码示例吗?是的,这是一个问题,也是你提到的原因。当您第二次启动应用程序时,该ip/端口已在使用中。您正在调用Dispose()、Close()和任何其他应该调用的清理方法吗?能否发布一些代码以便我们帮助您确定原因?您的应用程序是否实际终止?如果进程仍然挂起(您可以与任务管理器进行检查),您可能有一个阻塞的线程挂起了应用程序的终止。“看起来可能是这样,就像我随后再次启动应用程序时,它不光彩地崩溃了。”您对调试代码中崩溃的地方做了什么?听起来这里可能有多个bug。1) 套接字/进程未正确关闭。和2)初始化和绑定套接字的一般故障会导致异常或程序崩溃。您正在侦听哪个端口..例如。。当它坠毁的时候你怎么这么肯定是因为港口。。你能粘贴一个有时会崩溃的代码示例吗?是的,这是一个问题,也是你提到的原因。当您第二次启动应用程序时,该ip/端口已在使用中。您正在调用Dispose()、Close()和任何其他应该调用的清理方法吗?能否发布一些代码以便我们帮助您确定原因?您的应用程序是否实际终止?如果进程仍然挂起(您可以与任务管理器进行检查),您可能有一个阻塞的线程挂起了应用程序的终止。“看起来可能是这样,就像我随后再次启动应用程序时,它不光彩地崩溃了。”您对调试代码中崩溃的地方做了什么?听起来这里可能有多个bug。1) 套接字/进程未正确关闭。初始化和绑定套接字的一般失败会导致异常或程序崩溃。请始终质疑您的假设。这就是我最喜欢这个答案的原因。当事情失败,一切似乎都是对的时候,你所假设的事情正在发生,或者被正确地设置,或者以其他方式是它应该发生的——不是。总是质疑你的假设。这就是我最喜欢这个答案的原因。当事情失败,一切似乎都正常时,你所假设的事情正在发生,或者被正确设置,或者以其他方式是它应该发生的-不是。