This site requires JavaScript, please enable it in your browser!
Greenfoot back
fejfo
fejfo wrote ...

2015/12/13

Problems with multiplayer

fejfo fejfo

2015/12/13

#
I made a test multiplayer game (http://www.greenfoot.org/scenarios/15639 ). You can't really do anything just move around. (and the confirm with a frend (and skype) that it works and start to work on a real project) You are blue the other players are red. The problem is that the red players (the multiplayer players) don't appear. I will show a tiny part of my code but if you want to see the full code you should probably just download the source I provided. I don't think the mistake is in one of the utility classes I made because it does print the movement updates to the console. sorry I had to show so much code the most inportend stuff is form playworld (I think) the mistake most likely is here somewhere : Menu.class : -The reicive method gets called form the client -The messageHandler just has an abstract class with an abstract method reicive(String text) -The join() and host() get called if a Host or Join object gets clicked
import greenfoot.*;

import java.io.IOException;
/**
 * Write a description of class Menu here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Menu extends World implements MessageHandler
{
    static Client c;
    static Server s;

    PlayWorld w;
    /**
     * Constructor for objects of class Menu.
     * 
     */
    public Menu()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        addObject(new Host(),200,200);
        addObject(new Join(),400,200);
    }

    public void join() {
        String ip = Greenfoot.ask("ip");
        String portS = Greenfoot.ask("port");

        join(ip,Integer.valueOf(portS));
    }

    public void join(String ip, int port) {
        try {
            c = new Client(ip,port,this);
        }catch(IOException e) {
            System.out.println("ERROR:" + e);
            e.printStackTrace();
        }
        w = new PlayWorld();
        Greenfoot.setWorld(w);
        send("get player");
    }

    public void host() {
        //String ip = Greenfoot.ask("ip");
        String portS = Greenfoot.ask("port");
        host(Integer.valueOf(portS));
    }

    public void host(int port) {
        try {
            s = new Server(port);
        }catch(IOException e) {
            System.out.println("ERROR:" + e);
            e.printStackTrace();
        }
        
        join("127.0.0.1",port);
    }

    public void reicive(String data) {
        String[] words = data.split(" ");
        if(words[0] == "player") {            
            String id = words[1];
            String x = words[2];
            String y = words[3];

            w.set(id,Integer.valueOf(x),Integer.valueOf(y));
        }else if(words[0] == "get") {
            if(words[1] == "player") {
                MyPlayer pl = w.getPlayer();
                send("player " + pl.id + " " + pl.getX() + " " + pl.getY());
            }
        }
    }

    public static void send(String data) {
        try {
            c.send(data);
        }catch(IOException e) {
            System.out.println("ERROR:" + e);
            e.printStackTrace();
        }
        
    }
}
PlayWorld.set (the pl object is the local player (blue))
public void set(String id,int x,int y) {
        boolean found = false;
        if(id == pl.id) pl.setLocation(x,y);        
        else {
            for(Object a : getObjects(OtherPlayer.class)) {
                OtherPlayer pl = (OtherPlayer) a;
                if(pl.id == id) {
                    pl.setLocation(x,y);
                    found = true;
                    break;
                }
            }
        }
        
        if(found == false) {
            addObject(new OtherPlayer(id),x,y);
        }
    }
the addObject doesn't happen for some reason. the client :

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Client implements Runnable {
	private Socket socket;
	private PrintWriter out;
	private BufferedReader in;
	public long sleepTime = 100; //check for incomming messages 10 times per second
	public MessageHandler handler;	
	//public static IOHelp io = new IOHelp();

	public Client(String ip,MessageHandler m) throws IOException {
		this(ip,25565,m);
	}

	public Client(String ip, int port,MessageHandler m) throws IOException {	
		 handler = m;
	     socket = new Socket(ip, port);
	     out = new PrintWriter(socket.getOutputStream(),true);
	     in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
	     Thread t = new Thread(this);
		 t.start();
		 System.out.println("new Client is construckted (" + ip + "," + port + "," + m+")");
	}
	
	public Client(String ip,long sleepTime,MessageHandler m) throws IOException {
		this(ip,25565,sleepTime,m);
	}

	public Client(String ip, int port,long sleepTime,MessageHandler m) throws IOException {	
		 handler = m;
	     socket = new Socket(ip, port);
	     out = new PrintWriter(socket.getOutputStream(),true);
	     in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
	     this.sleepTime = sleepTime;
	     Thread t = new Thread(this);
		 t.start();
		 System.out.println("new Client is construckted (" + ip + "," + port + "," + m+","+sleepTime+")");
	}
	
	public void send(String data) throws IOException {
		out.println("send,"+ data);//io.stringize(data));
	}
	
	public void sendAll(String data) throws IOException {
		out.println("sendAll,"+ data);//io.stringize(data));
	}

	boolean active = true;
	@Override
	public void run() {
		// Auto-generated method stub
		System.out.println("Client is lissening for messages");
		while(active) {
			try {
				Thread.sleep(sleepTime);
			} catch (InterruptedException e) {
				// Auto-generated catch block
				//e.printStackTrace();
			}
			String input;
			try {
				while((input = in.readLine()) != null) {
					System.out.println("the clients got :" + input);
					handler.reicive(input);
				}
			} catch (IOException e) {
				// Auto-generated catch block
				e.printStackTrace();
			}
		}
	System.out.println("Client isn't lissening for messages anymore");
	}
	
	public void stop() {
		active = false;
	}	
}
I found out to that .jar exports with libraries don't work.
fejfo fejfo

