I can't connect to the server (C#) - c#

I have Server and Client scripts in C# programming language. When I create server on my computer and from another computer I'm trying to connect to the server it says:
an existing connection was forcibly closed by the remote host
It's happening when I change the IP of the server to 0.0.0.0 (IPAdress.Any)
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
It' happening when I change the IP of the server to IPv4 which is 10.0.0.6 (On the ipconfig - IPv4).
Also I turned off the Firewall, And I opened port (11000) on website of the router.
I put in the client the public IP address of the server (which is on whatismyip.com).
The program works perfectly when it is on the local network (I mean when the both computers have the same network).
Scripts:
Server code : http://pastebin.com/awsrkjk4
Client code : http://pastebin.com/2Xrm8YnA
Does anyone know how to solve it?

First of all, check to make sure your server is actively listening on the TCP port you expect. If it is a Windows machine, from the command line try:
netstat -ano | findstr ***LISTENINGPORT***
In your case I'm guessing you should be listening on 11000.
If so, on the client side, let me know exactly where your code is breaking. I'm assuming for now it's this:
sender.Connect(new IPEndPoint(IPAddress.Parse(ip), 11000));
.. and also run a WireShark capture on the client machine.
There are plenty of quick start-up guides - basically this will allow you to see the request your Client is making across the network, & see if a connection (if any) is being established on the server.
If you ARE making a connection, then it is just being closed for another reason, then you have a code issue. If NOT, then you have a network/configuration issue. This should help you make progress.

Related

c# Socket.RemoteEndPoint shows different port that I'm using to connect

I've developed a basic console application that will work as server-client. My general workflow will be like, Server starts clients connect to server and once a client send something the server will receive it and send it to ALL clients. Currently I can do this with my codes but my question is when I send back I've tested Socket.RemoteEndPoint and I saw ports like 65xxx. Like my first client had 65020, second 65021 and so go on. I wonder why this happen since I connect with using 9001 port and listen to 9001 port on my computer. I've done a little bit research but couldn't find a solution to my question. Why do I get these port numbers when I use Socket.RemoteEndPoint? It's okay for me since I don't use firewall or anything while I'm testing to even if the clients connects from another ports it's fine by me as long as its works but what if I have a firewall and I open ONLY 9001 port? Will my client-server based programs work then?
So far as the server/firewall on that machine is concerned, what normally matters is the server port. You wouldn't normally apply any filtering based on the client's port, which as I said in a comment, will normally be an ephemeral port. Under most circumstances, client code will make no effort to specify the local port or IP address and will let the OS pick appropriate values.
The Socket class's LocalEndPoint and RemoteEndPoint use "local" and "remote" to mean "from the perspective of the machine on which this code is running", not "who initiated the connection vs who was connected to".
So on the server, LocalEndPoint will give you information about the server end of the connection and RemoteEndPoint will give you information about the client end of the connection. Irrespective of who initiated the connection.

'No connection could be made because the target machine actively refused it

I am running a client application which should connect to a server side application in .NET.
Are there any restrictions on the number of connections a listening port can receive on the server?
I'm asking because I got an exception Message:
No connection could be made because the target machine actively refused it 1.9.64.212:62131
The error "No connection could be made because the target machine actively refused it" means that your request got through so it is not a firewall issue. This message means that noone listens to this port. Make sure your server application is running and that it listens to the right port number (and IP address). On your server computer you can run "netstat -a -b" to find port numbers and server applications that listen to those ports.
There are some possible reasons for that:
The server is not running. Hence it wont listen to that port. If it's a service you may want to restart the service.
The server is running but that port is blocked by Windows Firewall or other firewall. You can enable the program to go through firewall in the Inbound list.
There is a security program on your PC, i.e a Internet Security or Antivirus that blocks several ports on your PC.
I've seen this message when your service throws an unhandled exception, so check that.
This must be a firewall issue (or another connectivity issue), or the server was not started correctly.

