这是我的情况(在真实设备上,不是模拟器):
我有一个 App_A ,它通过TCP将一些数据流式传输到同一设备上可能是 App_B 的另一个软件。
在 App_A 用户手册中,据说可以将数据流式传输到同一设备上的另一个应用程序(在这种情况下为 App_B ),只需将destination_IP更改为&#34 ;本地主机"和destination_port到" 8888"通过 App_A 中提供的选项。
因此,数据流从 source 到 App_a ,最后到 App_B 。
在这种情况下,我正在开发App_B作为TCP客户端应用程序,用于侦听设备上的任何流式TCP数据。
这是我写的代码(在网上找到):
MAINACTIVITY
public class MainActivity extends AppCompatActivity {
TcpClient mTcpClient;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
new ConnectTask().execute("");
}
public class ConnectTask extends AsyncTask<String, String, TcpClient> {
@Override
protected TcpClient doInBackground(String... message) {
//we create a TCPClient object
mTcpClient = new TcpClient(new TcpClient.OnMessageReceived() {
@Override
//here the messageReceived method is implemented
public void messageReceived(String message) {
//this method calls the onProgressUpdate
publishProgress(message);
}
});
mTcpClient.run();
return null;
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
//response received from server
Toast.makeText(context, "Ricevo "+values[0], Toast.LENGTH_LONG).show();
Log.e("test", "response " + values[0]);
//process server response here....
}
}
}
TCP_CLIENT
public class TcpClient {
public static final String SERVER_IP = "localhost";
public static final int SERVER_PORT = 8888;
private String mServerMessage;
private OnMessageReceived mMessageListener = null;
private boolean mRun = false;
private PrintWriter mBufferOut;
private BufferedReader mBufferIn;
public TcpClient(OnMessageReceived listener) {
mMessageListener = listener;
}
public void sendMessage(String message) {
if (mBufferOut != null && !mBufferOut.checkError()) {
mBufferOut.println(message);
mBufferOut.flush();
}
}
public void stopClient() {
mRun = false;
if (mBufferOut != null) {
mBufferOut.flush();
mBufferOut.close();
}
mMessageListener = null;
mBufferIn = null;
mBufferOut = null;
mServerMessage = null;
}
public void run() {
mRun = true;
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
Log.e("TCP Client", "C: Connecting...");
try (Socket socket = new Socket(serverAddr, SERVER_PORT)) {
//sends the message to the server
mBufferOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
//receives the message which the server sends back
mBufferIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//in this while the client listens for the messages sent by the server
while (mRun) {
mServerMessage = mBufferIn.readLine();
if (mServerMessage != null && mMessageListener != null) {
//call the method messageReceived from MyActivity class
mMessageListener.messageReceived(mServerMessage);
}
}
Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + mServerMessage + "'");
} catch (Exception e) {
Log.e("TCP", "S: Error", e);
}
//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.
} catch (Exception e) {
Log.e("TCP", "C: Error", e);
}
}
public interface OnMessageReceived {
public void messageReceived(String message);
}
}
我的问题是我在Socket构造函数中作为第一个参数放入的每个值(在TcpClient类&gt;运行方法&gt;尝试使用资源语句),它抛出以下异常:
java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 8888): connect failed: ECONNREFUSED (Connection refused)
我试着写下所有的陈述,但我无法解决这个问题:
try (Socket socket = new Socket("localhost", SERVER_PORT)) ....
try (Socket socket = new Socket("127.0.0.1", SERVER_PORT)) ....
try (Socket socket = new Socket(InetAddress.getByName(SERVER_IP), SERVER_PORT)) ....
try (Socket socket = new Socket(InetAddress.getLocalHost(), SERVER_PORT)) ....
我读到localhost IP必须是&#34; 10.0.2.2&#34;只有当我需要在模拟器上访问localhost时,这不是我的情况。无论如何,我在设备上也尝试了这个选项,但它不起作用,并且抛出了带有ETIMEDOUT(连接超时)的ConnectException。
我知道我的代码没有针对这种情况进行优化,但目前我只关心TCP通信是否正常工作而不是性能问题。
我会请求你的帮助,如果你能帮助我,我将非常感激。