ServerSocket.getLocalSocketAddress()返回空 [英] ServerSocket.getLocalSocketAddress() returning empty
问题描述
我正在尝试在Android手机和Raspberry Pi之间以Wi-Fi P2P创建本地服务器,并以Android为主机.我已经能够使用Pi上的wpa_cli
成功建立P2P连接,但是现在我试图使用C客户端套接字连接到电话并传输数据.但是,行Log.d("Socket waiting", serverSocket.getLocalSocketAddress().toString());
吐出D/Socket waiting: ::/:::8888
.它似乎根本没有地址,那么我应该如何连接呢?
I'm trying to create a local server with Wi-Fi P2P between an Android phone and a Raspberry Pi, with the Android as the host. I have been able to successfully establish a P2P connection using wpa_cli
on the Pi, but now I am trying to use a C client socket to connect to the phone and transfer data. However, the line Log.d("Socket waiting", serverSocket.getLocalSocketAddress().toString());
spits out D/Socket waiting: ::/:::8888
. It doesn't seem to have an address at all, so how am I supposed to connect to it?
根据我的评论,我的研究告诉我正确的IP应该是192.168.49.1.如果IP有任何不同,那也可以,因为我可以将BLE数据包发送到电话,告诉IP.我的问题是IP完全空白.
As indicated by my comment, my research told me that the correct IP should be 192.168.49.1. If the IP were any different, that would be okay, because I can just send a BLE packet to the phone, telling it the IP. My issue is that the IP is entirely blank.
对于等待连接的线程,我的代码如下:
My code is as follows, for a thread that waits on a connection:
public static class DataTransfer extends Thread {
@Override
public void run() {
Log.d("DataTransfer", "Start");
ServerSocket serverSocket = null;
try {
/**
* Create a server socket and wait for client connections. This
* call blocks until a connection is accepted from a client
*/
// Expects a connection at 192.168.49.1:8888
serverSocket = new ServerSocket(8888);
//serverSocket.setReuseAddress(true);
//serverSocket.toString()
Log.d("Socket waiting", serverSocket.getLocalSocketAddress().toString());
Socket client = serverSocket.accept();
InputStream inputstream = client.getInputStream();
Log.d("InputStream Available", String.valueOf(inputstream.available()));
serverSocket.close();
}
catch (IOException e) {
Log.e("Receive Error", e.getMessage());
if(serverSocket != null) {
try {
serverSocket.close();
} catch (IOException ex) {
Log.e("Failed to close socket", ex.getMessage());
}
}
return;
}
}
}
这是ip a
在Pi上通过Wi-Fi P2P连接后的输出
And here is the output of ip a
on the Pi, once it is connected via Wi-Fi P2P
11: p2p-wlan0-8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether b2:0e:07:e6:e6:55 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.23/24 brd 192.168.1.255 scope global noprefixroute p2p-wlan0-8
valid_lft forever preferred_lft forever
inet6 fe80::e79c:33f3:6e49:b6ed/64 scope link
valid_lft forever preferred_lft forever
最终 我的问题似乎无关紧要.正如下面的两个注释所示,显示的IP很好,仅表示它接受来自任何设备的连接.我的实际问题是我在Pi上设置了静态IP,但未指定该静态IP用于哪个接口.客户端需要使用192.168.49.#地址,而静态IP阻止了该地址.
Final edit: My problem was seemingly unrelated. As both comments below indicate, the IP shown off is fine, it just means it accepts connections from anything. My actual issue was that I had a static IP set up on my Pi without specifying which interface the static IP was for. The client needed to be on a 192.168.49.# address, and the static IP was preventing it.
推荐答案
您可以通过将地址传递给构造函数来指定服务器套接字正在侦听的接口:
You can specify the interface the server socket is listening on by passing an address to the constructor:
serverSocket = new ServerSocket(8888, 10, InetAddress.getByName("192.168.49.1"));
看到::
表示您的服务器正在监听所有接口上的IPv6连接.这由全零的IPv6地址表示,该地址可以写为::
.但是您正在尝试连接到IPv4地址,而不是IPv6.我使用过的大多数系统都经过了配置,以便IPv6服务器可以接受IPv4连接,但是我想您的系统不能. 此问题的答案表明,您可以更改系统的sysctl
的行为:
Seeing ::
means your server was listening for IPv6 connections on all interfaces. That is represented by the IPv6 address of all zeros which can be written as ::
. But you are trying to connect to an IPv4 address, not IPv6. Most systems I've worked with are configured so that IPv4 connections can be accepted by an IPv6 server, but I guess yours isn't. The answer to this question suggests you may be able to change your system's behavior with sysctl
:
sysctl net.ipv6.bindv6only=0
这篇关于ServerSocket.getLocalSocketAddress()返回空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!