简体   繁体   English

为什么我的服务器 TcpListener 使用本地地址而不使用公共地址? - 例外:请求的地址在其上下文中无效

[英]Why is my Server TcpListener Working with Local Address but not with Public Address? - Exception: The requested address is not valid in its context

I wrote a server and a client program in C# using a TcpListener and a TcpClient and I want them to exchange a string.我使用 TcpListener 和 TcpClient 在 C# 中编写了一个服务器和一个客户端程序,我希望它们交换一个字符串。 It works if both PCs are connected to the same network and if I use the local address of the server, but when the PCs are connected to a different network and I use a public address it gives me the following error:如果两台 PC 都连接到同一个网络并且我使用服务器的本地地址,则它可以工作,但是当 PC 连接到不同的网络并且我使用公共地址时,它会给我以下错误:

Exception: System.Net.Sockets.SocketException (0x80004005): The requested address is not valid in its context
   at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.Sockets.Socket.Bind(EndPoint localEP)
   at System.Net.Sockets.TcpListener.Start(Int32 backlog)
   at System.Net.Sockets.TcpListener.Start()
   at Server___Network_Class.Program.Main(String[] args) in D:\Server - Network Class\Program.cs:line 18

This error refers to line 18, which is myList.Start();此错误涉及第 18 行,即 myList.Start(); but I don't know why it throws this exception.但我不知道为什么它会抛出这个异常。 I opened my router port and I set up correctly the Windows Firewall... Here's the Server and Client code I wrote:我打开了路由器端口,并正确设置了 Windows 防火墙……这是我编写的服务器和客户端代码:

Server服务器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace Server___Network_Class {
    class Program {
        static void Main(string[] args) {
            try {
                IPAddress ipAddress = IPAddress.Parse("146.241.31.193"); //That's the public IP

                //Initialize the listener
                TcpListener myList = new TcpListener(ipAddress, 51328);
                //Start listening the selected port
                myList.Start();

                
                Socket socket = myList.AcceptSocket();

                
                ASCIIEncoding asen = new ASCIIEncoding();
                socket.Send(asen.GetBytes("Can you read this?"));

                
                socket.Close();
                myList.Stop();

            }
            catch (Exception e) {
                Console.WriteLine("Exception: " + e);

            }

            Console.ReadKey();
        }
    }
}

Client客户

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace Client___Class_Network {
    class Program {
        static void Main(string[] args) {
            try {
                //Inizializzo il client
                TcpClient client = new TcpClient();

                //Cerco di connettermi al server
                client.Connect("146.241.31.193", 51328); //IP and Port i want to connect to
                
                //Stream sul quale inviare e ricevere i dati dal server
                NetworkStream stream = client.GetStream();

                Byte[] bytesRecived = new Byte[256];
                int totBytesRecived = stream.Read(bytesRecived, 0, bytesRecived.Length);
                String stringData = System.Text.Encoding.ASCII.GetString(bytesRecived, 0, totBytesRecived);


                Console.Write(stringData);

                client.Close();
            }

            catch (Exception e) {
                Console.WriteLine("Exception: " + e);
            }

            Console.ReadKey();
        }
    }
}

Only the server program throws that exception, the client one seems working file...只有服务器程序抛出该异常,客户端似乎工作文件......

Could you please help me out with this problem?你能帮我解决这个问题吗? I have (almost) searched all over the web but I couldn't find any useful answer.我(几乎)搜索了整个 web,但找不到任何有用的答案。 Thanks in advance!提前致谢!

Is the public IP address really available at your Windows machine or only on the router?公共 IP 地址真的在您的 Windows 机器上可用还是仅在路由器上可用?

Check if the public address shows up when running ipconfig in the Command line window.检查在命令行 window 中运行ipconfig时是否显示公共地址。 My guess is that you need to set up NAT port forwarding in the router and set the application to listen to the IP address your router gave to your Windows machine.我的猜测是您需要在路由器中设置 NAT 端口转发并将应用程序设置为侦听您的路由器为您的 Windows 机器提供的 IP 地址。 Such an IP address typically starts with 10. or 192. .这样的 IP 地址通常以10.192.开头。

Try binding your server to the address 0.0.0.0 .尝试将您的服务器绑定到地址0.0.0.0 This way, it will listen to all your network cards (WiFi, Ethernet) without the need for any configuration.这样,它无需任何配置即可监听您所有的网卡(WiFi、以太网)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取请求的地址在其上下文中无效。 在我的聊天应用程序中 - Getting The requested address is not valid in its context. in my chat application 套接字错误请求的地址在其上下文中无效 - Socket Error The requested address is not valid in its context “请求的地址在其上下文中无效” SocketException - “The requested address is not valid in its context” SocketException HttpListener:请求的地址在此上下文中无效 - HttpListener: The requested address is not valid in this context SocketException:请求的地址在上下文中无效 - SocketException: The requested address is not valid in the context 当我尝试侦听端口时,请求的地址在其上下文中无效 - The requested address is not valid in its context when I try to listen a port WCF Tcp绑定“请求的地址在其上下文中无效”错误 - WCF Tcp Binding “The requested address is not valid in its context” Error 32feet .NET api拒绝连接“请求的地址在其上下文中无效” - 32feet .NET api refusing to connect “The requested address is not valid in its context” Socket.bind()在Win7上返回“请求的地址在其上下文中无效。” - Socket.bind() returns “The requested address is not valid in its context.” on Win7 为什么Google会捕获与我的TcpListener不同的IP地址? - Why does google captures a different IP address than my TcpListener?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM