2016-10-14 2 views
0

온라인 게임을 만드는 기본 사항을 배우기 위해 게임을 마음껏 즐길 수 있습니다. 그러나, 그것은 내가 바라는만큼 잘 가지 않았다. 어떤 이유로 클라이언트가 패킷을받지 못하는 것처럼 보입니다. 제목에서 말했듯이 저는 Kryonet과 Slick2D를 사용하고 있습니다.Slick2D + Kryonet - 클라이언트가 연결되었지만 패킷이 수신되지 않았습니다.

플레이어 목록이 업데이트되지 않아 플레이어가 그려지지 않습니다.

저는 두 가지 예문과 수많은 예제를 찾고있었습니다. 나는 내 실수를 찾지 못하는 것 같습니다.

Servercode :

package main; 

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 

import org.newdawn.slick.AppGameContainer; 
import org.newdawn.slick.BasicGame; 
import org.newdawn.slick.GameContainer; 
import org.newdawn.slick.Graphics; 
import org.newdawn.slick.SlickException; 

import com.esotericsoftware.kryonet.Server; 

import lookup.Settings; 
import networking.NListener; 
import networking.Network; 
import structs.Player; 

public class ServerMain extends BasicGame{ 

    /* 
    * Server = Our public server object. 
    * Players = A list containing all players coords and connection ID. 
    */ 
    public static Server server; 
    public static List<Player>players = new ArrayList<Player>(); 


    /* Our Constructor to set up our server and game. */ 
    public ServerMain() throws IOException{ 
     super(Settings.Title); 

     /*We register our kryos and our server.*/ 
     server = new Server(); 
     Network.register(server); 

     /*We init our listener*/ 
     server.addListener(new NListener()); 

     /*Binding and starting the server*/ 
     server.bind(Settings.tcport, Settings.udport); 
     server.start(); 
    } 

    public void render(GameContainer arg0, Graphics g) throws SlickException { 
     //Test for now to see if players actually do connect. 
     g.drawString("Players connected:"+Integer.toString(players.size()), 10, 100); 
    } 

    public void init(GameContainer arg0) throws SlickException {} 
    public void update(GameContainer arg0, int arg1) throws SlickException {} 


    public static void main(String[] args) throws IOException{ 
     //Basic slick2d stuff we set our display non fullscreen and our title from our settings. 
     try{ 
      AppGameContainer appgc; 
      appgc = new AppGameContainer(new ServerMain()); 
      appgc.setDisplayMode(400, 400, false); 
      appgc.start(); 
     }catch(SlickException e){ 
      System.out.println("error"); 
     } 
    } 
} 

서버 NListener :

package networking; 

import com.esotericsoftware.kryonet.Connection; 
import com.esotericsoftware.kryonet.Listener; 

import lookup.Settings; 
import main.ServerMain; 
import networking.Network.*; 
import structs.Player; 

public class NListener extends Listener{ 

    @Override 
    /*Connected is called when a client is connected*/ 
    public void connected (Connection c) { 

     //We make a new player instance. 
     Player newPly = new Player(); 

     //Setting it to the starting position cuz we have no saving so far :) 
     newPly.x = Settings.startX; 
     newPly.y = Settings.startY; 
     newPly.c = c; 

     //We add our newly made player to the list. 
     ServerMain.players.add(newPly); 

     //make an instance of an updateList packet 
     UpdateList outpck = new UpdateList(); 

     //Putting our list into the newly made packet 
     outpck.players = ServerMain.players; 

     //Send said paceket to all clients. 
     ServerMain.server.sendToAllTCP(outpck); 
    } 

    @Override 
    public void disconnected (Connection c) { 
     //TODO: find where connection is in list and then delete that 
    } 

    @Override 
    public void received(Connection c, Object o){ 
     //TODO: act upon a moving client. 
    } 
} 

클라이언트 :

package main; 

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 

import org.newdawn.slick.AppGameContainer; 
import org.newdawn.slick.BasicGame; 
import org.newdawn.slick.GameContainer; 
import org.newdawn.slick.Graphics; 
import org.newdawn.slick.SlickException; 
import org.newdawn.slick.tiled.TiledMap; 

import com.esotericsoftware.kryonet.Client; 
import lookup.Settings; 
import networking.NListener; 
import networking.Network; 
import structs.Player; 


public class BaseGame extends BasicGame{ 
    /* 
    * CurrMap = is the current map to display 
    * players = a list of players to draw. 
    * client = our client object. 
    * drawStr = a test string I draw to test my code (: 
    * */ 

    public TiledMap currmap; 
    public static List<Player>players = new ArrayList<Player>(); 
    public Client client; 
    public String drawStr = "nothing yet"; 


