Java Networking Help

Associate
Joined
21 Jan 2006
Posts
2,171
Location
Seaham, Co. Durham
Hi all,

For my uni assignment I have to make a multiplayer pac man game using Java. I'm using a client/server approach and have made all the main classes.

I have 3 classes. GameClient, GameServer and Listener.

The Listener class uses a serverSocket to listen for clients requesting a connection. When a client makes a request the serverSocket creates a new Socket and creates a new GameServer, passing in the new Socket as a parameter. The GameClient and GameServer then communicate over this
Socket and it works fine. The Listener also increases the port it is listening on by 1.

The problem occurs when I try to join with a second client. The second GameClient requests a connection from the Listener. This connection is created using the new port which is 1 more than the port the first GameClient is using and a new GameServer is set up to handle this second client. However even though the servers are running on different ports they seem to communicate with each other, so each client is interfering with the other.

For example, if I move left 5 spaces in the first client the pac man moves 5 spaces left. If I then go to the second client, it has also moved 5 spaces to the left, even though I haven't told it to do so.

Is there any way that I can make sure the Sockets don't communicate with each other?
 
Is this a 3rd year project for Rynson?

Why are you increasing port numbers for each successive client? Wouldn't it be easier to have lots of different threads, each handling one clients connection?

Also are you sure that your new clients (numbers 2..n) are actually using a different port to number 1, or have you got lots of clients all connected to the same GameServer object? Does the first server socket refuse any more incoming connections when something is connected?

All sounds a bit bizarre, I'd just have one server, listening on one port, then for each new connection spawn a new thread to handle its IO.
 
It's a 2nd year project at Durham Uni. I tried having a serverSocket running on one port, creating a new thread for each connection, but it gave the symptoms. Therefore I thought it would need a different port for each thread. I'll try implementing it that way again to see if i can make it work and report back.
 
Ok so I have 3 classes on the server side:

Listener - Listens for connections from a client, creates a new Requester using the socket from the serverSocket.

Requester - Takes the socket from the Listener and uses it to create a new PacManProtocol.

PacManProtocol - Contains the game logic.

Listener
Code:
import java.net.*;
import java.io.*;

public class Listener {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = null;
        boolean listening = true;

        try {
            serverSocket = new ServerSocket(4444);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444");
            System.exit(-1);
        }

        while (listening){
            new Requester(serverSocket.accept()).start();
        }
        serverSocket.close();
    }
}

Requester
Code:
import java.io.*;
import java.net.*;

public class Requester extends Thread{

    private Socket socket = null;
    
    public Requester(Socket socket){
        super("Requester");
        this.socket = socket;
    }
    
    public void run(){
        
        try {
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(
                        new InputStreamReader(
                        socket.getInputStream()));
            
            String inputLine, outputLine;    
            PacManProtocol pmp = new PacManProtocol();
    
            while((inputLine = in.readLine()) != null){
               pmp.move(inputLine);
               outputLine = pmp.arrayToString(pmp.board);
               out.println(outputLine);
           }
           out.close();
           in.close();
           socket.close();
	    
        }catch (IOException e){
    	    e.printStackTrace();
    	}
    }
}

The problem remains that when one client makes a move it is reflected in the other, when each client should NOT be knowing what the other is doing.
 
Not that it's going to solve the issue, but why don't you have the following code in a finally block?

Code:
out.close();
in.close();
socket.close();

Actually I'm even surprised that compiles, your example is illegal

ILLEGAL
Code:
try {
   //Stuff
}
out.close();
in.close();
socket.close();
catch(SomeException ex) {
   //Deal with the exception
}

LEGAL
Code:
try {
   //Stuff
} catch(SomeException ex) {
   //Deal with the exception
}finally {
   out.close();
   in.close();
   socket.close();
}
 
Last edited:
Back
Top Bottom