如何在Android中打开端口?
如何在android中打开特定端口?
我有一个服务器套接字,但连接被拒绝,因为端口已关闭。
try {
ServerSocket server = new ServerSocket(2021);
Socket client = server.accept();
} catch (Exception e) {
// TODO Auto-generated catch block
a = false;
e.printStackTrace();
}
How can I open a specific port in android?
I have a server socket but the connection is rejected because the port is closed.
try {
ServerSocket server = new ServerSocket(2021);
Socket client = server.accept();
} catch (Exception e) {
// TODO Auto-generated catch block
a = false;
e.printStackTrace();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您仍然无法正常工作,我建议您创建一个扩展 Thread 的内部类来替换整个
new Thread() {
...}.start()
语句(当我尝试声明实例字段时,我总是很难让它们完全正确地工作,我只是坚持在这种语句中创建/重写方法)。我将使内部类(例如ClientAnsweringThread
)具有一个构造函数,该构造函数接受 Socket (client
) 作为参数,然后调用ProcessClientRequest(_client);< /code> 在
run()
方法中,就像您已经拥有的那样。If you still havn't got it to work, I would suggest that you create an inner class that extends Thread to replace that whole
new Thread() {
...}.start()
statement (I have always had trouble getting those to work exactly right when I try to declare an instance field, I just stick with creating/overriding methods in that kind of statement). I would make the inner class, sayClientAnsweringThread
, have a constructor that takes in the Socket (client
) as a parameter and then callsProcessClientRequest(_client);
in therun()
method as you already have.看起来您只是缺少一个围绕
accept()
调用的循环,因此您可以接受多个连接。像这样的事情:It looks that you are just missing a loop around the
accept()
call so you can accept multiple connections. Something like this:为了说明我在评论中的意思:
To illustrate what I meant in my comment: