[英]Android TCP Client can not receive data from TCP Server with wifi
我在使用wifi模塊的計算機和android設備之間發送/接收數據時遇到問題。 我的計算機可以從android發送/接收數據,但是android設備無法從計算機接收數據,它只能發送什么錯誤? 我將計算機設置為TCP Server,而android是TCP Client。 我認為DataInputStrem中存在錯誤,但我不知道如何解決。
public class TCPClient {
private String serverMessage;
public static final String SERVERIP = "192.168.0.1"; //your computer IP address
public static final int SERVERPORT = 6000;
private OnMessageReceived mMessageListener = null;
private boolean mRun = false;
DataOutputStream out;
DataInputStream 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) {
try {
out.writeUTF(message);
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void stopClient(){
mRun = false;
}
public void run() {
mRun = true;
try {
//here you must put your computer's IP address.
InetAddress serverAddr = InetAddress.getByName(SERVERIP);
Log.e("TCP Client", "C: Connecting...");
//create a socket to make the connection with the server
Socket socket = new Socket(serverAddr, SERVERPORT);
try {
//send the message to the server
out = new DataOutputStream(socket.getOutputStream());
Log.e("TCP Client", "C: Sent.");
Log.e("TCP Client", "C: Done.");
//receive the message which the server sends back
in = new DataInputStream(socket.getInputStream());
Log.e("TCP Client x", in.readUTF());
//in this while the client listens for the messages sent by the server
while (mRun) {
serverMessage = in.readUTF();
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.
//class at on asynckTask doInBackground
public interface OnMessageReceived {
public void messageReceived(String message);
}
}
謝謝您的所有回答,對不起,這個問題尚不清楚。 我的項目是通過包含wifi模塊“ HLKM03”的電子板在Android設備和電子板之間進行連接。 我通過測試連接
1我在使用TCPClient應用程序的客戶端(Android)中看到數據發送/接收。 和
2我在服務器端(電子板+ Wifi接入點)看到數據發送/接收,因此我通過此解決方案檢查了數據傳輸“ wifi +電子板-> usb2RS232->計算機(docklight程序)”。 來自所有。 現在,我可以通過更改此代碼來解決所有問題
try {
//send the message to the server
out = new DataOutputStream(socket.getOutputStream());
Log.e("TCP Client", "C: Sent.");
Log.e("TCP Client", "C: Done.");
//receive the message which the server sends back
in = new DataInputStream(socket.getInputStream());
//Log.e("TCP Client x", in.readUTF());
//in this while the client listens for the messages sent by the server
while (mRun) {
//serverMessage = in.readUTF();
//TCPClient localClientActivity = TCPClient.this;
int j = in.read(buffer);
serverMessage = bytesToHexString(buffer, j);
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();
}
並添加函數chage bytetoHexString用於轉換數據從服務器接收數據。 它可以幫助我的android已經從我的電子板上接收數據。 再次感謝你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.