Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: I created a server/client TCP connection, but it only works locally. Any ideas?  (Read 563 times)

0 Members and 1 Guest are viewing this topic.

meatbeater

  • Newbie
  • *
  • Posts: 1
    • View Profile
So, here's the whole code: https://github.com/Neroooooooo/sfmlNetworking

I posted the github repo link because it's probably more organized. And there are just about 200 lines among 5 files, some of them being comments and cout's, so it shouldn't be hard to understand what's there.

The thing I want to achieve is a server that can receive packets from clients through a TCP connection. Currently, it only works if both the client and the server are on the same machine and I use the local IP to connect. It doesn't even work on 2 different machines connected to the same router.

I tried to forward the port 20000 (used in the code), and connect to the server using the public IP address, but it doesn't work.
https://imgur.com/a/QDhlwAy

I disabled my windows 10 firewalls, it still didn't work.

Can you guys help me here? I'm stuck and I searched everywhere for a solution, but I can't find one. I really want to learn about multiplayer games and networking, but I can't see what I'm doing wrong.


EDIT: I played some more with firewalls, and now it works locally, on 2 different machines connected to the same router. But it does not work with someone that's not connected to the same router. Ideas?
« Last Edit: January 20, 2020, 06:12:14 pm by meatbeater »

Fx8qkaoy

  • Newbie
  • *
  • Posts: 42
    • View Profile
So, here's the whole code: https://github.com/Neroooooooo/sfmlNetworking

I posted the github repo link because it's probably more organized. And there are just about 200 lines among 5 files, some of them being comments and cout's, so it shouldn't be hard to understand what's there.

The thing I want to achieve is a server that can receive packets from clients through a TCP connection. Currently, it only works if both the client and the server are on the same machine and I use the local IP to connect. It doesn't even work on 2 different machines connected to the same router.

I tried to forward the port 20000 (used in the code), and connect to the server using the public IP address, but it doesn't work.
https://imgur.com/a/QDhlwAy

I disabled my windows 10 firewalls, it still didn't work.

Can you guys help me here? I'm stuck and I searched everywhere for a solution, but I can't find one. I really want to learn about multiplayer games and networking, but I can't see what I'm doing wrong.


EDIT: I played some more with firewalls, and now it works locally, on 2 different machines connected to the same router. But it does not work with someone that's not connected to the same router. Ideas?

Haven't took a very deep look, but since it works on same router and not globally the problems may be:
- ur server is not good connected (I listen to local router address and forward it, not connect to public one, deppending on my setup; try both)
- ur client doesn't connect to the public address
- the router is not forwarding the port
- client and server ports doesn't match
U could also try a scenario with a real server probably bought (VPS)

Martin Lefevre

  • Newbie
  • *
  • Posts: 6
    • View Profile
I got the same problem, it is surely the NAT: it blocks every connection from the outside world that seems unfamiliar. You're trying to send data to the client but the client's NAT sees it and says "I don't know you! Go away!". And your data just gets rejected. To avoid such rudeness, you should use the UDP/TCP hole punching methods, or forward ports of your server.

To make it simple, hole punching is sending something to the other contact, so that your NAT knows that you were trying to make a connection with this contact. Then, the other computer can send you data, and your NAT will say "Hey, that's the guy you were trying to connect to! You can pass." and the data will reach your computer. The problem with this method is that both computers have to know each other's address and port.

On the other hand, we have port forwarding: port forwarding is allowing connections on a specific port of your computer. I don't know how to do this though, but it should not be too complicated. Forwarding ports on the server's side is enough to establish a connection. The only requirement to this method is the user having the address of the server , and the port that is forwarded. Therefore, the client just has to try to connect to the server, the server's NAT will say "The port is forwarded, you're lucky." and will do nothing. Then, with the new connection formed between the client and the server, the server knows the address of the client, and can send data without trouble (because the client has already sent data to the server, so the client's NAT will be cool on the data the server is going to send).

Edit: oooops, I didn't see that you already knew port forwarding... My bad. Well, I'll keep this comment in case if someone didn't know that...

If you did not understand this book, feel free to insult me in a polite way.
« Last Edit: April 02, 2020, 06:39:48 pm by Martin Lefevre »