UWP C# asynchronous Socket Server can't accept - c#

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...

Related

Inter PC connection using .net

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

Code for creating sockets in c#

I am trying to create a server and client network using three computers inside my LAN. I have read the following post on how to create a server-client communication. I am wondering what those lines of code perform in the below example:
// Establish the local endpoint for the socket.
// The DNS name of the computer
// running the listener is "host.contoso.com".
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
// Create a TCP/IP socket.
Socket listener = new
Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp );
I am not familiar with network stuff. I do not understanding if those lines create a socket with specific IP and port which is waiting a response from clients. Is there a correspondent example for client socket?
EDIT: I have the following setup. Three computers with three kinect 2 which run a c# project which capture the kinect stream and stores it to the hard disk. I want when in the server is pressed record to simultanesouly record the stream from all kinects.
EDIT2: I am trying to run the client version and I am receiving the following error:
In the link you posted there is also an Asynchronous Client Socket example. Is that not what you are looking for?
// Connect to a remote device.
try {
// Establish the remote endpoint for the socket.
// The name of the
// remote device is "host.contoso.com".
IPHostEntry ipHostInfo = Dns.Resolve("host.contoso.com");
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
// Create a TCP/IP socket.
Socket client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Connect to the remote endpoint.
client.BeginConnect( remoteEP, new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
// Send test data to the remote device.
Send(client,"This is a test<EOF>");
sendDone.WaitOne();
// Receive the response from the remote device.
Receive(client);
receiveDone.WaitOne();
// Write the response to the console.
Console.WriteLine("Response received : {0}", response);
// Release the socket.
client.Shutdown(SocketShutdown.Both);
client.Close();
} catch (Exception e) {
Console.WriteLine(e.ToString());
}

Socket over internet for P2P

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)

SocketException Connection failed

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. ;-)

TCP socket error 10061

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.

Categories