c# - tcplistener 没有启动

标签 c# .net tcplistener

我在 Windows 窗体应用程序中使用 TcpListener;它有一个启动 TcpListener 的“开始”按钮。但是,当我单击开始按钮时,会抛出以下异常:

The requested address is not valid in its context

这是我用来启动监听器的代码:

IPAddress FriendIp = IPAddress.Parse(IpAddress);
server = new TcpListener(FriendIp, PortNum);
server.Start();

这是异常的调用堆栈:

System.dll!System.Net.Sockets.Socket.DoBind(System.Net.EndPoint endPointSnapshot, System.Net.SocketAddress socketAddress) + 0x9f bytes  
System.dll!System.Net.Sockets.Socket.Bind(System.Net.EndPoint localEP) + 0xd2 bytes 
System.dll!System.Net.Sockets.TcpListener.Start(int backlog) + 0x91 bytes   
System.dll!System.Net.Sockets.TcpListener.Start() + 0xa bytes   

BlueBall.exe!BlueBall.Chat_Window.Chat_Window(string ipadd, int port, bool serverclientjob) Line 46 + 0x10 bytes C# BlueBall.exe!BlueBall.BlueBall.Main_ListBox_DoubleClick(object sender, System.EventArgs e) Line 221 + 0x29 bytes C# System.Windows.Forms.dll!System.Windows.Forms.Control.OnDoubleClick(System.EventArgs e) + 0x7f bytes
System.Windows.Forms.dll!System.Windows.Forms.ListBox.WndProc(ref System.Windows.Forms.Message m) + 0x2c9 bytes System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) + 0x13 bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m) + 0x31 bytes
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam) + 0x64 bytes [Native to Managed Transition]
[Managed to Native Transition]
System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(System.IntPtr dwComponentID, int reason, int pvLoopData) + 0x287 bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason, System.Windows.Forms.ApplicationContext context) + 0x16c bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) + 0x61 bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.Form mainForm) + 0x31 bytes
BlueBall.exe!BlueBall.Program.Main() Line 18 + 0x1d bytes C# [Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) + 0x6d bytes
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() + 0x2a bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 0x63 bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool ignoreSyncCtx) + 0xb0 bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x2c bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 bytes
[Native to Managed Transition]

最佳答案

Harold 和 Spender 是正确的,TcpListener 只能绑定(bind)到它被实例化的机器上的本地 IP 地址。

关于c# - tcplistener 没有启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7795774/

相关文章:

c# - 读取 app.config 的 connectionStrings 元素

c# - 为什么在 XAML 中绑定(bind) MainWindow datacontext 无法像在代码隐藏中使用 this.datacontext=this 绑定(bind)一样?

.net - 并行时 WCF 服务变慢

c# - 协方差如何比多态性更酷……而不是多余的?

c# - 客户端/服务器发送大文件

c# - TCPListener C#/服务器上的多个客户端发送数据

android - ANDROID 向 C# TCPListener 发送的图像无效

c# - NHibernate ICriteria - 排序是否允许空值?

c# - 如何使用 C# 从 DIV 中抓取图像

.net - 在 VB.net 中实现这样的工具提示?