android客户端无法使用kryonet

时间:2015-10-27 03:40:49

标签: java android tcp udp kryonet

我正在尝试创建一个简单的程序,使用kryonet库将数据从我的计算机发送到我的Android应用程序。几乎没有任何关于如何为Android创建工作客户端的文档。到目前为止,我认为我已经将来自谷歌搜索问题的足够信息拼凑起来以使其正常工作,但我仍然无法将其连接起来。 stacktrace告诉我“java.io.IOException:无法连接到:...”但我知道我的ip和端口有效,因为我已经用ssh测试了它。

客户方:

    public class bclient extends AppCompatActivity {
        Client client;
        String ip = "(my ip)";
        int tcpPort=2300, udpPort =2300;


    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shell);

        System.out.println("entering main activity");
        client=new Client();


        client.addListener(new Listener() {

            public void received(Connection c, Object p) {
                System.out.println("packets received!!");
                //check if packetmessage class matches
                if (p instanceof PacketMessage) {
                    //cast it, so we can access the message within
                    PacketMessage packet = (PacketMessage) p;
                    System.out.println("recieved a message from host: " + packet.message);

                }
            }

        });

        client.getKryo().register(PacketMessage.class);
        client.start();



        new AsyncTask<Integer, Void, Void>(){
            @Override
            protected Void doInBackground(Integer... params) {
                // TODO Auto-generated method stub
                try {
                    client.connect(5000, ip, tcpPort, udpPort);
                    System.out.println("connected!!!");
                } catch(IOException e) {

                    System.out.println("not connected :(");
                    e.printStackTrace();
                    return null;
                }
                return null;

            }
        }.execute(1);




    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_shell, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

数据包类:

public class PacketMessage {
    public String message;
}

服务器端:

public class aserver extends Listener{
     //create server
    static Server server;
    //ports to listen on
     static int udpPort = 2300;
    static int tcpPort= 2300;
    public static void main(String[]arg) throws Exception {
        System.out.println("Creating the server...");
        //create the server
        server = new Server();
        //register a packet
        server.getKryo().register(PacketMessage.class);
        //we can only send objects as packets if they are registered

        //bind the server
        server.bind(tcpPort, udpPort);
        //start the server
        server.start();
        //add the listener
        server.addListener(new aiserver());


        System.out.println("Server is operational!");
    }
    //this is run when a connection is received!
    public void connected(Connection c){
        System.out.println("recieved connection fron"+c.getRemoteAddressTCP().getHostString());
        //create a message packet.
        PacketMessage packetMessage = new PacketMessage();
        packetMessage.message= "Hello Frirend! The time is: "+new Date().toString();
        //send message
        //c.sendTCP(packetMessage);
        //Alternatively we could do:
        c.sendUDP(packetMessage);   
        //to send over UDP
    }
    //this is run when a client has received a packet
    public void recieved(Connection c, Object p){
        System.out.println("packet recieved by client!");
    }

    public void disconnected(Connection c){
        System.out.println("A client disconnected!");
    }

}

0 个答案:

没有答案