我正在使用此代码使用libgdx net api启动一个简单的服务器(我目前正在使用libgdx,所以我将来会使用这个api)
Server server = new Server(1234);
该行调用此类:
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();
}
}
}
我的问题是,我一直在收到JVM BIND错误。我正在使用端口1234.它在我的路由器配置中打开(我打开它),未使用的netstat说。该程序在我的防火墙中是whitelistet,所以我不知道问题出在哪里。这是错误(它重复无限次,直到我关闭程序(虽然没有任何循环?)
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
答案 0 :(得分:1)
端口1234已在使用中。检查netstat。也许您的程序的早期实例仍在运行,或者仍有端口处于TIME-WAIT状态。