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.
Related
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.
I'm creating a server client application. I have been able to connect to applications on the same network. For example I can connect to 10.0.0.3 and 10.0.0.4.
Now I have been trying to connect to my external IP and I recieve:
System.Net.Sockets.SocketException (0x80004005). No connection could be made because the target machine actively refused it.
listen = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
write = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
end = new IPEndPoint(IPAddress.Parse(IP), 3212);
write.Connect(end);
Is this a problem with my code or with my router settings? Or is it that you can't connect to yourself... however that wouldn't make sense considering I can connect to 127.0.0.1.
This is a common loopback error, because you're router gets confused when doing Internet NAT redirections. Your external IP is assigned to your modem/router...not your computer, remember that your router could be handling traffic for more than one device inside the network under one single external IP then it uses NAT to redirect traffic to specific local connected devices. One example is, your home network with 2 computers, one printer and 2 mobile devices, all of them connected to your home network via wi-fi, all of them will have a local IP address assigned possible via DHCP, however, the external IP address is the same for all local devices because the external IP address is assigned to your router.
When connecting to local network devices ALWAYS use a local IP address instead of the external one
Edit
I wanted to keep this answer as simple as possible but Scott's comment made me re-think that it is always useful to add details that can help the community out there.
Basically, you can actually set up Port Forwarding which allows your router to redirect incoming traffic received in a specific port to a designated device in your local network. This will make your device accessible from outside your network on that specific port and you can easily take advantage of this to connect to sockets from inside your local network using your external IP address. The flip side is that not all routers/modems support Port Forwarding, so you will have to refer to your modem's manual and check if it's supported or not.
But, if you want to set it up solely for the reason that you want to connect devices from your local network using the external IP, I would recommend you NOT to do it and use the internal IP address, you don't really want to go through the security risks that are involved in exposing your local devices to the internet. You can easily set up strict firewall rules to outsiders and a more relaxed security in your local network.
For my socket application I used port number from 10000, secondly I have created Windows Firewall Rule on socket server computer, which allows your external client to connect to the socket server. you can specify number of individual ip address or a range with a port number 10000. From Start -> All Programs -> Windows Firewall with Advance Security. Follow the prompts set the rule for your client IP. Good luck
I presume the IP address you want to connect to is not 127.0.0.1, but your own IP address as used on the internet (e.g. 80.110.140.30)? You can't do that. Try copy-and-pasting that same IP address in your browser, for example, and you'll see it either does not connect or redirects to your router.
From your own network, the only way to connect to yourself is through the loopback device (localhost or 127.0.0.1).
OK so I've just started messing with TCP using c#, and I've successfully set up a server that i can send a 'Hello World' Message to, anyway I've been doing this locally (because both laptops are connected to the same router) i just use the 192.168 number to connect. but The whole purpose of it is to work over the internet, and the routers ip address is obviously the same for both computers, if i type the routers IP address it doesn't work, and if i type the 192.168 number that definitely won't work over the internet... So what IP do i use, or what is a better solution?
here's the line of code if it matters
var client = ScsClientFactory.CreateClient(new ScsTcpEndPoint("192.168.1.142", 10085));
Where 192.168.1.142 is the local ip of the laptop with the server started on it
and 10085 is the port.
You have to configure your router to forward any incoming connection to the port 10085 to you local IP adress. Then anyone will be able to connect using your external IP adress.
Each router has it's own configuration system so you have to search "port forwading" and your router model in google.
It depends on what your trying to acheive i guess. If for example your making a chat application. The client (behind the router) lets say its local IP is 192.168.1.111 and router IP is 80.120.78.100. The client would connect to the server.
Once that connection is made it doesn't matter about sending back to the client because the connection is already open between client and server so the server would just use the same connection. The router figures out where to "route" the packet, stuff which generally you dont need to know about.
If however your server is the one looking for clients, then thats different.
I'll try to explain a little about networks, but you'll have to search about it.
Basically, understand an IP mask, such as 192.168.1.0 as one network. Router's role is to connect different networks, that's why a router typically has 2 ports, WAN (wide) and LAN (local).
With this concept, you can see the internet as one big network made from the connection of various ISPs. Each ISP has a router to its network, and another one that gives you an internet connection. Finally, you have a router at your home. So, from this, you can understand that there are 3 networks connected: your home, your ISP and the internet.
In order for you to be able to connect to a computer at my home, I have to make this computer available from the internet, I have to publish it. I do this by setting up a NAT (network area translation) at my router. This NAT says "anything that comes from the internet on port 12345, forward to 192.168.1.10 (my server) at port 80".
This is an extremely simple explanation, ok?
Now, let's take a big step back. If you have another computer available on your network, you can test if your program is working with a much simpler approach.
Connect both computers to you LAN, so they will acquire similar IP addresses. Let's pretend they are 192.168.1.10 and 192.168.1.20
Run the server at 10 and disable all kind of firewalls (Windows and third party)
Run the client at 20 and try to connect to the server
I am trying to make a winforms client/server chat application. I already have a client and a server, both are working fine, i can log in, send messages, etc .. The problem is that it only works if i connect to my local IP. When i try to run the program with my external IP, i get the following error:
"The requested address is not valid in its context".
In other words, i can run the server on my own computer, start up (for example) 3 clients and let them connect to the running server. This basically means that i'm talking to myself. What im trying to do is to run the chat server on my computer, and let other people run the client-program and connect to the server through the internet.
this is the part where i get an error when i enter my external IP:
public void StartListening()
{
// Get the IP of the first network device, however this can prove unreliable on certain configurations
IPAddress ipaLocal = ipAddress;
// Create the TCP listener object using the IP of the server and the specified port
tlsClient = new TcpListener(ipaLocal, 50702);
// Start the TCP listener and listen for connections
tlsClient.Start(); <--- THINGS GO SOUTH HERE
// The while loop will check for true in this before checking for connections
ServRunning = true;
// Start the new tread that hosts the listener
thrListener = new Thread(KeepListening);
thrListener.Start();
}
im not sure if what i'm trying to do is possible? i can't imagine it is, but i kinda dont know how to proceed with this. I'm new to network programming so any help will be appreciated.
kind regards,
Jane
Jane,
I think your issue is with your IP address setup. This is a network connection problem. You need external IP address so that outside clients can contact your PC. You need more advanced networking. The IP address provided from you ISP is used for domestic uses. You need specialized public IP address so that clients can find you outside of the firewall. This is networking/ISP/external IP issue.
Your server application needs to listen to incoming connections on a specified port, on a specified locally installed NIC. That is why TcpListener always needs to be created using a local IP address: because it only cares which NIC (if you have multiple installed) it should use.
The MSDN page for TcpListener also states it explicitly:
TcpListener Constructor (IPAddress, Int32) Initializes a new instance of the TcpListener class that listens for incoming connection attempts on the specified local IP address and port number.
External IP address is completely irrelevant to a TCP/IP server. You can have numerous routers and network devices along the way, which can then forward incoming connections to your machine.
Just do make sure that your firewall and router are configured properly to allow incoming connections on a specified port. To do that, start your TCP/IP server to open the port, and then use a service like CanYouSeeMe to see if server can be reached from outside.
Regarding your comment (this can prove unreliable on certain configurations), it's obviously "unreliable" when you think about it: a laptop can easily have an Ethernet network controller with a completely different IP address than a Wifi network adapter. Your server app should allow the user to select which IP address to use, instead of picking the first address it gets.
I am using lidgen-network library 3 to try and create a peer to peer connection. I am new to network programming which is why i'm using this library.
Using the DiscoverKnownPeer() function i have been able to connect within my network. What i have read online is that i need to do a NAT punchthrough which requires a running server that is outside the router. I am hoping for a solution that doesnt require this extra server.
If i can store both ip addesses on a web server, and get create a web service to send the ip's to each respective client through XML, would I be able to create a peer to peer connection?
Thank you in advance.
No, you won't be able to establish a connection through NAT without a rendezvous server
NAT stands in your way because even if you know the IP address, that only gets you as far as the router. The router receives a request coming from the internet, and doesn't know which internal IP to send it to, so it throws it away.
NAT punchthrough works by making both peers send outgoing request to each other, the router then "knows" that packets directed at its public IP on the given port should go to the given computer.
So, you need to connect both computers to an external server not behind NAT, and then use that external server to coordinate connecting the two NAT obscured peers together (this is the peer introduction thing that Lidgren has built in).
I know this is old but...
You didn't specify that using the DiscoverKnownPeer() was necessary in your setup. If it isn't then you can setup peer to peer by appplying port forwarding rules in your router.