Why would I get an exception on a working server only once its running on host domain?


I have a client/server program that runs great on many private machines. However, once I install the server on a host domain of Amazon Lightsail, Windows Server 2016, I get an exception.

I am using UDP socket and trying to bind with the static IP I have but I am getting this 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 AP_Server_Side.Program.Main(String[] args)

I have tried many different ports as well as zero.

My code:

  Socket _reciveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  IPAddress _localAddress = IPAddress.Parse("");
  IPEndPoint _localip = new IPEndPoint(_localAddress, 0);
  Console.WriteLine("local Address : " + _localip.ToString());
  IPEndPoint _newend = new IPEndPoint(IPAddress.Any, 0);
  EndPoint _escoler = (EndPoint)_newend;

    byte[] _bufferrecived = new byte[1024];
    byte[] _sendBuffer = new byte[1024];
    int _recivefrom;
    string _msgRecived = string.Empty;
    int _userint = 1;


I doubt the problem is in the code because as I said the server has been tested and runs great on more than one machine.

I'm guessing its something with Windows or server setting, I turned firewall off and tried inbound all UDP reference.

asked on Stack Overflow Nov 12, 2019 by Buffamonteezi • edited Nov 13, 2019 by John Rotenstein

1 Answer


Sounds like an issue with the environment your deploying too. From the sound of it lightsail is meant for simple deployments for apps that just listen for HTTP traffic (TCP). And my best guess is that address. Any or localhost will probably work better. Unfortunately you can't bind to addresses willy nilly, and it depends on you network interface's IP range.

It could also be related to the network settings for your particular lightsail instance. You might want to check this out: Lightsail ports and firewalls

Also this might be of use in your investigations. Investigating lightsail port information

Also I would recommend learning more about networking and IP addresses before going and doing this kind of low-level socket programming!

answered on Stack Overflow Nov 12, 2019 by n234

User contributions licensed under CC BY-SA 3.0