2015/12/13

#
I think I made everything clear and removed all the spelling mistakes
fejfo fejfo

2015/12/13

#
help ? did anyone see this yet ? please just respond even if you can't help
fejfo fejfo

2015/12/17

#
I guess the Greenfoot site is more for small questions and not for giant ones like these
lordhershey lordhershey

2015/12/17

#
will have to DL it and see. This time of year is super busy for most. :/ Are the other players not showing up in all instances, or just the host or clients who joined? If Greenfoot uses the swing libraries, you might need to look up Swing and threads since there can be some wonkiness running other threads. Are you getting send and recieve messages from the Syste.out.print calls?
fejfo fejfo

2015/12/18

#
I pressed tab and then backspace and chrome removed all my texts uuurg here is a recap I made edits to the code since it has been 4 days I added print statements and using 2 instances of greenfoot I noticed the following bugs : the joiner doesn't add the actor representing the host but it does ricive it's movement updates the host doesn't get messages from the joiner (maybe the joiner doesn't send them to the host) and/or doesn't respond the host sends and ricives from him self (sends to joiner to) should I post the new code ?
fejfo fejfo

2015/12/18

#
ow yeah I forgot to show the prints : in the host's terminal : when you start up a host : Server : connecting to port 25565 new Server : 25565,1Server : ServerConnector is lissening on port : 25565 on ip :0.0.0.0 SERVER is listening for messages MENU : hosting Server: ServerConnector : a client joined:Socket Client : new Client is construckted (localhost,25565,Menu@2100e4) Client is lissening for messages MENU: client: 0 sending:player -892051227 300 200 MENU: client: 0 got:player -892051227 300 200 MENU: client: 0 sending:get player MENU : client created:Client@1495755 id:-2038024980 hosting:true MENU: client: -2038024980 got:get player when you move the host slightly : MENU: client: -2038024980 sending:player -892051227 300 199 MENU: client: -2038024980 got:player -892051227 300 199 MENU: client: -2038024980 sending:player -892051227 300 198 MENU: client: -2038024980 got:player -892051227 300 198 MENU: client: -2038024980 sending:player -892051227 300 197 MENU: client: -2038024980 got:player -892051227 300 197 MENU: client: -2038024980 sending:player -892051227 300 196 MENU: client: -2038024980 got:player -892051227 300 196 MENU: client: -2038024980 sending:player -892051227 300 195 MENU: client: -2038024980 got:player -892051227 300 195 when a client joins : Server: ServerConnector : a client joined:Socket when a client moves: (no message) in the client terminal : when the client joins : Client : new Client is construckted (localhost,25565,Menu@5bb961) MENU: client: 0 sending:player 1275904497 300 200 Client is lissening for messages MENU: client: 0 sending:get player MENU : client created:Client@1ca136b id:2043780589 hosting:false when the client moves: MENU: client: 2043780589 sending:player 1275904497 300 199 MENU: client: 2043780589 sending:player 1275904497 300 198 MENU: client: 2043780589 sending:player 1275904497 300 197 MENU: client: 2043780589 sending:player 1275904497 300 196 when the host moves: MENU: client: 2043780589 got:player -892051227 300 194 MENU: client: 2043780589 got:player -892051227 300 193 MENU: client: 2043780589 got:player -892051227 300 192 MENU: client: 2043780589 got:player -892051227 300 191 I haven't tried multiple clients yet
lordhershey lordhershey

2015/12/24

#
Where is the listening socket for the host?
lordhershey lordhershey

2015/12/24

#
I downloaded the scenario, got a compile bug , my version might be a bit old.
lordhershey lordhershey

2015/12/24

#
I'm going to cough up a little sample listener and see if the client tries sending anything.
You need to login to post a reply.