Right now, I am working on a Server Protcocol to use on a game I am working on so server's and clients are talking to each other. I have two servers that I want to be able to connect to the main server, and only those two. So I made a system where the main server will check their IP when logging on, and if the IP is in the list, it will see if the password is correct. If it is, it will allow the sub-server in. Now, to make sure the port was correct, I would create a socket using this method:
Socket s = new Socket();
s.bind("localhost", [PORT]);
s.connect([SERVERIP], [SERVERPORT]);
And that method worked just fine when it was on my computer, but I decided to move the Main Server code over to my Macintosh using a USB. And thats when I started getting this error (Yes, I changed the IP and port to the correct one when using s.connect()).
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)
Here is my code to the sub-server, the main side of the server (The macintosh) is working just fine. I just cant get the connection to 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();
}
}
Aucun commentaire:
Enregistrer un commentaire