I am using this code to start a simple server using the libgdx net api (I am currently working with libgdx, so i will use this api in the future)
Server server = new Server(1234);
The line calls this class:
package com.me.mygdxgame;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Net;
import com.badlogic.gdx.net.ServerSocket;
import com.badlogic.gdx.net.Socket;
public class Server extends Thread {
int port;
ServerSocket server;
boolean running = true;
public Server(int port) {
this.port = port;
this.start();
}
@Override
public void run() {
Socket s = null;
String text = "";
try {
server = Gdx.net.newServerSocket(Net.Protocol.TCP, port, null);
s = server.accept(null);
BufferedReader in = new BufferedReader(new InputStreamReader(
s.getInputStream()));
while (s.isConnected()) {
text = in.readLine();
if (text != "") {
Gdx.app.log("Output", text);
Gdx.app.exit();
}
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
My problem is, that i am getting an JVM BIND error all the time. I am using port 1234. It is open in my routers configuration (I opened it), and unused netstat says. The Programm is whitelistet in my firewall, so I don't know where the problem is. Here is the error (it repeats infinite times, until I close the program (although there isn't any loop?)
com.badlogic.gdx.utils.GdxRuntimeException: Cannot create a server socket at port 1234.
at com.badlogic.gdx.backends.lwjgl.LwjglServerSocket.<init>(LwjglServerSocket.java:68)
at com.badlogic.gdx.backends.lwjgl.LwjglNet.newServerSocket(LwjglNet.java:44)
at com.me.mygdxgame.Server.run(Server.java:27)
Caused by: java.net.BindException: Address already in use: JVM_Bind
at java.net.DualStackPlainSocketImpl.bind0(Native Method)
at java.net.DualStackPlainSocketImpl.socketBind(Unknown Source)
at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
at java.net.PlainSocketImpl.bind(Unknown Source)
at java.net.ServerSocket.bind(Unknown Source)
at java.net.ServerSocket.bind(Unknown Source)
at com.badlogic.gdx.backends.lwjgl.LwjglServerSocket.<init>(LwjglServerSocket.java:64)
... 2 more
Port 1234 is already in use. Check netstat. Maybe an earlier instance of your program is still running, or maybe there are still ports in TIME-WAIT state.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.