带有kryonet的非通缉非阻塞套接字

时间:2014-03-19 20:16:43

标签: java android sockets kryonet

我使用kryonet来处理网络上与游戏的数据交换。 但我不知道为什么TCP套接字和UDP套接字变为非阻塞。

所以我很乐意得到一些帮助。

我有一个服务器类和一个客户端类。

package com.me.mygdxgame;

import java.io.IOException;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Server;

public class Serveur {

    private static Serveur instance;

    private World world;
    private WorldController controller;

    private GameScreen hoteScreen;

    private boolean leftPressedHote = false;
    private boolean rightPressedHote = false;
    private boolean leftPressedClient = false;
    private boolean rightPressedClient = false;

    private Server serveur;

    private boolean connected = false;

    private Serveur() {
        serveur = new Server();


        Kryo kryo = serveur.getKryo();
        kryo.register(KeyInfo.class);
        kryo.register(World.class);

        serveur.addListener(new Listener(){

            public void connected(Connection connection) {
                Serveur.getInstance().setConnected(true);
            }

            public void received (Connection connection, Object object) {
                if (object instanceof KeyInfo) {
                    Serveur.getInstance().setKeyInfo((KeyInfo) object);

                    connection.sendTCP(Serveur.getInstance().getWorld());
                }
            }

            public void disconnected(Connection connection) {
                ClientThread.getInstance().setConnected(false);
            }
        });
    }

    public void initialize(GameScreen hote) {
        world = new World();
        controller = new WorldController(world);
        this.hoteScreen = hote;
        hoteScreen.setWorld(world);
    }

    public void start() throws IOException {
        serveur.start();
        serveur.bind(54555, 54777);
    }

    public void stop() {
        serveur.stop();
        Serveur.instance = null;
    }

这是客户端代码:

package com.me.mygdxgame;

import java.io.IOException;
import java.net.InetAddress;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Listener.ThreadedListener;
import com.esotericsoftware.minlog.Log;

public class ClientThread {

    private Client client;

    private GameScreen gameScreen;

    private static ClientThread instance;

    private boolean connected = false;

    public static ClientThread getInstance() {
        if (instance == null) {
            instance = new ClientThread();
        }
        return instance;
    }

    public ClientThread() {
        Log.set(Log.LEVEL_TRACE);

        client = new Client();
        client.start();

        Kryo kryo = client.getKryo();
        kryo.register(World.class);
        kryo.register(KeyInfo.class);



        client.addListener(new Listener() {
            public void connected(Connection connection) {
                ClientThread.getInstance().setConnected(true);
            }

            public void received(Connection connection, Object object) {

                if (object instanceof World) {
                    World world = (World) object;
                    ClientThread.getInstance().getGameScreen().setWorld(world);
                }

            }

            public void disconnected(Connection connection) {
                ClientThread.getInstance().setConnected(false);
            }
        });
    }

    public boolean connectToServer() {
        InetAddress address = client.discoverHost(54777, 5000);
        if (address != null) {
            try {
                client.connect(5000, address, 54555, 54777);
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
            return true;        
        }
        return false;
    }

    public void stop() {
        client.stop();
        ClientThread.instance = null;
    }

    public void sendKeyInfo(KeyInfo info) {
        client.sendTCP(info);
    }

这是我在客户端LogCat上的日志:

03-19 21:16:20.051: D/dalvikvm(26615): create interp thread : stack size=128KB
03-19 21:16:20.051: D/dalvikvm(26615): create new thread
03-19 21:16:20.052: D/dalvikvm(26615): new thread created
03-19 21:16:20.052: D/dalvikvm(26615): update thread list
03-19 21:16:20.052: D/dalvikvm(26615): threadid=15: interp stack at 0x5e91a000
03-19 21:16:20.052: D/dalvikvm(26615): threadid=15: created from interp
03-19 21:16:20.052: D/dalvikvm(26615): start new thread
03-19 21:16:20.054: D/dalvikvm(26615): threadid=15: notify debugger
03-19 21:16:20.054: D/dalvikvm(26615): threadid=15 (Client): calling run()
03-19 21:16:20.218: I/System.out(26615): [CDS]close[34549]
03-19 21:16:20.221: I/System.out(26615): [socket][0] connection /192.168.43.143:54555;LocalPort=-1(5000)
03-19 21:16:20.221: I/System.out(26615): [CDS]connect[/192.168.43.143:54555] tm:5
03-19 21:16:20.223: D/Posix(26615): [Posix_connect Debug]Process com.me.my_gdx_game_android :54555 
03-19 21:16:20.234: I/System.out(26615): [socket][/192.168.43.1:-1] connected
03-19 21:16:20.238: I/System.out(26615): [CDS]EAGAIN or EWOULDBLOCK in Recvfrom
03-19 21:16:20.240: I/System.out(26615): [CDS]connect[/192.168.43.143:54777] tm:90
03-19 21:16:20.241: D/Posix(26615): [Posix_connect Debug]Process com.me.my_gdx_game_android :54777 
03-19 21:16:20.241: I/System.out(26615): [CDS]port[60759]
03-19 21:16:20.245: I/System.out(26615): [CDS]EAGAIN or EWOULDBLOCK in Recvfrom
03-19 21:16:20.250: D/GraphicBuffer(26615): create handle(0x5cbc7f00) (w:1280, h:720, f:4)
03-19 21:16:20.255: I/View(26615): Touch up dispatch to com.badlogic.gdx.backends.android.AndroidGraphics$1{4151ad68 VFE..... .F....I. 0,0-1280,720}, event = MotionEvent { action=ACTION_UP, id[0]=0, x[0]=701.45197, y[0]=430.40222, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=192119851, downTime=192119806, deviceId=3, source=0x1002 }
03-19 21:16:20.415: I/System.out(26615): [CDS]close[51897]
03-19 21:16:20.415: I/System.out(26615): close [socket][/0.0.0.0:51897]
03-19 21:16:20.415: I/System.out(26615): [CDS]close[60759]

1 个答案:

答案 0 :(得分:0)

从Kryonet API的外观来看,例如:写队列,它在内部使用非阻塞套接字通道。