I want to get all clients which are connected to a server port (i.e. port 80).
If I connect to the remote port using:
IPEndPoint endPoint = new IPEndPoint(ip, port);
Socket tcpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
tcpSocket.Connect(endPoint);
I get as LocalEndPoint the ipaddress of the current client. How can I get the ip addresses of all clients which are connected to this port on the remote machine (or at least the count of connected clients)? I don't want to invoke the remote system.
Thanks for any help.
Harald
You can't. That's how sockets work and security is done. Once you have a connection between server and client, you can't get information about other clients.
You can change remote server code and alter protocol to add handling for your request so server will return number of connected clients. But that will require you to support that in protocol and alter TCP server implementation. Event that part can be tricky (calculate number of connected clients), because of half-closed connections, timeouts etc.
Related
I am trying to implement NAT hole punching for UDP datagrams for a simple Unity game. I have a public-available server (example.com) which is listening on both TCP and UDP on port 8995.
The idea for algorithm follows:
Client connects on TCP/8995 and specify the details of the game it want to host
Server reply with token
Client starts UDP socket via UdpClient class in C#
Client sends UDP datagram to server from this socket with the token. The server learns NAT Ip/Port of the client
When another client wants to connect, it asks server for the Ip/Port of the client it want to connect
The problem I have is step 4. I setup a client at my PC (192.168.1.1 port 9589) which is behind NAT and send my UDP packet to server. I received it via:
_listener = new UdpClient(_settings.EndPoint);
IPEndPoint source = new IPEndPoint(IPAddress.Any, 0);
var datagram = _listener.Receive(ref source);
_logger.LogDebug("Recevied UDP handshake from {EndPoint}", source);
The log I am getting here is Recevied UDP handshake from x.x.x.x:9589 where x.x.x.x is my correct public IP of the client.
Obviously the port I am getting in the log is not a temporary port on NAT, but a local port of the client behind NAT. So the Peer must have traversed the NAT to learn that (via UPnP maybe?). Is there any way to disable that behavior on server so I can learn the temporary port on NAT?
I am using Net Core 6 and Ubuntu server
Obviously the port I am getting in the log is not a temporary port on NAT, but a local port of the client behind NAT.
You get the port assigned by NAT, not the port used by the client on the LAN. These ports can be the same, because many NAT implementation try to do port preservation if possible.
I have a command line application that listens on an address ie. 192.168.1.89:8001. I also have a c# client application that uses a socket to communicate with the server. Both of these applications are running on the same machine and I am not able connect to the server.
When I try to input 192.168.1.89:8001 as the address for the client I get a "No connection could be made because the target machine actively refused it" error. However when I used putty to make a raw connection to that same address I was successfully able to communicate with the server. Any ideas why putty is able to connect but not the client?
The client is creating the socket like this
this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(IPAddress.Parse(HostIP), Port);.
I have wrote a group chat using C#, using client socket and server socket.
the communication between the client and the server works fine when I run the programs (both server and client) in my own pc using VS 2017.
When I run the Client program in a laptop and the server in my own pc (still using VS 2017, although I don't think this is matter) the client doesn't connect to the server.
my question is how I connect the server and the client outside of the localhost?
I will add the functions from the server and client side the responsible for connecting each other.
function in the server code that start up the server:
public static void ServerUp()
{
IPAddress ipAdd = IPAddress.Parse("127.0.0.1");
TcpListener myListener = new TcpListener(ipAdd, 8001);
myListener.Start();
Console.WriteLine("The server running at port: " + myListener.LocalEndpoint);
users = new List<ClientSocket>();
}
function in the client code that connect to server:
public static void ConnectToServer()
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Console.WriteLine("Connecting...");
IPEndPoint ipAdd = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8001);
socket.Connect(ipAdd);
Console.WriteLine("Connected");
Console.WriteLine(new string('-',Console.WindowWidth));
}
If you run the client on another computer, it crosses the network.
Unless you opened a firewall port for your server somehow, the windows integrated firewall will block all access from outside sources.
Simple as that.
Go to the advanced firewall settings and open the port for the server.
Change the IPs. 127.0.0.1 is used for localhost. Use your local network IPs e.g. 192.168.1.10 etc etc. Also have the required port (8001) open on the firewall of the server machine.
You need the server to listen on the real ip address of your pc instead of 127.0.0.1, and also in the client, on your laptop, you have to use the ip address of your computer instead of 127.0.0.1.
127.0.0.1 is the local network address, which is not bound to your network connection and only reachable on the same computer, not on the network.
Further your windows firewall might block the incoming connection - either add a inbound tcp rule for port 8001 an your computer or temporarily disable the firewall on your computer.
127.0.0.1 is local IP, use the IP assigned by your network's DHCP server.
my Sockets are correctly working in my local network, but when sending the program to a friend, he cant connect to me.
I host the server with this Code:
Socket listener = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
listener.Bind(new IPEndPoint(IPAddress.IPv6Any, 30000));
listener.Listen(500);
And he connects to this server using my external IP address (gotten from http://whatismyip.org/) and my specified port (30000) with this code:
Socket inOut = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
inOut.Connect(new IPEndPoint(IPAddress.Parse(textBox5.Text), 30000));
When using cmd netstat -a i cant find a listening entry with port 30000, so i guess that my server has the wrong settings or i need to specify something different, but i already opened the port in my router (30000).
As-is, the code you have shown should work, IF all of these conditions are met:
your ISP has assigned your Internet connection an IPv6 address. Ask your ISP. Many ISPs don't do this yet for home users. If your ISP has not, your client will have to use AddressFamily.InterNetwork (IPv4) instead. Depending on what kind of IP your client wants to connect to, it needs to create an appropriate Socket type. An IPv4 socket can't connect to an IPv6 IP address, and an IPv6 socket can't connect to an IPv4 address (unless it is configured to be a dual-stack socket).
your ISP is not blocking inbound TCP connections to your Internet IP. Many ISPs do, so they can charge customers extra for hosting their own servers. Most home Internet users don't need to run their own servers. Ask your ISP if this feature is enabled for your Internet account.
your router is configured to forward inbound packets received by the router's public IP address on port 30000 to your server's LAN IPv6 address on port 30000. If your router does not have a public IPv6 address (because the ISP hasn't assigned one), you will have to use AddressFamily.InterNetwork on the client side. Because a router is sitting between the client and server, they can use different IP versions to communicate with the router.
your server code is calling listener.Accept() or Listener. BeginAccept() after listener.Listen() exits without error. This is a given if your code already works on the local LAN, but I'm throwing it in anyway for good measure.
Try doing AddressFamily.InterNetwork instead of AddressFamily.InterNetworkV6
I have a problem with socket connection.
I have a Client and a Server application where the server application listens for clients on a particular port.
500 clients are connected and sending data to the server to be processed and everything is working fine.
At particular time, I closed all clients and also close the server. When I restart the server after 10 minutes and restart the clients 2 minutes later, very few clients (5-15) are able to reconnect.
Please give me a solution why all clients do not reconnect.
mainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
endpoint = new IPEndPoint(IPAddress.Any, int.Parse(txt_server_port.Text));
mainSocket.Bind(endpoint);
mainSocket.Listen(100);
mainSocket.BeginAccept(new AsyncCallback(ConnetedClient), mainSocket);
If all your clients are trying to connect together it might be that the listener backlog got filled.
You can increase the backlog in the parameter passed to the Listen method.