gpt4 book ai didi

java - Java 中的多人游戏。将客户端(玩家)连接到其他客户端创建的游戏

转载 作者:行者123 更新时间:2023-12-01 22:22:06 27 4
gpt4 key购买 nike

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

附注我是网络编程新手,所以如果您能附上一些示例,我将非常感激。

最佳答案

由于防火墙的原因,另一个客户端无法连接到客户端 A。

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

  • 服务器-客户端

  • 点对点

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

示例:客户端B将自己的 map 位置发送给服务器,服务器将数据发送给所有客户端,这样客户端A就可以在客户端B的位置绘制角色图 block 。

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

您也可以在这里查看https://stackoverflow.com/questions/30227722/creating-a-multiplayer-game-in-python/30228185#30228185 ,我举了一个例子,客户端能够通过 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)
{
e.printStackTrace();
}
}
}
}

我无法为您提供点对点网络的示例,因为我从未这样做过。这真的很困难,你必须在互联网上做大量的研究。

更多信息:

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

关于java - Java 中的多人游戏。将客户端(玩家)连接到其他客户端创建的游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29545597/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com