    /*Our constructor to init both game and client*/ 
    public BaseGame(String title) { 
     super(title); 

     //initing our client 
     client = new Client(); 
     client.start(); 

     //Registering our classes and adding our listener 
     Network.register(client); 
     client.addListener(new NListener()); 


     //Lets try connect :) 
     try{ 
      client.connect(Settings.timeout, Settings.IP, Settings.tcport, Settings.udport); 

     } catch(IOException e){ 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void render(GameContainer gc, Graphics g) throws SlickException { 

     //Render our map. at 0,0 
     currmap.render(0, 0); 

     //Lets go through all players and draw them. 
     for (int i = 0; i < players.size(); i++) { 
      Player Tref = players.get(i); 
      g.fillRect(Tref.x*Settings.tileSize, Tref.y*Settings.tileSize, Settings.tileSize, Settings.tileSize); 
     } 

     //this is more for testing to see if player got updated or not. 
     g.drawString(Integer.toString(players.size()), 10, 100); 
     g.drawString(drawStr, 10, 120); 

     g.drawString(players.toString(), 10, 350); 
    } 

    //Initing our tiled map 
    public void init(GameContainer arg0) throws SlickException { 
     currmap = new TiledMap("res/maptest.tmx"); 
    } 

    public void update(GameContainer arg0, int arg1) throws SlickException { 
     //TODO: Update players. 
    } 

    public static void main(String[] args){ 

     //Basic slick2d stuff we set our display non fullscreen and our title from our settings. 
     try{ 
      AppGameContainer appgc; 
      appgc = new AppGameContainer(new BaseGame(Settings.Title)); 
      appgc.setDisplayMode(400, 400, false); 
      appgc.start(); 
     }catch(SlickException e){ 
      System.out.println("error"); 
     } 
    } 
} 

클라이언트 NListener :

package networking; 

import com.esotericsoftware.kryonet.Connection; 
import com.esotericsoftware.kryonet.Listener; 

import main.BaseGame; 
import networking.Network.UpdateList; 

public class NListener extends Listener{ 

    /* 
    * This is our network listener. 
    * Here we handle our receive functions and cleaning up after ourselves :) 
    */ 

    @Override 
    public void connected(Connection c){ 

    } 

    @Override 
    public void received (Connection connection, Object object) { 
     if (object instanceof Network.UpdateList) { 
      UpdateList inpck = new UpdateList(); 

      inpck = (UpdateList)object; 
      BaseGame.players = inpck.players; 
     } 
    } 
} 

공통 네트워크 클래스 :

package networking; 

import java.util.ArrayList; 
import java.util.List; 

import com.esotericsoftware.kryo.Kryo; 
import com.esotericsoftware.kryonet.EndPoint; 

import structs.Player; 

public class Network { 

    static public void register(EndPoint Ep){ 

     //Getting the client kryo 
     Kryo kryo = Ep.getKryo(); 

     //Register our classes 
     kryo.register(UpdateList.class); 
    } 

    /* 
    * Updatelist is for updating list at client. 
    * 
    * */ 
    static public class UpdateList {public List<Player>players = new ArrayList<Player>();} 
} 
+0

새 스레드 (클라이언트) .start(); 이 시도. https://groups.google.com/forum/?fromgroups#!topic/kryonet-users/QTHiVmqljgE을 읽으십시오. – Sneh

답변

0

나는 당신의 문제를 재현하려하지만 나를 위해 일하고있다. 패키지를 수신하면 서버 콘솔 창이 "연결됨"으로 인쇄됩니다. 다음 코드를 시도해보십시오. 클라이언트 콘솔 창이 "테스트"를 인쇄합니다. 이것이 작동하지 않는다면 컴퓨터/네트워크 설정이나 다른 프로젝트의 일부가 당신을 망쳐 놓을 것이라고 가정합니다.

package stack; 

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 

import org.newdawn.slick.AppGameContainer; 
import org.newdawn.slick.BasicGame; 
import org.newdawn.slick.GameContainer; 
import org.newdawn.slick.Graphics; 
import org.newdawn.slick.SlickException; 
import org.newdawn.slick.tiled.TiledMap; 

import com.esotericsoftware.kryonet.Client; 


public class BaseGame extends BasicGame{ 
public TiledMap currmap; 
public static List<Player>players = new ArrayList<Player>(); 
public Client client; 
public String drawStr = "nothing yet"; 


/*Our constructor to init both game and client*/ 
public BaseGame(String title) { 
    super(title); 

    //initing our client 
    client = new Client(); 
    client.start(); 

    //Registering our classes and adding our listener 
    Network.register(client); 
    client.addListener(new NListener2()); 


    //Lets try connect :) 
    try{ 
     client.connect(5000, "localhost", 55555, 55556); 

    } catch(IOException e){ 
     e.printStackTrace(); 
    } 
} 

@Override 
public void render(GameContainer gc, Graphics g) throws SlickException { 

    //Render our map. at 0,0 

    //Lets go through all players and draw them. 
    for (int i = 0; i < players.size(); i++) { 
     Player Tref = players.get(i); 
    } 

    //this is more for testing to see if player got updated or not. 
    g.drawString(Integer.toString(players.size()), 10, 100); 
    g.drawString(drawStr, 10, 120); 

    g.drawString(players.toString(), 10, 350); 
    } 

//Initing our tiled map 
public void init(GameContainer arg0) throws SlickException { 
    } 

public void update(GameContainer arg0, int arg1) throws SlickException { 
    //TODO: Update players. 
    } 

public static void main(String[] args){ 

    //Basic slick2d stuff we set our display non fullscreen and our title from our settings. 
    try{ 
     AppGameContainer appgc; 
     appgc = new AppGameContainer(new BaseGame("yo23")); 
     appgc.setDisplayMode(400, 400, false); 
     appgc.start(); 
    }catch(SlickException e){ 
     System.out.println("error"); 
    } 
} 
} 

-

package stack; 

import java.util.ArrayList; 
import java.util.List; 

import com.esotericsoftware.kryo.Kryo; 
import com.esotericsoftware.kryonet.EndPoint; 


    public class Network { 

static public void register(EndPoint Ep){ 

    //Getting the client kryo 
    Kryo kryo = Ep.getKryo(); 

    //Register our classes 
    kryo.register(String.class); 
} 

/* 
* Updatelist is for updating list at client. 
* 
* */ 
    static public class UpdateList {public List<Player>players = new      ArrayList<Player>();} 
    } 

-

package stack; 
    import com.esotericsoftware.kryonet.Connection; 
    import com.esotericsoftware.kryonet.Listener; 


    public class NListener extends Listener{ 

@Override 
/*Connected is called when a client is connected*/ 
public void connected (Connection c) { 

    //We make a new player instance. 
    Player newPly = new Player(); 

    //Setting it to the starting position cuz we have no saving so far :) 

    //We add our newly made player to the list. 
    ServerMain.players.add(newPly); 
    System.out.println("connected"); 

    //make an instance of an updateList packet 

    //Putting our list into the newly made packet 

    //Send said paceket to all clients. 
    ServerMain.server.sendToAllTCP("test"); 
    } 

@Override 
public void disconnected (Connection c) { 
    //TODO: find where connection is in list and then delete that 
    } 

@Override 
public void received(Connection c, Object o){ 
    //TODO: act upon a moving client. 
    } 
    } 

-

package stack; 
import com.esotericsoftware.kryonet.Connection; 
import com.esotericsoftware.kryonet.Listener; 

public class NListener2 extends Listener{ 


@Override 
public void connected(Connection c){ 

} 

@Override 
public void received (Connection connection, Object object) { 
    if (object instanceof String) { 
     String temp = (String)object; 
     System.out.println(temp); 
    } 
} 
} 

-

package stack; 

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 

import org.newdawn.slick.AppGameContainer; 
import org.newdawn.slick.BasicGame; 
import org.newdawn.slick.GameContainer; 
import org.newdawn.slick.Graphics; 
import org.newdawn.slick.SlickException; 

import com.esotericsoftware.kryonet.Server; 


public class ServerMain extends BasicGame{ 

/* 
* Server = Our public server object. 
* Players = A list containing all players coords and connection ID. 
*/ 
public static Server server; 
public static List<Player>players = new ArrayList<Player>(); 


/* Our Constructor to set up our server and game. */ 
public ServerMain() throws IOException{ 
    super("yo"); 

    /*We register our kryos and our server.*/ 
    server = new Server(); 
    Network.register(server); 

    /*We init our listener*/ 
    server.addListener(new NListener()); 

    /*Binding and starting the server*/ 
    server.bind(55555, 55556); 
    server.start(); 
} 

public void render(GameContainer arg0, Graphics g) throws SlickException { 
    //Test for now to see if players actually do connect. 
    g.drawString("Players connected:"+Integer.toString(players.size()), 10, 100); 
} 

public void init(GameContainer arg0) throws SlickException {} 
public void update(GameContainer arg0, int arg1) throws SlickException {} 


public static void main(String[] args) throws IOException{ 
    //Basic slick2d stuff we set our display non fullscreen and our title from our settings. 
    try{ 
     AppGameContainer appgc; 
     appgc = new AppGameContainer(new ServerMain()); 
     appgc.setDisplayMode(400, 400, false); 
     appgc.start(); 
    }catch(SlickException e){ 
     System.out.println("error"); 
    } 
    } 
    } 
관련 문제