Multiplayer game in Java. Connect client (player) to game that was created by other client

I am working on multiplayer game and I cant to find out how can I connect other clients to the created game. I mean that client A create a socket connection to the server and how other clients (A,B…) can connect to the client A? Can somebody help me please?

P.S. I’m new with network programming, so if you can attach some example i would be very grateful.

Answer

Another client cannot be connected to the Client A because of his firewall.

You can create two majors kinds of network:

  • Server-Client

  • Peer-to-Peer

But a client can save some data to the server and the server can send them to all the clients (you don’t need a Peer-to-Peer network for allow the Client B to send some data to the Client A).

Example: The Client B send his map position to the server, the server send the data to all the clients, so the Client A is able to draw a character tileset at the position of the Client B.

For connect two PCs together, you need to forward a port from the modem of your server to the PC used as server, and open the port from the firewall of the PC used as server.

You can also take a look here Creating a Multiplayer game in python, I give an example where the clients was able to connect them together with IRC and play at a Tic-Tac-Toe game (so you didn’t have to manage a server). I have add an example in Java at the end of this post.

A simple server example:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;


public class Server
{
    public static void main(String[] args) throws Exception
    {
        ServerSocket listener = new ServerSocket(4000);
        String line;
        try
        {
            while (true)
            {
                Socket socket = listener.accept();
                BufferedReader readerChannel = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                BufferedWriter writerChannel = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                try
                {
                    writerChannel.write(new Date().toString() + "nr");
                    writerChannel.flush();

                    while ((line = readerChannel.readLine()) != null)
                    {
                        System.out.println(line);
                    }
                }
                finally
                {
                    socket.close();
                }
            }
        }
        finally
        {
            listener.close();
        }
    }
}

A simple client example:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Date;


public class Client
{
    public static void main(String[] args) throws Exception
    {
        Socket socket = new Socket("127.0.0.1", 4000);
        BufferedWriter writerChannel = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        BufferedReader readerChannel = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String line;

        writerChannel.write(new Date().toString() + "nr");
        writerChannel.flush();

        while ((line = readerChannel.readLine()) != null)
        {
            System.out.println(line);
        }
    }
}

Also take a look at:

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;


public class Client
{
    public static void main(String[] args) throws Exception
    {
        SSLSocketFactory socketBuilder = (SSLSocketFactory) SSLSocketFactory.getDefault();
        SSLSocket socket = (SSLSocket) socketBuilder.createSocket("127.0.0.1", 4000);
    }
}

A simple IRC example:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;


public class Client
{
    public static void main(String[] args) throws Exception
    {
        SSLSocketFactory socketBuilder = (SSLSocketFactory) SSLSocketFactory.getDefault();
        SSLSocket socket = (SSLSocket) socketBuilder.createSocket("irc.freenode.net", 6697);
        BufferedWriter writerChannel = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        BufferedReader readerChannel = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        String line, computerName, nick, login, channel = "#bot", channelPassword = "";
        long id = 1;

        computerName = java.net.InetAddress.getLocalHost().getHostName();
        nick = computerName + "_" + id;
        login = computerName + "_" + id;
        writerChannel.write("NICK " + nick + "rn"); // Join IRC with a specific Nick
        writerChannel.write("USER " + login + " 8 * :" + login + " rn"); // Join IRC with a specific User
        writerChannel.flush();

        while ((line = readerChannel.readLine()) != null)
        {
            if (line.indexOf("004") != -1) // If connected
            {
                break;
            }
            else if (line.indexOf("433") != -1) // If Nick already in use
            {
                id++;
                nick = computerName + "_" + id;
                login = computerName + "_" + id;
                writerChannel.write("NICK " + nick + "rn");
                writerChannel.write("USER " + login + " 8 * :" + login + " rn");
                writerChannel.flush();
            }
        }

        writerChannel.write("JOIN " + channel + " " + channelPassword + "rn"); // Join a channel
        writerChannel.flush();

        while ((line = readerChannel.readLine()) != null)
        {
            try
            {
                line = line.substring(line.indexOf("#"));
                channel = line.substring(0, line.indexOf(" "));

                if (line.toLowerCase().startsWith("ping")) // avoid ping time-out
                {
                    writerChannel.write("PONG :" + line.substring(5) + "rn");
                    writerChannel.flush();
                }
                else if (line.toLowerCase().contains("!ping"))
                {
                    writerChannel.write("PRIVMSG " + channel + " :pongrn");
                    writerChannel.flush();
                }
                else if (line.toLowerCase().contains("!join"))
                {
                    String newChannel = line.substring(line.indexOf("!join") + 6);
                    int stringPosition;
                    if ((stringPosition = newChannel.indexOf(" ")) != -1)
                    {
                        String newPassword = newChannel.substring(stringPosition + 1);
                        newChannel = newChannel.substring(0, stringPosition);
                        writerChannel.write("JOIN " + newChannel + " " + newPassword + "rn");
                        writerChannel.flush();
                    }
                    else
                    {
                        writerChannel.write("JOIN " + newChannel + "rn");
                        writerChannel.flush();
                    }
                }
                else if (line.toLowerCase().contains("!leave"))
                {
                    writerChannel.write("PART " + channel + "rn");
                    writerChannel.flush();
                }
                else if (line.toLowerCase().contains("!quit"))
                {
                    writerChannel.write("QUITrn");
                    writerChannel.flush();
                    System.exit(0);
                }
            }
            catch (Exception e)
            {

            }
        }
    }
}

I cannot give you an example for a Peer-to-Peer network because I have never do it. This is really difficult and you have to do a lot of research on internet.

More informations:

Hint – I have already answer at some similar questions. Even if the programming language are sometime different, I give you the link, the logic is always the same so it can maybe help you:

Leave a Reply

Your email address will not be published. Required fields are marked *