Java中的多人游戏。将客户端(玩家)连接到其他客户端创建的游戏
另一个客户端由于其防火墙而无法连接到客户端A。
您可以创建两种主要的网络:
服务器客户端
点对点
但是客户端可以将一些数据保存到服务器,服务器可以将它们发送给所有客户端(您不需要点对点网络就可以让客户端B向客户端A发送一些数据)。
示例:客户端B将其地图位置发送到服务器,服务器将数据发送给所有客户端,因此客户端A能够在客户端B的位置绘制字符图块。
要将两台PC连接在一起,您需要将端口从服务器的调制解调器转发到用作服务器的PC,然后从用作服务器的PC的防火墙打开该端口。
您还可以在这里看看如何使用python创建一个多人游戏,我举了一个示例,其中客户端可以将它们与IRC连接在一起,并可以玩井字游戏(因此您不必管理服务器))。我在这篇文章的结尾添加了一个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() + 'nr'); 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() + 'nr');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); }}
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 NickwriterChannel.write('USER ' + login + ' 8 * :' + login + ' rn'); // Join IRC with a specific UserwriterChannel.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 channelwriterChannel.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) { }} }}
我无法为您提供对等网络的示例,因为我从未这样做过。这确实很困难,您必须在互联网上进行大量研究。
https://docs.oracle.com/javase/7/docs/api/java/net/Socket.html
https://docs.oracle.com/javase/tutorial/networking/sockets/
http://www.oracle.com/technetwork/java/socket-140484.html
您需要一个多线程服务器来处理许多不同的连接。
提示-我已经回答了一些类似的问题。 即使编程语言有时有所不同,我也会为您提供链接,但逻辑总是相同的,因此可以为您提供帮助:
在python中创建多人游戏
Xcode Mass Multiplayer(不是您可能在想的)
MMO如何处理真人游戏的每一刻为成千上万的玩家计算和发送数据包?
解决方法我正在开发多人游戏,无法找出如何将其他客户端连接到所创建的游戏。我的意思是客户端A创建与服务器的套接字连接,其他客户端(A,B…)如何连接到客户端A?有人可以帮我吗?
PS我是网络编程的新手,因此,如果您可以举一些例子,我将不胜感激。
相关文章:
1. mysql怎么保存一件游戏装备,一般游戏开发是否用mysql2. showpassword里的this 是什么意思?代表哪个元素3. c++ - pycharm 4.5 的 python -> preferences 找不到4. 关于mysql unique的问题,如图所示5. python - uwsgi+django的搭建问题6. javascript eval7. javascript - 可以DIY的页面是如何实现的?8. javascript - 求教各位,本地HTML页面怎么在DIV中嵌套服务器上的页面内容?不用iframe。9. html5 - 用h5本地存储是否安全?10. javascript - jQuery中live事件在移动微信端下没有效果;代码如下