I can't get TcpClient to connect to a TcpListener on another host

I have a simple client and server application that talk to each other over TCP/IP using TcpClient and TcpListener. If I run both the server and the client on the same computer with an IP address of 192.168.10.150, they can talk to each other just fine. However, if I move the client to a different computer on my home network, it can’t connect to the server any more (which is still running on 192.168.10.150). From the client computer, I can ping 192.168.10.150. I have turned off Windows Firewall on both computers. I am using port 2244, which I don’t think is used by something else. The two computers are both connected to the same wireless router. But this line of code in the client:
TcpClient client = new TcpClient("192.168.10.150", 2244);
Throws the following exception:
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
192.168.10.150:2244
Both computers are running XP. Any ideas how I get these to connect?
Thanks for the responses. It seems that a couple of you are correct that the network traffic was being stopped by the router. I hooked up the two computers with a switch, and they were able to talk. Unfortunately I haven't been able to successfully configure the router to allow the traffic, but that's OK, as I can just use the switch for now.

How to know if tcp server is in listening state or not

I am developing one tcp server application using tcp listener in Windows service using c#.
After starting my service it works fine and accepts new clients. After some time when ever the new client is trying to connect to server it gives the following exception:
connection failed, is the server running? No connection could be made
because the target machine actively refused it.
At that time the service is in running state only.
How can I verify the tcp server is in listening state or not?
netstat -ab
This command will give you information about all active connections with name of the programs.
Another option is TcpView from Mark Russinovich
If your server is still running and hasn't close the listening socket, it is still in listening state. However if the platform is Windows and your server's backlog queue is full, a windows will refuse further connections. That means that your server is slow accepting new connections.
Not sure if you mean programatically or not, but for the latter, you can try telnet: telnet host port
telnet yahoo.com 80
telnet localhost 3306
etc.
If you want to check if the server is listening, and the server is Windows, you can simply use (e.g. 8888 is your listening port)
netstat -ano|findstr "8888"
Then you'll see the PID of your server instance.
In addition, if you want to check why the TCP connection attempt is fail, you could use Wireshark to capture all the TCP packets between server & client, then the reason will be quite clear.
E.g. if the server has reached the limitation of open socket number, you'll see a SIN from client to server, and how the server is responding.

Unable to connect to local machine IP address using internet IP address

I've spent the last month writing a multiplayer game. I have only been testing it on one machine, using 127.0.01:9051 as the IPEndPoint.
I changed the IP address to my WAN IP, configured port forwarding on my router, configured my software firewall etc... But, it doesn't connect.
I have checked if the port is open using this site. Result: the port is really open.
Also, when I check the port from that website, my server receives packets just fine; however, when I connect from my own machine... it doesn't receive anything.
I've broken everything down to the basics to make sure it wasn't a problem with my code.
This basic code does not work:
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("XXX.XXX.XXX.XXX"), 9051);
TcpClient client = new TcpClient();
client.Connect(iep);
Any ideas are much appreciated.
If you are attempting to connect to the IP that is Forwarded (outsideIP) to the same machine (insideIP), it won't work. There are very few enterprise firewalls and no consumer devices (I know of) that will route a packet from the inside out and translate it back in.
[Internet] -- outsideIP[Router/Firewall]insideIP1 -- insideIP2[Computer]
In this case, packets from insideIP(X) will not be able to connect to outsideIP.
Try to see if you can telnet to your wan IP on port 9051. Routers don't really like it when you connect to their wan IP from within the network itself.
You could use Dyndns or some other service for the outside world to connect to your server and put the same name into your host file that resolves to 127.0.0.1 so that you can connect internally.
Is your client and your server (both the same machine in your failing test) using the same endpoint ip/port? You can only bind a port once at a time. Try using two ports one for server in one for server out (clinet in). That way they will not clash. Use Telnet to check you can connect to the IP/Ports you are using and Netstat to check they are not in use already.

Categories

Resources