Java中的多人游戏。将客户端(播放器)连接到由其他客户端创建的游戏

时间:2015-04-09 17:53:10

标签: java sockets multiplayer

我正在开发多人游戏,我无法找到如何将其他客户连接到创建的游戏。我的意思是客户端A创建到服务器的套接字连接以及其他客户端(A,B ......)如何连接到客户端A? 有人可以帮帮我吗?

P.S。我是网络编程的新手,所以如果你能附上一些例子,我将非常感激。

2 个答案:

答案 0 :(得分:1)

由于他的防火墙,另一个客户端无法连接到客户端A.

您可以创建两种主要类型的网络:

  • 服务器 - 客户端

  • 对等网络

但客户端可以将一些数据保存到服务器,服务器可以将它们发送给所有客户端(您不需要点对点网络,以允许客户端B向客户端发送一些数据A)。

示例:客户端B将其地图位置发送到服务器,服务器将数据发送到所有客户端,因此客户端A能够在客户端B的位置绘制字符区块集。

要将两台PC连接在一起,您需要将服务器调制解调器的端口转发到用作服务器的PC,并从用作服务器的PC的防火墙打开端口。

你也可以看一下这里Creating a Multiplayer game in python,我举一个例子,客户可以将它们与IRC连接在一起并玩Tic-Tac-Toe游戏(所以你没有管理服务器)。我在本文末尾用Java添加了一个例子。

一个简单的服务器示例:

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() + "\n\r");
                    writerChannel.flush();

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

一个简单的客户端示例:

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() + "\n\r");
        writerChannel.flush();

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

另请参阅:

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);
    }
}

一个简单的IRC示例:

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 + "\r\n"); // Join IRC with a specific Nick
        writerChannel.write("USER " + login + " 8 * :" + login + " \r\n"); // 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 + "\r\n");
                writerChannel.write("USER " + login + " 8 * :" + login + " \r\n");
                writerChannel.flush();
            }
        }

        writerChannel.write("JOIN " + channel + " " + channelPassword + "\r\n"); // 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) + "\r\n");
                    writerChannel.flush();
                }
                else if (line.toLowerCase().contains("!ping"))
                {
                    writerChannel.write("PRIVMSG " + channel + " :pong\r\n");
                    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 + "\r\n");
                        writerChannel.flush();
                    }
                    else
                    {
                        writerChannel.write("JOIN " + newChannel + "\r\n");
                        writerChannel.flush();
                    }
                }
                else if (line.toLowerCase().contains("!leave"))
                {
                    writerChannel.write("PART " + channel + "\r\n");
                    writerChannel.flush();
                }
                else if (line.toLowerCase().contains("!quit"))
                {
                    writerChannel.write("QUIT\r\n");
                    writerChannel.flush();
                    System.exit(0);
                }
            }
            catch (Exception e)
            {

            }
        }
    }
}

我不能给你一个Peer-to-Peer网络的例子,因为我从来没有这样做过。这真的很难,你必须在网上做很多研究。

更多信息:

提示 - 我已经回答了一些类似的问题。即使编程语言有时不同,我也会给你链接,逻辑总是一样的,所以它可以帮助你:

答案 1 :(得分:0)

这是处理它的一种方法。当玩家想要创建游戏时,他的应用程序副本应该在已知端口上打开一个ServerSocket - 应用程序知道的端口号 - 并且可能向播放器显示套接字打开的IP地址。

然后,当另一个玩家想要加入游戏时,她应该输入相同的IP地址,并且她的应用程序副本应该使用常规客户端Socket,输入的IP地址以及应用程序知道的已知端口进行连接。 p>

检查Socket和ServerSocket javadoc以获取详细信息。