简体   繁体   English

获取请求的地址在其上下文中无效。 在我的聊天应用程序中

[英]Getting The requested address is not valid in its context. in my chat application

so I am coding a chat app in C# which is a console app where the user types the IPV4 address of the recipient.所以我在 C# 中编写了一个聊天应用程序,这是一个控制台应用程序,用户在其中键入收件人的 IPV4 地址。 The problem is though, when tying in the IP address the message will come from returns this when not from localhost.问题是,当绑定 IP 地址时,消息将来自而不是来自本地主机时返回此地址。

Message=The requested address is not valid in its context. Message=请求的地址在其上下文中无效。 Source=System.Net.Sockets来源=System.Net.Sockets

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

namespace SimpleTcpSrvr
{
class Program
{
    static void Main(string[] args)
    {
        Console.OutputEncoding = Encoding.UTF8;

        int recv;

        byte[] data = new byte[1024];
        Console.WriteLine("You will need to recieve the password.txt file from your chat buddy.");
        Console.WriteLine("");
        Console.WriteLine("Please enter the IPv4 address of the buddy you are sending messages to.");
        string rip = Convert.ToString(Console.ReadLine());

        IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(rip), 8080);

        Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        newsock.Bind(ipep);

// This is where the error occurs. // 这是发生错误的地方。

        newsock.Listen(10);

        Console.WriteLine("Waiting for a client...");

        Socket client = newsock.Accept();

        IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;

        Console.WriteLine("Connected with {0} at port {1}", clientep.Address, clientep.Port);

        string welcome = "Welcome to encrypted chat. Use ByteOribtPrivacyCannon to  decrypt incoming messages.";

        data = Encoding.UTF8.GetBytes(welcome);

        client.Send(data, data.Length, SocketFlags.None);

        string input;

        while (true)
        {

            data = new byte[1024];

            recv = client.Receive(data);

            if (recv == 0)

                break;

            Console.WriteLine("Client: " + Encoding.UTF8.GetString(data, 0, recv));

            input = Console.ReadLine();

            Console.WriteLine("You: " + input);

            client.Send(Encoding.UTF8.GetBytes(input));
        }

        Console.WriteLine("Disconnected from {0}", clientep.Address);

        client.Close();

        newsock.Close();

        Console.ReadLine();

    }
}

} }

Why might this be happening?为什么会发生这种情况? Thanks very much.非常感谢。

In fact, your code acts as a 'Server'.事实上,您的代码充当“服务器”。 You need to listen to an address available on your computer, but you are binding the IP address of another host.您需要收听计算机上可用的地址,但您正在绑定另一台主机的 IP 地址。

The Server can only accept connections, it cannot choose Clients.服务器只能接受连接,不能选择客户端。 But the Client can choose the Server.但是客户端可以选择服务器。 You can try the following code to solve it.您可以尝试以下代码来解决它。

Server:服务器:

IPEndPoint ipep = new IPEndPoint(IPAddress.Any,8080);

Client:客户:

IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("172.xx.xx.xxx"),8080);
//172.xx.xx.xxx is the IPV4 address of your server computer

暂无
暂无

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

相关问题 Socket.bind()在Win7上返回“请求的地址在其上下文中无效。” - Socket.bind() returns “The requested address is not valid in its context.” on Win7 套接字错误请求的地址在其上下文中无效 - Socket Error The requested address is not valid in its context “请求的地址在其上下文中无效” SocketException - “The requested address is not valid in its context” SocketException 为什么我的服务器 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 WCF Tcp绑定“请求的地址在其上下文中无效”错误 - WCF Tcp Binding “The requested address is not valid in its context” Error 当我尝试侦听端口时,请求的地址在其上下文中无效 - The requested address is not valid in its context when I try to listen a port HttpListener:请求的地址在此上下文中无效 - HttpListener: The requested address is not valid in this context SocketException:请求的地址在上下文中无效 - SocketException: The requested address is not valid in the context 32feet .NET api拒绝连接“请求的地址在其上下文中无效” - 32feet .NET api refusing to connect “The requested address is not valid in its context” 面临错误 tcp 侦听器 SocketException:{0}System.Net.Sockets.SocketException (0x80004005):请求的地址在其上下文中无效 - Facing error tcp listener SocketException: {0}System.Net.Sockets.SocketException (0x80004005): The requested address is not valid in its context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM