Client based Winsock application C++ - c++

I want to make a game server/room. I tried to search it up, but it always needs a physical server room to run. Is there a way and how to make a game room by hosting it on the client's device and people can join and play?
--My own theory--
When the player presses "Host", create a server on their device.
People can enter the game code (an encoded device's public IP address) to join the room
Shut down server & disconnect all client when the host exit the game.
Player can press "Join" and enter a game code (mentioned above) to join a server.
--Problems--
The internet says that IP address is not the only thing needed. What more do I need?
When I try to connect with the public IP address, it blocks the connection (tested with a chat application I created, it can only connect with local IP such as 192.168.0.1 and not public IP like 222.133.114.88). How to make them allow my connections?
Thank you!

Related

Checking if a program is running on local network

I want to write a simple program in c++ that use tcp socket to communicate with the same program on another computer in lan.
To create the tcp socket I could make the user write the ip and the port to make the connection. But I also need to be able to autodetect in the local area network if there is any computer also running the program.
My idea was:
when the program is autodetecting for available connection in lan, it will send all ips a message via udp to a specific port, meanwhile it will also keep listening to a port waiting to eventual answer.
when the program on the other computer is opened for lan connection, it will keep listening to the a port in case another computer is trying to detect, then it will send also via udp the response messagee notifying the possibility of connection.
All the security system is another problem for which I don't need answer now.
// Client 1:
// Search for all ips in local network
// create udp socket
// send check message
// thread function listening for answers
// if device found than show to menu
// continue searching process
// Client 2 (host) :
// user enable lan connection
// create udp socket
// thread function listening for detection requests
// if request structure is right send back identification message
// continue listening for request
My question - Is there a more efficient or standard way to do something like that?
Testing whether another computer is listening on a given port is what hackers do all day to try to take over the world...
When writing a software like you describe, though, you want to specify the IP and port information. A reason to search and automatically find a device would be if you are implementing a printer, for example. In that case, as suggested by Hero, you could use broadcasting. However, in that case, you use UDP (because TCP does not support that feature).
The software on one side must have a server, which in TCP parlance means a listen() call followed by an accept() until a connection materialized.
The client on the other side can then attempt a connect(). If the connect works, then the software on the other side is up and running.
If you need both to be able to attempt a connection, then both must implement the client and server (which is doable if you use ppoll() [or the old select()] you know which event is happening and can act on it, no need for threads or fork()).
On my end, I wrote the eventdispatcher library to do all those things under the hood. I also want many computers to communicate between each others, so I have a form of RPC service I call communicatord. This service is at the same time a client and a server. It listens on a port and tries to connect to other systems. If the other system has a lower IP address, it is considered a server. Otherwise, it is viewed as a client and I disconnect after sending a GOSSIP message. That way the client (larger IP address) can in turn connect to the server. This communicator service allows all my other services to communicate without having to re-implement the communication layer between computer over and over again.

How do I make a multiplayer game with code join in godot?

I want to have a lobby type thing. One player would press host and would get a game key. And the other player would type that code in and join the game. Something like in Jackbox party pack.
The godot docs have a example for lobby code in their documentation here: https://docs.godotengine.org/de/stable/tutorials/networking/high_level_multiplayer.html
There is also a part for running godot as a dedicated server : https://docs.godotengine.org/de/stable/getting_started/workflow/export/exporting_for_dedicated_servers.html#doc-exporting-for-dedicated-servers
There are also enough tutorials out there on youtube like: https://www.youtube.com/watch?v=TGIWD24QIvY&list=PLKCbXu2bxxqa2PtT2rYNb4t5QUsotrTlP
The first link shows you a way to create simple server lobby in godot. To do the password way you have to create some kind of server application which does the routing for you.
For example:
Host creates new Game by calling a server
Server creates an Instance (dedicated Server with ip and port) and a unique Passcode.
Clients connect to the server by entering the passcode.
The server searches with the passcode for the right ip and port the Clients have to connect to.

How can a server application safely connect to a user-supplied IP address?

I'm making a public web service with an "announce" endpoint, that allows users to supply a host+port on which they are hosting a multiplayer game (Minecraft). My service then connects to their game to get some info about it. The game protocol requires my service to send the initial handshake, and then wait for a proper reply.
Ideally, the game could be listening on any port above 1024, and on a host that is not necessarily the source of the announcement. If that cannot be done safely, I could potentially restrict to the game's standard port and/or the remote host of the announce request.
How can I do this in a safe way? What restrictions do I need to put on the host/port? Is there something special I should do when connecting to the game?

iOS NAT64 IPv6 environment issue

Now that apple has changed their review environment to an IPv6 only network ive been running into troubles with my application while testing.
The application requires the user to input their server/systems IP address, port (4401) and credentials to gain access to the application. Following the guidelines provided by apple i set up a NAT64 environment to test my application's compatibility. Running iOS 9.3.2 on an iphone 5s.
My work environment has several internal networks. I connected my iMac to the internal server (10.10.50.XX) via Ethernet which also has an external IP which is what i provide to apple. The 50 network does not run behind a proxy, but there are some sites which get filtered, but can access by confirming. I then shared my Ethernet connection through WiFI.
I connected the device and had internet access with the same pages being filtered. The device gets assigned a local-link address of 169.254.XX.XX. From what ive read my device is connecting to the ipv6 only network correctly, as the iphone does not show the ipv6 address only local link.
If I am correctly connected to the shared IPv6 network, the problem I am having is connecting to my server on the 50 network from my application. The application fails when ever i try to connected to the 10.10.50.XX network or the external(understandable because my imac is on the 50 internal network) .
Testing the application using IPv4 with a wireless router that is connected to the 50 network runs perfectly.I think it is an issue with the local link address not being able to see the 50 network or something.
Heres some brief background info on how i connect to the server. The user inputs all the credentials and the IP address and port of the server they want to connect to. The application saves this as a text (utf-8) and uses boost shared_ptr to send it to the cpp connection class which validates the credentials and provides access to the user. This cpp connection class handles the connection, synchronization, logging, requests and response from the server.
Is it possible to somehow get access to the 50 network, using the NAT64 internet sharing option?
If you're passing an IP address straight down to your network lib, then no. When your device is on the IPv6-only side of the NAT64, the server's IPv4 address is useless.
You almost certainly need a DNS name for the server you're trying to reach. The NAT64 relies first on DNS64 to create IPv6 addresses for your application when the server is IPv4-only. These synthesised addresses contain the IPv4 address(es) for the server, giving the NAT64 the information it needs to translate from IPv6 to IPv4.
Updating this to add: as suggested by user102008 in the comment thread attached to this answer, you may also be able to pass the IPv4 string literal though getaddrinfo() (see code listing 10-1 on this page). When you're behind a NAT64, a synthesised IPv6 address should be among the results returned by that call. The NAT64 will translate from this address back to IPv4 to reach the host you specified. In this case, if the system knows the correct prefix to use for the NAT64, a hostname is not necessary.

Acting as a server outside LAN

I'm currently writing a peer-to-peer chat application to chat around the world, but my problem is that I can't connect() to another computer outside my LAN because although I have the public IP address of my friends and if necessary I could get their private IP address, how can I connect to their PC since the public IP address is the router's address, not the computer's one and I can only specify one address (either the public or the private). So how can I send the message to the computer in specific and not to the router without having to setup it?. I know it's possible because otherwise if you requested a web page all the computers in your lan can get your private information (in a wired scheme, since in wi-fi it's clearly possible).
Any help would be appreciated, sorry for my poor English :D.
There are two possibilities:
1 Read around here: http://en.wikipedia.org/wiki/STUN
2 Go for IPv6! ;-)
Actually the private IP address of computers on the other side of the router will not help you. In these situations, if they initiate the connection to you, then you are fine as you can respond on that connection.
If you want to initiate the connection to them, you must to it to their public IP address and they must have their router configured to map the inbound request (on whatever port) to the correct IP address in their network.
First, you need to be sure that the computer you are trying to connect is listening to a specific port (the same one that you are trying to connect to).
You will also need to configure your friends routers so that they forward all the request on that port to your friends' computers. If you don't have access to the routers, it is probably best to use a computer that is not behind any router as the chat server.
The ideal solution is to use uPNP. This allows your application to talk to the router(s) on the current network and ask them to forward the appropriate messages to you. This then makes a hole in the firewall which allows your app (on another PC) to connect.
Assuming both apps do this, either end can initiate the connection. If only one end has the app, then the OTHER end must initiate connection.
Imagine what would happen if everyone on the internet could just send messages to anyone - with no software pre-installed...