简体   繁体   English

使用TCP通过套接字发送字符串

[英]Sending a string through a socket using TCP

I am trying to send a string (and later also an image) from one Android app, to another Android app. 我正在尝试将一个字符串(以及后来的图像)从一个Android应用程序发送到另一个Android应用程序。 I know that I am very close. 我知道我很亲密。

The code "SendString" is one Android app on one phone. 代码“SendString”是一部手机上的一款Android应用。 And the code "AppListener" is on the other phone. 代码“AppListener”在另一部手机上。 "SendString" seems to be working correctly and sending the string because all of my print statements are printing. “SendString”似乎正常工作并发送字符串,因为我的所有打印语句都在打印。

"SendString", which is sending a string to "AppListener": “SendString”,它将字符串发送到“AppListener”:

(the string that I am sending "applicationName" is getting passed to "SendString" from a different activity in the project) (我发送“applicationName”的字符串从项目中的其他活动传递给“SendString”)

I know this code is working because I get all the print statements in "try" and it never gets passed to the catch Exception. 我知道这段代码是有效的,因为我在“try”中得到所有的print语句,它永远不会传递给catch异常。

public class SendString extends AsyncTask<String, String, String> {

@Override
protected String doInBackground(String... params) {

    Log.d("tag_name", "Entered AsyncTask");

    String applicationName = params[0];


    // SEND APPLICATION NAME AND ICON TO OTHER APP

    try {

        Log.d("tag_name", "TRY TO SEND STRING");
        Socket socket = new Socket("192.168.0.26", 1755);
        DataOutputStream DOS = new DataOutputStream(socket.getOutputStream());
        DOS.writeUTF(applicationName);
        Log.d("tag_name", "Application Name Sent!");
        socket.close();

    }

    catch (Exception e){

        Log.d("tag_name", "Did not send string");

    }

    return null;
}

}

Here is the other Android app code that is listening for a port connection, and then it should be receiving the string from "SendString". 这是另一个正在侦听端口连接的Android应用程序代码,然后它应该从“SendString”接收字符串。

public class AppListener extends AsyncTask<String, String, String> {



@Override
protected String doInBackground(String... uri) {

    String msg_received = null;

    System.out.println("LISTENING FOR LAST INSTALLED APP");

    try {


        System.out.println("TRY");
        ServerSocket socket = new ServerSocket(1755);
        System.out.println("Connect to Socket and listening");
        Socket clientSocket = socket.accept();       //This is blocking. It will wait.
        System.out.println("This should print after connection");
        DataInputStream DIS = new DataInputStream(clientSocket.getInputStream());
        msg_received = DIS.readUTF();

        System.out.println("Message from server" + msg_received);

        clientSocket.close();
        socket.close();
    } catch (Exception e) {
        System.out.println("Did not connect to SendString");
    }
    System.out.println("Return Statement is Reached");
    return msg_received;
}
}

The problem is that it seems to be getting stuck at the line 问题是它似乎陷入困境

Socket clientSocket = socket.accept();       //This is blocking. It will wait.

because I never get the print statement "this should print after connection", but I get all the print statements before that. 因为我从来没有得到print语句“这应该在连接后打印”,但我之前得到了所有的打印语句。 Does anyone see what the problem is and why my AppListener activity seems to not connect to the port to receive the string, even though I know that the "SendString" app is connecting and sending the string as it should be (because I see all the print statements). 有没有人看到问题是什么以及为什么我的AppListener活动似乎没有连接到端口接收字符串,即使我知道“SendString”应用程序正在连接并发送字符串应该是(因为我看到所有的印刷声明)。

Does anyone see what the problem is and why my Client seems to not connect to the port to receive the string, even though I know that the server side is connecting and sending the string as it should be. 有没有人看到问题是什么以及为什么我的客户端似乎没有连接到端口接收字符串,即使我知道服务器端正在连接并发送应该是的字符串。

  1. You have 'server' and 'client' mixed up. 你有'服务员'和'客户'混淆了。 Clients connect: servers accept. 客户端连接:服务器接受。
  2. Your real server, which you described as your client, isn't accepting a connection. 您描述为客户端的真实服务器不接受连接。
  3. This can only mean that your real client, which you described as your server, isn't creating one. 这只能意味着您描述为服务器的真实客户端没有创建一个。

You must have either not executed your real client at all, or else got a ConnectException you haven't told us about. 你必须要么根本没有执行你的真实客户端,要么得到一个你没有告诉我们的ConnectException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM