SocketException Connection failed - c#

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

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

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

How to emulate PuTTY raw connection in C#?

Please forgive my ignorance, but I believe I just need to be pointed in the right direction...
I have a device that communicates with my PC via Ethernet. I open PuTTY, put in the IP, port, and select a raw connection type. After successful connection, I can then type an "R" to receive one sample from this device. This works without issue.
I've tried many examples I've found for achieving this exact thing in C# (MSDN socket examples, blog examples) and everything so far has fallen short.
private static void StartClient()
{
// Connect to a remote device.
try
{
IPAddress ipAddress = IPAddress.Parse("192.168.0.4");
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
// Create a TCP/IP socket.
Socket client = new Socket(AddressFamily.InterNetwork,
SocketType.Raw, ProtocolType.Raw);
// Connect to the remote endpoint.
client.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
var isConnected = client.Connected;
// Send test data to the remote device.
Send(client, "S");
sendDone.WaitOne();
I modified this example (from MSDN: https://msdn.microsoft.com/en-us/library/fx6588te(v=vs.110).aspx) to include the IP and port I'm using and I attempted to change the SocketType and ProtocolType to raw. The device's datasheet does say that it ships with TCP as it's default.
I cannot establish a connection to this device at all, let alone write to and read from it.
Any advice would be greatly appreciated.

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)

Categories