Putty can connect to server but client cannot - c#

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

Related

Get all clients which are connected to a remote server port

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.

Connect Windows Form Client With Tcp Server on Asp.Net Mvc Web

I am working on client, server project according to this project the client wait for the server to come online and if the server is online connect with it and then wait for commands to act on.
I would like to implement server on Web so that the Dns ip can be resolve easily into ip address using
IPHostEntry hostEntry;
hostEntry= Dns.GetHostEntry(host);
//you might get more than one ip for a hostname since
//DNS supports more than one record
if (hostEntry.AddressList.Length > 0)
{
var ip = hostEntry.AddressList[0];
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.IP);
s.Connect(ip, 80);
}
But Here are some issue when connecting client with server on different networking scenario. That's why I would like to shift the server on web application because a Url is accessible by all in any networking scenario.
Issue When Implementing on Windows Application(Form)
The client program when run under the router (Infrastructure mode) cannot be able to connect with server if server using dynamic IP.
Have to Port Forwarding on client side.
Please suggest me any method (if exist) to make connection between client/server without having these issues.

Client Server Socket C# communication

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.

Why IPAddress.Any needs to connect to remote host?

I'm building one client server application in c# where server is remote host. I've router and firewall in my network.
My client side code is
hostSocket = new TcpClient();
hostSocket.Connect(serverIp, serverPort);
And My server side code was
eqListner = new TcpListener(IPAddress.Parse(eqIp), eqPort);
in this scenario I'm able to connect client on same pc by giving ip 127.0.0.1 but cannot connect when I ran server on another pc in my network.
Then I've changed my server side code by following:
IPAddress ipAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];
IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, eqPort);
TcpListener eqListner = new TcpListener(ipLocalEndPoint);
But still the result is same. Then I changed my server side code again by this:
eqListner = new TcpListener(IPAddress.Any, eqPort);
And this works perfectly fine. I've read msdn for IPAddress.Any and found that this property set server to listen for client activity on all network interfaces.
My question is why IPAddress.Any needs to connect to remote host? and other functions cannot connects?
Thanks in advance....
in this scenario I'm able to connect client on same pc by giving ip 127.0.0.1 but cannot connect when I ran server on another pc in my network
127.0.0.1 is the loopback address that is always the local host. When the service and the client are only on the same machine this can be convenient.
You are better off coding services to listen on all interfaces, this way should the server change address your application doesn't need to be updated or restarted.
The problem may be in the first line.
IPAddress ipAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];
It will get the first ip of the host were it is running. That may be a IPv6 or localhost address.
If you want to listen in a specific address, it will be better to add it to project settings. It will be stored in app.config, and you can change it without recompile.

Can´t open a socket connection from J2ME to a C# server

I´m trying to open a tcp socket from a j2me midlet on a symbian device to a windows (C#) socket server. The server is working and was tested for months.
Now when I try to open a socket from the midlet
clientSocketConnection = (SocketConnection) Connector.open("socket://" + ip + ":" + port);
it just times out with a -34 error (Could not connect).
I know the phone has network capabilities and permissions as I can open such socket connections between 2 phones (emulated) using "ServerSocket" on one side and in the same machine the server is on.
Somehow the J2ME Socket is not compatible with the C# counterpart....
listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Any hint on this matter would be really appreciated.
Also, should I use another type of socket on the server side? I could not find information about the AdressFamily, SocketType nor ProtocolType for the J2ME sockets.
Adding info due to the recent comments.
It´s not a firewall problem as I have no firewall and DMZ is configured for the machine ip.
The phone (emulated) has access to internet and when I open a socket from an emulator to another, both on the same machine, using my WAN PUBLIC ip on the "client" side to find the "server" side it works.
Well it´s solved now.
Really weird but binding a j2me listening port (ServerPort) on "localhost" it´s not the same as binding a .net listening port on "localhost".
I mean, when I made a "server" midlet listening on localhost the clients could connect, but when I did it on the .net server they couldn´t.
The "solution" was to hardcore the computer ip instead of using "localhost". I really do not understand why, but this worked...

Categories

Resources