Kryonet:服务器/客户端 - 无法连接

时间:2015-07-20 11:01:53

标签: java android networking kryonet

我正在尝试使用Kryonet在我的电脑上运行服务器类,所以我的Android设备可以连接到服务器。由于我的手机无法连接,我为PC编写了客户端类,并测试了从我的笔记本电脑向我的电脑发送消息。

但我在尝试连接时在客户端上获取此异常:

Creating new Client...
Staring Client...
Trying to connect to 192.168.2.54
java.io.IOException: Unable to connect to: /192.168.2.54:34567
    at com.esotericsoftware.kryonet.TcpConnection.connect(TcpConnection.java:92)
    at com.esotericsoftware.kryonet.Client.connect(Client.java:149)
    at com.esotericsoftware.kryonet.Client.connect(Client.java:110)
    at com.cookedapps.robotredemption.server.ClientTest.run(ClientTest.java:40)
    at com.cookedapps.robotredemption.server.ClientTest.<init>(ClientTest.java:22)
    at com.cookedapps.robotredemption.server.ClientTest.main(ClientTest.java:47)
Caused by: java.net.SocketTimeoutException
    at sun.nio.ch.SocketAdaptor.connect(Unknown Source)
    at com.esotericsoftware.kryonet.TcpConnection.connect(TcpConnection.java:77)
    ... 5 more

我发现我的防火墙阻止了连接,所以我打开了我的Bitdefender防火墙并设法通过cmd将ping发送到我的服务器。 然而,我无法连接。 我也尝试了不同的端口和不同的方向(在笔记本电脑上安装了服务器等)。

我的服务器clas:

    package com.cookedapps.robotredemption.server;

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

import com.cookedapps.robotredemption.util.PacketString;
import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Server;

public class MainServer {

    Server server;
    public int udpPort, tcpPort;

    public MainServer(int udp, int tcp) {
        udpPort = udp;
        tcpPort = tcp;

        run();

    }

    public void run() {
        InetAddress ip = null;
        try {
            ip = InetAddress.getLocalHost ();
        } catch (UnknownHostException e1){ 
            e1.printStackTrace();
        }
        System.out.println("Creating Server...         Server IP: "+ip);

        server = new Server();
        server.getKryo().register(PacketString.class);
        System.out.println("Starting Server...");
        server.start();
        try {
            server.bind(tcpPort, udpPort);
        } catch (IOException e) {
            e.printStackTrace();
        }


        server.addListener(new Listener(){
            public void connected(Connection c){
                System.out.println("Received a connection from "+c.getRemoteAddressTCP().getHostString());

                PacketString packetMessage = new PacketString();
                packetMessage.message = "Hello friend!";
                c.sendTCP(packetMessage);
            }

            public void received(Connection c, Object p){
                System.out.println("New Message received! "+p);
            }

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

    public static void main(String[] args) {
        new MainServer(34567, 34568);
    }
}

我的客户端类:

    package com.cookedapps.robotredemption.server;

import java.io.IOException;

import com.cookedapps.robotredemption.util.PacketString;
import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;

public class ClientTest {

    private String serverIp;
    private int tcpPort, udpPort, TIMEOUT;
    public Client client;

    public ClientTest(String serverIP, int tcpPort, int udpPort){
        this.serverIp = serverIP;
        this.tcpPort = tcpPort;
        this.udpPort = udpPort;
        TIMEOUT = 5000;

        run();
    }

    public void run(){
        System.out.println("Creating new Client...");
        client = new Client();
        client.getKryo().register(PacketString.class);
        System.out.println("Staring Client...");
        client.start();


        client.addListener(new Listener(){
            public void received(Connection connection, Object object){
                System.out.println("New Message received: "+object);
            }
        });
        try {
            System.out.println("Trying to connect to "+serverIp);
            client.connect(TIMEOUT, serverIp, tcpPort, udpPort);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args){
        new ClientTest("192.168.2.54", 34567, 34568);
    }

}

由于互联网不起作用,我决定在这里发帖。 感谢您的任何提示!

0 个答案:

没有答案