我想在我的路由器和Android设备之间建立tcp/ip
连接。我已经连接到特定的wifi网络。但我还需要从路由器获取IPv4地址以建立tcp/ip
连接。如果我已经连接,是否可以获得IPv4地址。这是我的代码。如何连接到我的路由器。
@Override
public void onReceive(Context context, Intent intent) {
List<ScanResult> list = scanner.getScanResults();
for(ScanResult result : list){
if(result.SSID.equals("Micro")){
//Connect to THIS network
connect(result.SSID);
break;
}
}
}
public void connect(String name){
String password = "myPassword";
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + name + "\"";
conf.preSharedKey = "\""+ password +"\"";
scanner.addNetwork(conf);
List<WifiConfiguration> list = scanner.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + name + "\"")) {
scanner.disconnect();
scanner.enableNetwork(i.networkId, true);
scanner.reconnect();
break;
}
}
}
这是我的客户班 公共类TCPClient {
private String serverMessage;
public static final String SERVER_IP = "xxx.xxx.x.xx"; // computer IP address
public static final int SERVER_PORT = 4444;
private OnMessageReceived mMessageListener = null;
private boolean run = false;
private PrintWriter out;
private BufferedReader in;
/**
* Constructor of the class. OnMessagedReceived listens for the messages received from server
*/
public TCPClient(OnMessageReceived listener) {
mMessageListener = listener;
}
/**
* Sends the message entered by client to the server
* @param message text entered by client
*/
public void sendMessage(String message){
if (out != null && !out.checkError()) {
out.println(message);
out.flush();
}
}
public void stopClient(){
run = false;
}
public void run() {
run = true;
try {
// computer's IP address.
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
//create a socket to make the connection with the server
Socket socket = new Socket(serverAddr, SERVER_PORT);
try {
//send the message to the server
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
//receive the message which the server sends back
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//in this while the client listens for the messages sent by the server
while (run) {
serverMessage = in.readLine();
if (serverMessage != null && mMessageListener != null) {
//call the method messageReceived from MyActivity class
mMessageListener.messageReceived(serverMessage);
}
serverMessage = null;
}
Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + serverMessage + "'");
} catch (Exception e) {
Log.e("TCP", "S: Error", e);
} finally {
//the socket must be closed. It is not possible to reconnect to this socket
// after it is closed, which means a new socket instance has to be created.
socket.close();
}
} catch (Exception e) {
Log.e("TCP", "C: Error", e);
}
}
// Declare the interface. The method messageReceived(String message) will must be implemented in the MyActivity
// class at on asynckTask doInBackground
public interface OnMessageReceived {
void messageReceived(String message);
}
}
所以现在我已经对我的IP地址进行了硬编码,但是如何从连接的设备中获取?
答案 0 :(得分:0)
下一步是运行dhcp客户端或从您的应用程序发送DHCP消息。一个简单的替代方法是让它快速工作是分配一个静态IP地址,但不建议这样做,因为可能存在IP冲突。