Java - 无法访问的网络:连接

时间:2015-02-22 22:23:08

标签: java sockets networking connection local

现在,我正在开发一个Server Protcocol,用于我正在开发的游戏,因此服务器和客户正在互相交谈。我有两个服务器,我想能够连接到主服务器,只有那两个。所以我建立了一个系统,主服务器在登录时会检查他们的IP,如果IP在列表中,它将查看密码是否正确。如果是,它将允许子服务器进入。现在,为了确保端口是正确的,我将使用这种方法创建一个套接字:

Socket s = new Socket();
s.bind("localhost", [PORT]);
s.connect([SERVERIP], [SERVERPORT]);

当它在我的计算机上时,该方法工作正常,但我决定使用USB将主服务器代码移动到我的Macintosh。当我开始收到此错误时(是的,我在使用s.connect()时将IP和端口更改为正确的)。

java.net.SocketException: Network is unreachable: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at net.trenterprises.minefortress.game.TestMain.main(TestMain.java:19)

这是我对子服务器的代码,服务器的主要端(Macintosh)工作得很好。我只是无法建立与esablish的联系。

public static void main(String[] args) {
    try {
        // Create Socket
        Socket s = new Socket();
        s.bind(new InetSocketAddress("localhost", 23288));
        s.connect(new InetSocketAddress(InetAddress.getByName("10.1.10.30"), 25567));

        // Create PacketBuffer and put values
        PacketBuffer PB = PacketBuffer.allocate(5);
        PB.put((byte) 0x01);
        PB.putInt(1);
        PB.putString("minefortress");
        PB.putString("killstreak");
        PB.putString("SuperstarGamer");
        PB.putInt(new Random().nextInt());

        // Send it through Socket
        DataOutputStream out = new DataOutputStream(s.getOutputStream());
        out.writeUTF(PB.array());
        out.flush();
        out.close();
        s.close();

        // Close socket, then create a ServerSocket on the same port to listen for a response
        ServerSocket ss = new ServerSocket(23288);
        Socket s2 = ss.accept();
        DataInputStream input = new DataInputStream(s2.getInputStream());
        PacketBuffer PB2 = PacketBuffer.wrap(input.readUTF());
        byte PacketID = PB2.get();
        if(PacketID == 0x15) {
            System.out.println("Rejected from server!");
            System.out.println("Reason: " + PB2.getString());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

0 个答案:

没有答案