Java中的多人游戲。將客戶端(玩家)連接到其他客戶端創(chuàng)建的游戲
另一個客戶端由于其防火墻而無法連接到客戶端A。
您可以創(chuàng)建兩種主要的網絡:
服務器客戶端
點對點
但是客戶端可以將一些數據保存到服務器,服務器可以將它們發(fā)送給所有客戶端(您不需要點對點網絡就可以讓客戶端B向客戶端A發(fā)送一些數據)。
示例:客戶端B將其地圖位置發(fā)送到服務器,服務器將數據發(fā)送給所有客戶端,因此客戶端A能夠在客戶端B的位置繪制字符圖塊。
要將兩臺PC連接在一起,您需要將端口從服務器的調制解調器轉發(fā)到用作服務器的PC,然后從用作服務器的PC的防火墻打開該端口。
您還可以在這里看看如何使用python創(chuàng)建一個多人游戲,我舉了一個示例,其中客戶端可以將它們與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中創(chuàng)建多人游戲
Xcode Mass Multiplayer(不是您可能在想的)
MMO如何處理真人游戲的每一刻為成千上萬的玩家計算和發(fā)送數據包?
解決方法我正在開發(fā)多人游戲,無法找出如何將其他客戶端連接到所創(chuàng)建的游戲。我的意思是客戶端A創(chuàng)建與服務器的套接字連接,其他客戶端(A,B…)如何連接到客戶端A?有人可以幫我嗎?
PS我是網絡編程的新手,因此,如果您可以舉一些例子,我將不勝感激。
相關文章:
1. python - 獲取到的數據生成新的mysql表2. 為什么python中實例檢查推薦使用isinstance而不是type?3. mysql里的大表用mycat做水平拆分,是不是要先手動分好,再配置mycat4. window下mysql中文亂碼怎么解決??5. sass - gem install compass 使用淘寶 Ruby 安裝失敗,出現 4046. python - (初學者)代碼運行不起來,求指導,謝謝!7. 為啥不用HBuilder?8. python - flask sqlalchemy signals 無法觸發(fā)9. python的文件讀寫問題?10. javascript - js 對中文進行MD5加密和python結果不一樣。
