Android内网通信主要有两种方式:
- 使用VPN
使用VPN是一种比较常见的解决方案。通过搭建VPN服务器,实现互联网和内网之间的隧道通信。VPN可以在不同的设备上进行安装,然后通过VPN连接在互联网和内网之间进行通信。 - 使用端口映射
端口映射是另一种解决方法,它可以实现将内网中的一个端口映射到互联网上。在Android设备中,我们可以使用PortMap这个应用来实现端口映射。具体实现步骤如下:
1) 在内网中的另一个设备或路由器中安装PortMap服务端。
2) 配置需要映射的端口和设备。
3) 在Android设备上安装PortMap客户端,并且指定服务端IP和端口。
4) 客户端连接成功后,即可访问内网中的设备。
Android 内网通信通常使用 Socket 编程。以下是示例代码:
服务端代码:
public class ServerActivity extends Activity {
private TextView status;
private ServerSocket serverSocket;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
status = (TextView) findViewById(R.id.status);
new Thread(new ServerThread()).start();
}
@Override
protected void onStop() {
super.onStop();
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
// Do nothing
}
}
}
class ServerThread implements Runnable {
public void run() {
try {
serverSocket = new ServerSocket(8888);
while (true) {
Socket client = serverSocket.accept();
status.post(new Runnable() {
@Override
public void run() {
status.setText("Client connected!");
}
});
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
final String msg = line;
status.post(new Runnable() {
@Override
public void run() {
status.setText(msg);
}
});
}
in.close();
client.close();
}
} catch (IOException e) {
final String error = e.getMessage();
status.post(new Runnable() {
@Override
public void run() {
status.setText(error);
}
});
}
}
}
}
客户端代码:
public class ClientActivity extends Activity implements OnClickListener {
private EditText address;
private EditText message;
private TextView status;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
address = (EditText) findViewById(R.id.address);
message = (EditText) findViewById(R.id.message);
status = (TextView) findViewById(R.id.status);
Button send = (Button) findViewById(R.id.send);
send.setOnClickListener(this);
}
public void onClick(View v) {
new Thread(new ClientThread(address.getText().toString(), message.getText().toString())).start();
}
class ClientThread implements Runnable {
private String address;
private String message;
public ClientThread(String address, String message) {
this.address = address;
this.message = message;
}
public void run() {
try {
Socket socket = new Socket(address, 8888);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(message);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
final String response = in.readLine();
status.post(new Runnable() {
@Override
public void run() {
status.setText(response);
}
});
out.close();
in.close();
socket.close();
} catch (UnknownHostException e) {
final String error = e.getMessage();
status.post(new Runnable() {
@Override
public void run() {
status.setText(error);
}
});
} catch (IOException e) {
final String error = e.getMessage();
status.post(new Runnable() {
@Override
public void run() {
status.setText(error);
}
});
}
}
}
}
以上代码使用 Socket 实现了客户端和服务端之间的通信。客户端向服务端发送消息,服务端接收并返回消息。在实际应用中,还需要进行异常处理和优化。
发布者:luotuoemo,转转请注明出处:https://www.jintuiyun.com/157870.html