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)
Related
I am trying to set up a basic client-server connection on Linux between two PCs connected on the same network.
I successfully connected my client to my server on my local machine.
But when I give my client the server-PC's ip thanks to the 'ifconfig' command I have : Connection refused.
I already opened the port I use thanks to the command : sudo firewall-cmd --add-port=4242/tcp
Here is how I set the socket connection with "xxx.xxx.xxx.xxx" the IP address that I get from the 'ifconfig' command (maybe it's not the proper way to do it ?).
// Establish the remote endpoint
// for the socket.
IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddr = IPAddress.Parse("xxx.xxx.xxx.xxx");
IPEndPoint localEndPoint = new IPEndPoint(ipAddr, 4242);
// Creation TCP/IP Socket using
Socket sender = new Socket(ipAddr.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
Here is the exception output:
SocketException : System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (111): Connection refused xxx.xxx.xxx.xxx:4242
My Socket Server can't receive any connection requests.
Tried to connect to the server with simple Socket Service => A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 127.0.0.1:1337.
Enabled private internet and internet (client and server) in appxmanifest.
Service:
IPEndPoint serviceEndPoint = new IPEndPoint(IPAddress.Loopback, 1337);
Socket _serviceSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_serviceSocket.Bind(serviceEndPoint);
_serviceSocket.Listen(254);
Socket clientSocket = null;
await _serviceSocket.AcceptAsync(clientSocket);
Client:
IPAddress _address = IPAddress.Loopback;
IPEndPoint _ep = = new IPEndPoint(_address, 1337);
Socket _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_clientSocket.Connect(_ep);
So I tried to debug it on my Raspberry Pi and it worked. I don't know why this code doesn't work on my PC.
According to the docs, two UWP apps cannot communicate on the local host due to (what they call) network isolation. I have no experience with that but the documentation may show some configuration changes needed...
I'm writing a server/client application. my server works great on local ip addresses and i am able to test all of its features using 127.0.0.1 as ip address and communicate with my server.
Today i used my public ip address as ip address in my server application and i configured my port forwarding and checks it with common tools on network and everyone says my port is open an actually i get port checker site packets on my server that shows everything works fine.
but when i want to connect from my client application that it is written in c#(also my server is in c#) i get exception in Socket.Connect function that code is below "No connection could be made because the target machine actively refused it" :
clientSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
Console.WriteLine("Connectiong to server");
clientSocket.Connect("151.243.130.245", 1892);
and here is my listen method on my server :
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
for (int i = 0; i < MAX_CLIENTS; ++i)
{
clients[i] = new Client();
}
serverSocket.Bind(new IPEndPoint(IPAddress.Any, 1892));
serverSocket.Listen(MAX_CLIENTS);
serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
i have to mention that i tested port checker sites from my friend home and it did send a packet to my server.
also i have a wireless usb adaptor connected to my computer and i am connected with it to my mode.
i did search all the internet and everyone said that in this error common issues are you are not listening on that port or you are blocked by a firewall program but i have to say that i did both of them,it means i used -netstat -anb and it shows i am listening on port 1892 and also i configure firewall to let any connection goes through it but still no luck :(
any helps will greatly be appreciated.
I use the sample code on MSDN but it cannot work.
Below is code:
IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddr = ipHost.AddressList[2];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11000);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.Connect(ipEndPoint);
You will need to also have an Active TcpListener on your local machine (i'm guessing by using Dns.GetHostname()).
Instead of relying on DNS when trying to connect back to yourself, you can use IPAddress.Loopback
The code that MSDN provided is for example. This means it may not work in all circumstances.
The problem you facing is, that there is no software listening on the port 11000. (for a client to connect on a port, there should be a server listening.) irl same like: If you (client) phone your friend, but your friend(server) isn't home(listening) to pick up the homephone, there will be no conversation. ;-)
I have created a windows service socket programme to lisen on specific port and accept the client request. It works fine.
protected override void OnStart(string[] args)
{
//Lisetns only on port 8030
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 8030);
//Defines the kind of socket we want :TCP
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//Bind the socket to the local end point(associate the socket to localendpoint)
serverSocket.Bind(ipEndPoint);
//listen for incoming connection attempt
// Start listening, only allow 10 connection to queue at the same time
serverSocket.Listen(10);
Socket handler = serverSocket.Accept();
}
But I need the service programme to listen on multiple port and accept the client request on any available port.
So I enhanced the application to bind to port 0(zero), so that it can accept the request on any available port.
But then I got the error 10061
No connection could be made because the target machine actively refused it.
I am unable to know whats the reason of getting this error.
Can anybody please suggest the way to enhance the code to accept the request on any port.
But the client need to send request to connect to specific port. e.g client1 should connect to port 8030, client2 should connect to port 8031.
So I enhanced the application to bind to port 0(zero), so that it can accept the request on any available port.
Wrong. 0 means that the OS should assign a port. A server can only listen at one port at a time. The listen socket just accepts new connections.
The new connection will have the same local port, but the combination of Source (ip/port) and destination (ip/port) in the IP header is used to identify the connection. That's why the same listen socket can accept multiple clients.
UDP got support for broadcasts if that's what you are looking for.
Update:
A very simplified example
Socket client1 = serverSocket.Accept(); // blocks until one connects
Socket client2 = serverSocket.Accept(); // same here
var buffer = Encoding.ASCII.GetBytes("HEllo world!");
client1.Send(buffer, 0, buffer.Count); //sending to client 1
client2.Send(buffer, 0, buffer.Count); //sending to client 2
Simply keep calling Accept for each client you want to accept. I usually use the asynchronous methods (Begin/EndXXX) to avoid blocking.