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());
}
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 wrote a program in C# that sends SMS using GSM LAN Modem(Coniugo). I'm using socket as client to asynchronously connect to the GSM LAN Modem. The Modem IP address is 192.186.2.1, and the port is 10001. I use this code to start the connection to the Modem
AsynchronousClient smsClient; // the clinet manager
IPAddress ipAddress;
int port;
IPEndPoint remoteEP;
// Create a TCP/IP socket.
Socket client;
private void btnStartConnect_Click(object sender, EventArgs e)
{
try
{
ipAddress = IPAddress.Parse("192.186.2.1");
port = 10001;
remoteEP = new IPEndPoint(ipAddress, port);
// Create a TCP/IP socket.
client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Connect to the remote endpoint.
client.BeginConnect(remoteEP,
new AsyncCallback(smsClient.ConnectCallback), client);
smsClient.connectDone.WaitOne();
if (client.Connected)
{
lblStatus.Text = "Client is Connected";
smsClient.Receive(client);
}
else
{
lblStatus.Text = "Client is Not Connected";
}
}
catch (Exception ex)
{
lblStatus.Text = ex.ToString();
}
}
When i run the code and start to connect to the Modem from a host in the network, the connection works without problem, but when i try run the code on another host, the connection does not work. I got the exception message
No connection could be made because the target machine actively refused it 192.186.2.1:10001.
How to connect to the GSM Modem from several hosts using socket, and avoid this exception?
You cannot. The network adapter (from Lantronix) in the Coniugo GSM modem will only accept one connection at a time.
This is necessary: The modem itself cannot deal with multiple connections. The modem actually uses serial communications - that can only handle one connection. If you allow multiple TCP connections, two users could send data at the same time. The modem cannot deal with that situation.
You have two options:
Your program only connects to the modem for as long as it takes to send the SMS. Enter all your data, hit send, and the program connects, sends the SMS, then disconnects.
You write a server program that connects to the modem. Your clients who want to send an SMS connect to the service, and it handles queueing and sending the SMS as well a keeping indiviual clients informed of the state.
I'd go for option 2.
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.