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

0

I wrote a server and a client program in C# using a TcpListener and a TcpClient and I want them to exchange a string. 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:

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(); 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:

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. Thanks in advance!

c#
sockets
client-server
tcplistener
socketexception
asked on Stack Overflow Dec 5, 2020 by justcivah • edited Dec 15, 2020 by justcivah

1 Answer

0

Is the public IP address really available at your Windows machine or only on the router?

Check if the public address shows up when running ipconfig in the Command line window. 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. Such an IP address typically starts with 10. or 192..

Try binding your server to the address 0.0.0.0. This way, it will listen to all your network cards (WiFi, Ethernet) without the need for any configuration.

answered on Stack Overflow Dec 5, 2020 by Gogowitsch

User contributions licensed under CC BY-SA 3.0