如何使用套接字从服务器读取数据?

时间:2018-06-10 01:48:19

标签: android sockets android-asynctask readline

该应用程序具有MainActivity(包含EditText和Button)和DisplayActivity(包含单个TextView)用户在EditText中输入消息并按下发送按钮。 EditText中的消息字符串将发送到服务器。然后启动DisplayActivity的新意图。 DisplayActivity将使用readLine()从服务器读取数据,并从服务器接收的数据中设置TextView。

activity_main.xml有一个带有id =“@ + id / message_textView”的EditText和带有id =“@ + id / send_button”的Button。 DisplayActivity有android:id =“@ + id / displayTextView”。

我将数据发送到服务器的代码有效,但是当我尝试从服务器读取数据时,readline()就会停在那里并永远坐在那里。

Android应用有MainActivity和DisplayActivity类。

我在eclipse中运行服务器代码。

服务器代码。

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.net.*;
import java.io.*;

public class MyServer {

     public static void main(String[] args) throws IOException {

         int portNumber = 4442;

         System.out.println("Starting server..");

         try {
             while(true) {
             ServerSocket serverSocket =
                 new ServerSocket(portNumber);
             Socket clientSocket = serverSocket.accept();     
             PrintWriter out =
                 new PrintWriter(clientSocket.getOutputStream(), true);                   
             BufferedReader in = new BufferedReader(
                 new InputStreamReader(clientSocket.getInputStream()));
             String message = in.readLine();
             System.out.println(message);//Just print to console, to test if server got message
             out.println(message);
             serverSocket.close();
             clientSocket.close();
//           out.close();
//           in.close();
             }
         } catch (IOException e) {
             System.out.println("Exception caught when trying to listen on port "
                 + portNumber + " or listening for a connection");
             System.out.println(e.getMessage());
         }
     }
}

MainActivity

import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {

    static Socket client;
    private PrintWriter printWriter;
    private EditText messageET;
    private Button sendButton;
    private String message;

    static String hostIP = "10.0.2.2";
    static int port = 4442;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        messageET = findViewById(R.id.message_textView);
        sendButton = findViewById(R.id.send_button);

        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                message = messageET.getText().toString();
                messageET.setText("");

                MyTask myTask = new MyTask();
                myTask.execute();

                Intent intent = new Intent(getApplicationContext(), DisplayActivity.class);
                startActivity(intent);
            }
        });
    }

    class MyTask extends AsyncTask<String,Void,String>
    {
        @Override
        protected String doInBackground(String... strings) {
            try
            {
                client = new Socket(hostIP, port);
                printWriter = new PrintWriter(client.getOutputStream(), true);
                //printWriter.write(message);
                printWriter.println(message);
                //printWriter.flush();
                printWriter.close();
                client.close();
            }catch(IOException e)
            {
                e.printStackTrace();
            }
            return null;
        }
    }
}

DisplayActivity

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;


public class DisplayActivity extends AppCompatActivity {

    //final String TAG = "displayactivitylog";
    private Socket socket;
    private BufferedReader bufferedReader;

    private TextView displayTV;
    private String msgReceived = null;

    String hostIP = "10.0.2.2";
    int port = 4442;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display);

        displayTV = findViewById(R.id.displayTextView);
        ReadTask readTask = new ReadTask();
        readTask.execute();
    }
    class ReadTask extends AsyncTask<String,Void,String>
    {
        @Override
        protected String doInBackground(String... strings) {
            String result = "";
            try
            {
                //create a new socket and attempt to read from it
                socket = new Socket(hostIP,port);
                bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                //*****the app stops on this line, and nothing happens after*****  
                msgReceived = bufferedReader.readLine();
                //************************************ 
//                while((msgReceived = bufferedReader.readLine()) != null){
//                    result += msgReceived;
//                }

                bufferedReader.close();
                socket.close();
            }catch(IOException e)
            {
                e.printStackTrace();
            }
            return result;
        }
        //update the TextView with the message from the server
        @Override
        protected void onPostExecute(String s) {
            displayTV.setText(s);
            super.onPostExecute(s);
        }
    }
}

当我运行调试器时,它实际上只是在doInBackground()方法中的msgReceived = bufferedReader.readLine()上的DisplayActivity.java中停止并且没有给出错误。

我的服务器启动正常,当我从我的应用程序向服务器发送数据时,在eclipse上打印出发送的内容(有时由于某种原因而为空)。

enter image description here

--------------- SOLUTION ---------------

问题由绿色应用程序回答,但基本上服务器类期望在首次打开连接时读取内容,然后发送数据。但是在ReadTask中,它所做的只是尝试从服务器读取数据(但它应该首先发送数据,然后从中读取数据)

更新了代码。

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private EditText messageET;
    private Button sendButton;
    static String message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        messageET = findViewById(R.id.message_textView);
        sendButton = findViewById(R.id.send_button);

        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                message = messageET.getText().toString();
                messageET.setText("");

                Intent intent = new Intent(getApplicationContext(), DisplayActivity.class);
                startActivity(intent);
            }
        });
    }
}

DisplayActivity.java

public class DisplayActivity extends AppCompatActivity {

    private Socket socket;
    private PrintWriter printWriter;
    private BufferedReader bufferedReader;

    private TextView displayTV;
    private String msgReceived = null;

    String hostIP = "10.0.2.2";
    int port = 4442;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display);

        displayTV = findViewById(R.id.displayTextView);
        ReadTask readTask = new ReadTask();
        readTask.execute();
    }
    class ReadTask extends AsyncTask<String,Void,String>
    {
        @Override
        protected String doInBackground(String... strings) {
            String result = "";
            try
            {
                //create a new socket and attempt to write to it first then read from it
                socket = new Socket(hostIP,port);
                //add data to the server
                printWriter = new PrintWriter(socket.getOutputStream(), true);
                printWriter.println(MainActivity.message);
                //get a stream, to be able to read data from the server
                bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                msgReceived = bufferedReader.readLine();
                displayTV.setText(msgReceived);
                //close the socket connections
                printWriter.close();
                bufferedReader.close();
                socket.close();
            }catch(IOException e)
            {
                e.printStackTrace();
            }
            return result;
        }
    }
}

enter image description here

1 个答案:

答案 0 :(得分:3)

你有两个clientsocketss。还有两个服务器套件。

这是一种非常奇怪的方法。

第一个客户端发送一行并关闭连接。这可以正常,因为服务器尝试读取该行并且确实如此。两者都关闭了。

然后你启动第二个客户端。这个连接到一个新的serversocket。但是这个客户端直接尝试读取一行。当服务器也试图从这个新客户端读取一行时,它们将在readLine()上等待永恒。