Inter PC connection using .net - c#

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

Related

UWP C# asynchronous Socket Server can't accept

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

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

No connection could be made because the target machine actively refused it [not similar to others]

I'm trying to learn Socket Programming, and I encountered this error while connecting to my server application.
Here's my declaration of the TcpListener in the server application:
TcpListener listener = new TcpListener(IPAddress.Loopback, 5152);
and here's my declaration of the TcpClient in my client application:
TcpClient client = new TcpClient(Dns.GetHostEntry(IPAddress.Loopback).HostName, 5152);
I have read several questions like this, and I always get the same answer: Either the server application isn't listening to the port or not running at all. But I've double-checked the Resource Monitor and cmd using netstat to see if the service is listening to the port, and it is. I've also included the service in the Firewall exceptions, so I'm not sure why I keep getting this error while trying to connect to the server app.
Any ideas?
Dns.GetHostEntry(IPAddress.Loopback).HostName returns the host name of your machine. When you pass a host name to TcpClient, it will resolve it to one or more IP addresses using Dns.GetHostAddresses(hostName). This includes the public and link-local IP addresses of your machine (e.g., 192.168.15.4), but not the loopback address (127.0.0.1).
So your client is trying to connect to any of the non-loopback addresses of your machine, while your server is listening only on the loopback address. Thus, no connection can be established.
Solution: Connect to the same end point your server is listening on.
IPEndPoint endPoint = new IPEndPoint(IPAddress.Loopback, 5152);
TcpListener listener = new TcpListener(endPoint);
TcpClient client = new TcpClient();
client.Connect(endPoint);

Categories