从Socket Connection接收数据流 - Android

时间:2016-04-13 10:25:41

标签: android sockets websocket stream inputstream

我想要实现的目标:

步骤1>>打开套接字连接:

Socket socket = new Socket(IPADDRESS, PORTNUMBER);

步骤2>>开始发送&从套接字接收数据:

PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
pw.write(dataForSocket + "\n");//Sending Data to socket....

BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String translatedMessage = br.readLine();
Log.d(TAG, "Translated Message = "+translatedMessage);//Recieving data from Socket...

步骤3>>接收来自Socket& Sons的数据。继续更新UI ......

我到现在为止做了什么:

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

//High priority UI variables goes below....
private PrintStream out;
private Socket socket;
private BufferedReader in;


//Medium priority NON-UI variables goes below.....
public static final String SERVER_IP = "203.197.xx.xxx";
public static final int SERVER_PORT = 4xxx;


//Least priority variables goes below.....
private final String TAG = "AsyncThread";


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

    try {
        socket = new Socket(SERVER_IP, SERVER_PORT);
        String dataForSocket = "A_LARGE_STRING_OF_DATA_SEPERATED_AND_END_WITH_\n";

        /////////////---------CODE TO READ & WRITE------------\\\\\\\\\\\\\\\\\\\\\\\\\

//            out = new PrintStream(socket.getOutputStream(), true);
//            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//
//
//            out.write(dataForSocket.getBytes());
//            String message = in.readLine();
//            Log.d(TAG, "Message from Socket == "+message);



        PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        // Send first message - Message is being correctly received
              pw.write(dataForSocket + "\n");
//            pw.write(dataForSocket);
        // Send off the data



        String translatedMessage = br.readLine();
        Log.d(TAG, "Translated Message = "+translatedMessage);//In every case this comes null....


//            pw.flush();
//            pw.close();


    }
    catch(Exception e){
        e.printStackTrace();
    }

    return null;
}//doInBackground
}//async task class closes here.....

但BufferedReader始终为null。所以,我不知道出了什么问题。

因此作为结论:

如果有人能给我一些确切的步骤&amp;指导我完成这个场景,然后我将非常感谢你。

P.S。 :直到现在我甚至无法连接到插座:-( 我尝试在设备上运行,在模拟器上运行,在Genymotion上运行没有任何作用。

另外,如果有人可以提供某种在线测试仪,那么请提供给我。

1 个答案:

答案 0 :(得分:0)

最后得到了解决方案:

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

    //High priority UI variables goes below....
    private PrintStream out;
    private Socket socket;
    private BufferedReader in;


    //Medium priority NON-UI variables goes below.....
    public static final String SERVER_IP = "203.xxx.xx.xxx";
    public static final int SERVER_PORT = 4xxx;


    //Least priority variables goes below.....
    private final String TAG = "AsyncThread";


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



    StringBuilder buffer = null;

    try {
        socket = new Socket(SERVER_IP, SERVER_PORT);
        String dataForSocket = "A LARGE STRING TO SERVER";

    /////////////---------CODE TO READ & WRITE------------\\\\\\\\\\\\\\\\\\\\\\\\\

    InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
    Log.i("ClientActivity", "Connecting...");
    Socket socket = new Socket(serverAddr, SERVER_PORT);


        Log.i("ClientActivity", "Sending command.");
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
        // where you issue the commands
        out.println(dataForSocket);
        Log.i("ClientActivity", "Sent.");

        final BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        buffer = new StringBuilder();

        buffer.append(input.readLine());
        Log.d(TAG, "Inside thread : input.readLine() == " + input.readLine());
        Log.d(TAG, "Inside thread : buffer == "+buffer);

}//TRY closes here....
catch(Exception e){
    e.printStackTrace();
}//catch closes here.....

return buffer.toString().trim();
 }//doInBackground CLOSES HERE.....
}//AsyncThread class closes here.....

获得以下参考: how to read socket input stream in Android

现在,我只需要使用服务而不是Async,&amp;我的问题已经解决了。