简体   繁体   中英

Java MultiThreading socket Programming for Network Game

I am new in java Multithreading. Actually I want to make two player game on java on lan by using Socket programming. I just want that when both player will connect to the server after that the game will start. My GameServer is basically using thread. Who make thread when client will connect to the server. My logic is that when one client will make a connection with the server after that this thread will wait until the 2nd thread will make connection. When second thread successfully make a connectin. Then both thread will be live and start work. This is connection part of my GameServer logic.

for(int i = 0; i <= 1; i++) {
    connectionSocket = welcomeSocket.accept();
    System.out.println("Connection has been accepted");
    no++;              
}

But this code will not work logically. Any idea or suggestion please?

Your server will support only one match per running - you start the server, players connect, play the game and, for another game to start, you must kill the server and restart - because of the way for loop is written.

while(true) {
    playerOneSocket = welcomeSocket.accept(); // 1
    playerTwoSocket = welcomeSocket.accept(); // 2
    startNewMatch(playerOneSocket,playerTwoSocket);
}

public void startNewMatch(final Socket pOne, final Socket pTwo) {
    new Thread(new Runnable() {
        @Overrride
        public void run() {
            // pOne, pTwo variables visible here       
        }
      }).start();
}

(Any) server always must be free to accept new requests, that's why you pass the two socket's in a new Thread where you handle the single match logic. The arguments of the startNewMatch function have been made final to be seen inside of the run method, but you'll probably want to make a new class extending Thread and pass them in a constructor. Hope that helps.

EDIT:

The while loop runs in your main thread, eg inside:

public static void main(String[] args) 

function, and you start one new thread per every match. You don't wan't every player in his own thread since that would be a waste of resources (see EDIT^3) and would be a lot heavier to write such code. Take a look at following tutorials 1 , 2 , 3

EDIT^2:

"But my confusion is that how server will know that now playerone is communicating and when player two is communicating....Bcz server has same socket for both of the client."

No, server always listens at the given port, and each request is given a new connection (port) by the accept method.

java API doc is your best friend - specifically for for ServerSocket accept it says:

Listens for a connection to be made to this socket and accepts it. The method blocks until a connection is made

So, when

playerOneSocket = welcomeSocket.accept();

is executed, in the playerOneSocket variable you ve got the established "connection" to the player one (on some other port known to both server and player), and the server can accept another connection on the same port - same goes again for the second player. Eg 1345 is your game port:

P1: connect @ server:1345
accept method start - line 1
SERVER: lets meet at port 9999
P1: connect @ server:9999
SERVER: ok connected
accept method end - line 1
P2: connect @ server:1345
accept method start - line 2
SERVER: lets meet at port 10000
P2: connect @ server:10000
SERVER: ok connected
accept method end - line 2

EDIT^3:

I suggest you do like this: First, make it work with one thread per match, then move to one thread per player (as shown by Tudor)- the game will be more responsive on bigger network lag (eg over the Internet). In order to do one thread per player model you'll need to understand good threading and thread communication - see this tutorial

I think you mean something like this?

class Player implements Runnable {

     private Socket socket;

     public Player(Socket socket) {
         this.socket = socket;
     }

     public void run() {
     }
}

List<Socket> players = new ArrayList<Socket>();
// accept two connections
for(int i = 0; i <= 1; i++)
{
     Socket playerSocket = welcomeSocket.accept();
     players.add(playerSocket);
}

// start one thread per connection
for(Socket socket: players) {
     new Thread(new Player(socket)).start();         
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM