sockets - Multiplayer game in Java. Connect client (player) to game that was created by other client -


i working on multiplayer game , cant find out how can connect other clients created game. mean client create socket connection server , how other clients (a,b...) can connect client a? can me please?

p.s. i'm new network programming, if can attach example grateful.

another client cannot connected client because of firewall.

you can create 2 majors kinds of network:

  • server-client

  • peer-to-peer

but client can save data server , server can send them clients (you don't need peer-to-peer network allow client b send data client a).

example: client b send map position server, server send data clients, client able draw character tileset @ position of client b.

for connect 2 pcs together, need forward port modem of server pc used server, , open port firewall of pc used server.

you can take here creating multiplayer game in python, give example clients able connect them irc , play @ tic-tac-toe game (so didn't have manage server). have add example in java @ end of post.

a simple server example:

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);                     }                 }                                 {                     socket.close();                 }             }         }                 {             listener.close();         }     } } 

a simple client example:

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

also take at:

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

a simple irc example:

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 specific nick         writerchannel.write("user " + login + " 8 * :" + login + " \r\n"); // join irc 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 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 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)             {              }         }     } } 

i cannot give example peer-to-peer network because have never it. difficult , have lot of research on internet.

more informations:

hint - have answer @ similar questions. if programming language sometime different, give link, logic same can maybe you:


Popular posts from this blog