Java 多人游戏。将客户端(玩家)连接到其他客户端创建的游戏
Multiplayer game in Java. Connect client (player) to game that was created by other client
我正在开发多人游戏,但我不知道如何将其他客户端连接到创建的游戏。我的意思是客户端 A 创建到服务器的套接字连接以及其他客户端(A,B ...)如何连接到客户端 A?
有人可以帮我吗?
P.S。我是网络编程的新手,所以如果你能附上一些例子,我将不胜感激。
这是一种处理方法。当玩家想要创建游戏时,他的应用程序副本应该在已知端口上打开 ServerSocket - 应用程序知道的端口号 - 并且可能向玩家显示打开套接字的 IP 地址。
然后,当另一个玩家想要加入游戏时,她应该输入相同的 IP 地址,她的应用程序副本应该使用常规客户端套接字、输入的 IP 地址和应用程序知道的已知端口进行连接。
查看 Socket 和 ServerSocket javadoc 了解详细信息。
由于防火墙的原因,另一个客户端无法连接到客户端 A。
您可以创建两种主要类型的网络:
服务器-客户端
点对点
但是客户端可以将一些数据保存到服务器,服务器可以将它们发送给所有客户端(您不需要对等网络来允许客户端 B 将一些数据发送到客户端A).
例子:客户端B将自己的地图位置发送给服务器,服务器将数据发送给所有客户端,这样客户端A就可以在客户端B的位置绘制角色图块
为了将两台PC连接在一起,您需要从服务器的调制解调器转发一个端口到用作服务器的PC,并从用作服务器的PC的防火墙打开该端口。
你也可以看看这里Creating a Multiplayer game in python,我举了一个例子,客户可以通过 IRC 将他们连接在一起并玩井字游戏(所以你没有管理服务器)。我在 post 末尾的 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)
{
}
}
}
}
我不能给你一个点对点网络的例子,因为我从来没有做过。这真的很难,你必须在网上做很多研究。
更多信息:
https://docs.oracle.com/javase/7/docs/api/java/net/Socket.html
您需要一个多线程服务器来处理许多不同的连接。
提示 - 我已经回答了一些类似的问题。即使编程语言有时不同,我给你 link,逻辑总是相同的,所以它可能对你有帮助:
Creating a Multiplayer game in python
Xcode Mass Multiplayer (Not What You're Probably Thinking)
我正在开发多人游戏,但我不知道如何将其他客户端连接到创建的游戏。我的意思是客户端 A 创建到服务器的套接字连接以及其他客户端(A,B ...)如何连接到客户端 A? 有人可以帮我吗?
P.S。我是网络编程的新手,所以如果你能附上一些例子,我将不胜感激。
这是一种处理方法。当玩家想要创建游戏时,他的应用程序副本应该在已知端口上打开 ServerSocket - 应用程序知道的端口号 - 并且可能向玩家显示打开套接字的 IP 地址。
然后,当另一个玩家想要加入游戏时,她应该输入相同的 IP 地址,她的应用程序副本应该使用常规客户端套接字、输入的 IP 地址和应用程序知道的已知端口进行连接。
查看 Socket 和 ServerSocket javadoc 了解详细信息。
由于防火墙的原因,另一个客户端无法连接到客户端 A。
您可以创建两种主要类型的网络:
服务器-客户端
点对点
但是客户端可以将一些数据保存到服务器,服务器可以将它们发送给所有客户端(您不需要对等网络来允许客户端 B 将一些数据发送到客户端A).
例子:客户端B将自己的地图位置发送给服务器,服务器将数据发送给所有客户端,这样客户端A就可以在客户端B的位置绘制角色图块
为了将两台PC连接在一起,您需要从服务器的调制解调器转发一个端口到用作服务器的PC,并从用作服务器的PC的防火墙打开该端口。
你也可以看看这里Creating a Multiplayer game in python,我举了一个例子,客户可以通过 IRC 将他们连接在一起并玩井字游戏(所以你没有管理服务器)。我在 post 末尾的 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)
{
}
}
}
}
我不能给你一个点对点网络的例子,因为我从来没有做过。这真的很难,你必须在网上做很多研究。
更多信息:
https://docs.oracle.com/javase/7/docs/api/java/net/Socket.html
您需要一个多线程服务器来处理许多不同的连接。
提示 - 我已经回答了一些类似的问题。即使编程语言有时不同,我给你 link,逻辑总是相同的,所以它可能对你有帮助:
Creating a Multiplayer game in python
Xcode Mass Multiplayer (Not What You're Probably Thinking)