0

I am trying to make a server & client app that sends pictures over p2p to a specified host.

In this case I am using my own PC as the host and client, trying to establish a connection through my external IPs (not local).

However, I keep getting the error

system.net.sockets.socketexception the requested address is not valid in its context

This is how I try to set up the host:

    Sock = new Socket(IP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
    SocketPermission permission = new SocketPermission(NetworkAccess.Accept, 
      TransportType.Tcp, IP.ToString(), m_Port);
    //IP = IPAddress.Parse("0.0.0.0");
    IP_EndPoint = new IPEndPoint(IP, m_Port);

and in another listener method, I have a listener socket:

Socket listener = new Socket(IP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
listener.Bind(IP_EndPoint);

I had tried once with the IP set to 0.0.0.0, in which case it did start listening, but didn't receive anything.

I am using port 80. Before that I tried a different, unused port on my machine, something like 33338 etc, no luck.

What am I doing wrong here? (firewall is not the problem)

TaW
  • 53,122
  • 8
  • 69
  • 111
Alx
  • 651
  • 1
  • 9
  • 26
  • You could look for `TcpListener` class for host and `TcpClient` class for client to setup your p2p app. For testing you can setup the localhost address 127.0.0.1 (just be sure to execute server and client on same machine) and some random unused port (ex: 33338). – Lithium Aug 14 '15 at 09:52
  • Can't you just use `IPAddress.Any` or `IPAddress.IPv6Any` as the listen IP? Or is this how you're trying to prevent the use of the loopback interface? – C.Evenhuis Aug 14 '15 at 09:54
  • You might find this answer relevant: http://stackoverflow.com/a/2080495/292411 – C.Evenhuis Aug 14 '15 at 10:01
  • `TcpListener` and `TcpClient` are the simplest way if you're not at ease with sockets. [TcpListener example](https://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener(v=VS.71).aspx) and [TcpClient example](https://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient(v=VS.71).aspx) – Lithium Aug 14 '15 at 10:09
  • @Lithium I tried using TcpListener and TcpClient with local IP, which works just fine. The problem is that I want to use it over the internet, not just local PCs. When I try my external IP I get the same socket exception as described – Alx Aug 14 '15 at 12:51
  • 1
    Ok, I think your problem is not about the code. It's a network problem. you have to open the port (ex: 33338) on your router. you can also use whatsmyip to get your public IP over the internet. – Lithium Aug 14 '15 at 14:32
  • @Lithium Thank you, I was able to make it work by opening the port. I am also using the preferred IP. However, is there any way to detect available ports to use so that I dont need to manually open them? :) – Alx Aug 14 '15 at 19:05

0 Answers0