I write the game on java, it is necessary to implement the so-called multiplayer (we limit ourselves to 2 players), and it is imperative to write all of this on sockets ... I found information and even client-server code examples on the Internet, but these examples worked only within one computer, and you need to connect players to each other with different devices (ideally on the Internet, that is, so that people can literally from different parts of the world connect to each other). Can you give code examples or articles / literature where you can see them? I am currently sorting this out at all, so if something is not correctly formulated or said, don’t blame much.
1 answer
I studied sockets by this example. https://habrahabr.ru/post/69136/ This is a special case of the http server. Instead of the Local host, specify the address of another machine and your port and you will be happy. But here you need to carefully monitor the firewalls so that they would not cut this traffic.
- thanks, I will study) only a question ... if I want one player to be the host and the second to connect, then the first player will be the server ... the question is: if I can think of a port and register it in the game, then the address of the machine what should be specified? - Nikita Kragel
- But in your question the essence is hidden without knowing the public IP or host, you actually do not know where to connect ... Then the web server is used ... The participants declare their details on them ... As an option, customers enter data necessary for connection ... So it was - Peter Slusar
- you need to register the address of the machine on the Internet, for this to happen, there must be a white address from the provider and you need to forward the port to the outside. In general, this is better done through the server where both machines are connected. Of course, you can, but from the player that the server server will require the ability to configure routers. What is not always easy - Artem Tikhonovich
|