ServerSocket的accept()方法不接受Android上的连接 [英] ServerSocket accept() not accepting connections on Android

查看:121
本文介绍了ServerSocket的accept()方法不接受Android上的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图安装在ServerSocket的在我的Andr​​oid手机,并从我的电脑发送一个char或int或任何东西。

I am trying to setup at ServerSocket on my Android phone, and send a char or int or anything from my computer.

在手机上的code创建一个ServerSocket,然后阻止,同时等待连接的:

The code on the phone creates a ServerSocket and then blocks whilst waiting for a connection:

ServerSocket serverSocket = ServerSocketFactory.getDefault()
                            .createServerSocket(4444);
Log.d("HostThread", "ServerSocket created:"+serverSocket
                             .getInetAddress().getHostAddress());
Socket socket = serverSocket.accept();

(日志说:11月10号至27日:41:43.437:DEBUG / HostThread(23957):ServerSocket的创建:0.0.0.0)

(The log says "10-27 11:41:43.437: DEBUG/HostThread(23957): ServerSocket created:0.0.0.0")

code运行在我的电脑上的一个简单的一点尝试连接到手机:

A simple bit of code running on my PC tries to connect to the phone:

Socket s = new Socket("xx.xx.xx.xx", 4444);

...(加上一些更多的比特,如果在创建套接字。但我没有得到这点,所以离开了这一点!)

... (plus some more bits if the socket is created. But I'm not getting this point, so left that out!)

基本上,手机越来越接受,并且电脑没有连接。该XX.XX.XX.XX是手机,我获得编程的公网IP​​(和它匹配了检查的whatismyip.com)。

Basically, the phone is getting to accept, and the computer is not connecting. The xx.xx.xx.xx is the public IP of the phone I obtain programatically (and it matches up with checking on whatismyip.com).

我已经设置在手机上INTERNET权限。我也一直能够做到这一点反向(ServerSocket的PC机上,客户端上的电话)。

I have set the INTERNET permission on the phone. I have also been able to do this in reverse (ServerSocket on pc, client on phone).

在任何地方我错了的想法?

Any ideas where I am going wrong?

推荐答案

您运行此在模拟器或真实设备上?据我了解,大多数运营商网络是NAT的,不会允许连接到设备上。这可能是你的问题,如果你是一个真正的设备上运行。

Are you running this in the emulator or on a real device? As far as I understand, most operators networks are NATed and doesn't allow connections to devices. This may be your problem, if you're running on a real device.

然而,如果你的code以上只是一个例子 - 你居然不使用端口4444,你也应该知道,大多数UNIX系统(包括Android)将不允许在端口传入的连接下超过1024个,除非你有root权限。

However, if your code above is just an example - and you actually don't use port 4444, you should also be aware of that most unix systems (including Android) won't allow incoming connections on ports lower than 1024, unless you have root permissions.

这篇关于ServerSocket的accept()方法不接受Android上的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