我最初是使用Java main方法为Android应用程序运行服务器,但由于Android Studio而被迫通过Android主要活动运行它。在尝试从主活动运行它时,我遇到了绑定到指定端口的问题。当我尝试连接我的客户端(我知道工作正常)时,它返回连接拒绝错误,指示我的服务器实际上没有在指定端口上侦听。这是我的服务器和主要活动。我相信在设置它时我的语法一定存在一些小问题。
public class Server extends Thread {
ServerSocket serverSocket = null;
final Reactor reactor = new Reactor();
int port;
boolean running;
Server(int p) throws IOException {
running = true;
port = p;
}
@Override
public void run() {
try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
while (running == true) {
try {
System.out.println("Connecting");
System.out.println("Server running on port: " + port);
Socket listener = serverSocket.accept();
} catch (IOException e) {
e.printStackTrace();
running = false;
}
}
}
}
这是我的mainActivity,用于启动侦听指定端口的服务器线程
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Server server = null;
try {
server = new Server(4600);
} catch (IOException e) {
e.printStackTrace();
}
server.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是来自客户端的堆栈跟踪
02-14 17:42:56.109 1314-1364/edu.carleton.l8client W/System.err﹕ j java.net.UnknownHostException: Unable to resolve host "172.17.201.280": No address associated with hostname
02-14 17:42:56.109 1314-1364/edu.carleton.l8client W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
02-14 17:42:56.117 1314-1364/edu.carleton.l8client W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
02-14 17:42:56.117 1314-1364/edu.carleton.l8client W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:214)
02-14 17:42:56.117 1314-1364/edu.carleton.l8client W/System.err﹕ at java.net.Socket.tryAllAddresses(Socket.java:108)
02-14 17:42:56.117 1314-1364/edu.carleton.l8client W/System.err﹕ at java.net.Socket.<init>(Socket.java:177)
02-14 17:42:56.127 1314-1364/edu.carleton.l8client W/System.err﹕ at java.net.Socket.<init>(Socket.java:149)
02-14 17:42:56.127 1314-1364/edu.carleton.l8client W/System.err﹕ at edu.carleton.l8client.Client.run(Client.java:99)
02-14 17:42:56.127 1314-1364/edu.carleton.l8client W/System.err﹕ Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
02-14 17:42:56.137 1314-1364/edu.carleton.l8client W/System.err﹕ at libcore.io.Posix.getaddrinfo(Native Method)
02-14 17:42:56.137 1314-1364/edu.carleton.l8client W/System.err﹕ at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55)
02-14 17:42:56.147 1314-1364/edu.carleton.l8client W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
答案 0 :(得分:2)
172.17.201.280不是有效的IP地址,因此它将其解释为主机名。
尝试范围0.0.0.0 - 255.255.255.255
(280> 255)