简体   繁体   中英

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. The problem is though, when tying in the IP address the message will come from returns this when not from localhost.

Message=The requested address is not valid in its context. Source=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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM