Я пытаюсь реализовать NSD, но не могу получить ip-адрес хоста на стороне сервера. Ip-адрес, так как я жарил его, он реальный, но всякий раз, когда я пытался получить его со стороны клиента, он возвращает null
Клиентская сторона
package com.example.nsd_client_test;
import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Map;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, Runnable {
public static final String SERVICE_TYPE = "_http._tcp.";
private Button mBtnScan;
private Adapter adapter;
private NsdManager.DiscoveryListener mDiscoveryListener;
private Socket mSocket;
private NsdManager nsdManager;
private NsdManager.ResolveListener mResolverListener;
private NsdServiceInfo mNsdServiceInfo;
private BufferedWriter bufferedWriter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
createDiscoverListener();
createResolverListener();
}
private void init() {
nsdManager = (NsdManager) getSystemService(NSD_SERVICE);
mBtnScan = findViewById(R.id.btn_Scan);
mBtnScan.setOnClickListener(this);
}
@Override
public void onClick(View view) {
nsdManager.discoverServices(SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
}
private void createResolverListener() {
mResolverListener = new NsdManager.ResolveListener() {
@Override
public void onResolveFailed(NsdServiceInfo nsdServiceInfo, int i) {
Toast.makeText(MainActivity.this, "onResolveFailed", Toast.LENGTH_SHORT).show();
}
@Override
public void onServiceResolved(NsdServiceInfo nsdServiceInfo) {
mNsdServiceInfo = nsdServiceInfo;
new Thread(MainActivity.this).start();
}
};
}
private void createDiscoverListener() {
mDiscoveryListener = new NsdManager.DiscoveryListener() {
@Override
public void onStartDiscoveryFailed(String s, int i) {
Toast.makeText(MainActivity.this, "onStartDiscoveryFailed", Toast.LENGTH_SHORT).show();
}
@Override
public void onStopDiscoveryFailed(String s, int i) {
Toast.makeText(MainActivity.this, "onStopDiscoveryFailed", Toast.LENGTH_SHORT).show();
}
@Override
public void onDiscoveryStarted(String s) {
Toast.makeText(MainActivity.this, "onDiscoveryStarted", Toast.LENGTH_SHORT).show();
}
@Override
public void onDiscoveryStopped(String s) {
Toast.makeText(MainActivity.this, "onDiscoveryStopped", Toast.LENGTH_SHORT).show();
}
@Override
public void onServiceFound(final NsdServiceInfo nsdServiceInfo) {
Toast.makeText(MainActivity.this, "Server Founded ", Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, "Found"+nsdServiceInfo.getHost().getHostAddress(), Toast.LENGTH_LONG).show();
// FoudHandler(Integer.valueOf(nsdServiceInfo.getPort()));
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView textView = findViewById(R.id.found_txt);
// textView.setText(nsdServiceInfo.getHost().toString());
}
});
}
@Override
public void onServiceLost(NsdServiceInfo nsdServiceInfo) {
Toast.makeText(MainActivity.this, "onServiceLost", Toast.LENGTH_SHORT).show();
}
};
}
private void FoudHandler(Integer toString) {
return;
}
@Override
public void run() {
try {
mSocket = new Socket(mNsdServiceInfo.getHost().getHostAddress(), mNsdServiceInfo.getPort());
bufferedWriter = new BufferedWriter(new OutputStreamWriter(mSocket.getOutputStream()));
bufferedWriter.write("Connected to you!" + "\n");
bufferedWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
nsdManager.stopServiceDiscovery(mDiscoveryListener);
}
}
, где Серверная сторона
package com.example.nsd_server_test;
import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, Runnable {
public static final String SERVICE_NAME = "TV Server";
public static final String SERVICE_TYPE = "_http._tcp.";
private Button mBtnRegister;
private TextView mTvContent;
private NsdServiceInfo mNsdServiceInfo;
private ServerSocket mServerSocket;
private int mPort;
private NsdManager mNsdManager;
private NsdManager.RegistrationListener mRegistrationListener;
private String my_IP;
String ip ;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
mTvContent.setText(mTvContent.getText() + (String) msg.obj);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTvContent = findViewById(R.id.list_text_view);
mBtnRegister = findViewById(R.id.btn_Register);
mBtnRegister.setOnClickListener(this);
// getIphost b = new getIphost();
// b.execute("hello");
// my_IP=b.ip;
}
@Override
public void onClick(View view) {
// Toast.makeText(getApplicationContext(),ip.toString(),Toast.LENGTH_LONG).show();
createServerSocket();
try {
createNsdServiceInfo();
} catch (UnknownHostException e) {
e.printStackTrace();
}
createRegistration();
register();
}
private void register() {
mNsdManager = (NsdManager) getSystemService(NSD_SERVICE);
mNsdManager.registerService(mNsdServiceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);
}
private void createRegistration() {
mRegistrationListener = new NsdManager.RegistrationListener() {
@Override
public void onRegistrationFailed(NsdServiceInfo nsdServiceInfo, int i) {
Toast.makeText(MainActivity.this, "onRegistrationFailed", Toast.LENGTH_SHORT).show();
}
@Override
public void onUnregistrationFailed(NsdServiceInfo nsdServiceInfo, int i) {
Toast.makeText(MainActivity.this, "onUnregistrationFailed", Toast.LENGTH_SHORT).show();
}
@Override
public void onServiceRegistered(NsdServiceInfo nsdServiceInfo) {
Toast.makeText(MainActivity.this, "onServiceRegistered", Toast.LENGTH_SHORT).show();
new Thread(MainActivity.this).start();
}
@Override
public void onServiceUnregistered(NsdServiceInfo nsdServiceInfo) {
Toast.makeText(MainActivity.this, "onServiceUnregistered", Toast.LENGTH_SHORT).show();
}
};
}
/**
* Create the Server Socket
* */
private void createServerSocket() {
if (mServerSocket != null) return;
try {
mServerSocket = new ServerSocket(0);// 0 will give us the free port
mPort = mServerSocket.getLocalPort();
WifiManager wm = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
int ips = wm.getConnectionInfo().getIpAddress();
String ipString = String.format("%d.%d.%d.%d", (ips & 0xff), (ips >> 8 & 0xff), (ips >> 16 & 0xff), (ips >> 24 & 0xff));
ip=ipString;
Toast.makeText(getApplicationContext(),ip,Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* set the info about the server created
*/
private void createNsdServiceInfo() throws UnknownHostException {
if (mNsdServiceInfo != null) return;
mNsdServiceInfo = new NsdServiceInfo();
mNsdServiceInfo.setServiceName(SERVICE_NAME);
mNsdServiceInfo.setServiceType(SERVICE_TYPE);
mNsdServiceInfo.setHost(InetAddress.getByName(ip));
mNsdServiceInfo.setPort(mPort);
Toast.makeText(getApplicationContext(),mNsdServiceInfo.getHost()+" sent!",Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(),mNsdServiceInfo.getPort()+" sent!",Toast.LENGTH_LONG).show();
}
@Override
public void run() {
try {
Socket socket = mServerSocket.accept();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String content = null;
while ((content = bufferedReader.readLine()) != null) {
Message msg = mHandler.obtainMessage();
msg.obj = content;
msg.what = 0;
mHandler.sendMessageDelayed(msg,3000);
}
bufferedReader = null;
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* close the socket whenever the application distorted
*
* */
@Override
protected void onDestroy() {
super.onDestroy();
try {
mServerSocket.close();
mServerSocket = null;
} catch (IOException e) {
e.printStackTrace();
}
mNsdManager.unregisterService(mRegistrationListener);
}
}
, где я передаю тост за отправленный IP, кажется правильным, но на стороне клиента возвращает ноль. Я проверил документацию Android, но не показывает, как решить эту проблему