简体   繁体   中英

socket on android, every second request disappears

I'm writing simple software, PC is socket server, android connects to it and then whatever I write on keyboard on PC, android gets it and simply logs.

Android code: package com.example.socket;

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

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {


protected void onCreate(Bundle savedInstanceState) {


        Socket echoSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            echoSocket = new Socket("192.168.2.3", 4444);
            out = new PrintWriter(echoSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(
                    echoSocket.getInputStream()));
        } catch (UnknownHostException e) {
            Log.e("j", "Don't know about host: JANIPC.");
            System.exit(1);
        } catch (IOException e) {
            Log.e("j", "Can't connect to Jani-PC");
            System.exit(1);
        }

        try {
        BufferedReader stdIn = new BufferedReader(new InputStreamReader(
                echoSocket.getInputStream()));
        String userInput;


            while ((userInput = stdIn.readLine()) != null) {
                //out.println(userInput);
                Log.e("j", "echo: " + in.readLine());
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }


}

And here goes my PC Java code:

package jani.study.socketserver;


import java.io.*;
import java.net.*;
/**
 *
 * @author Jani
 */
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
   ServerSocket serverSocket = null;
    try {
        serverSocket = new ServerSocket(4444);
    } catch (IOException e) {
        System.err.println("Could not listen on port: 4444.");
        System.exit(1);
    }

    Socket clientSocket = null;
    try {
        clientSocket = serverSocket.accept();
    } catch (IOException e) {
        System.err.println("Accept failed.");
        System.exit(1);
    }

    PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
    BufferedReader in = new BufferedReader(
            new InputStreamReader(
            System.in));
    String inputLine;

    System.out.println("Connected!");
    /*while ((inputLine = in.readLine()) != null) {
         out.println(inputLine);

    }*/
    Thread.sleep(1000);
    out.println("1");
    Thread.sleep(1000);
    out.println("2");
    Thread.sleep(1000);
    out.println("3");
    Thread.sleep(1000);
    out.println("4");
    Thread.sleep(1000);
    out.println("5");
    Thread.sleep(1000);
    out.println("6");
    out.close();
    in.close();
    clientSocket.close();
    serverSocket.close();
}


}

In Android debugger, I only get every second request, I mean, I only get: echo: 2 echo: 4 echo: 6

what am I doing wrong? please help

You're using two BufferedReader s, one in and one stdIn both reading from the socket. You are consuming the current line first in the while loop, then consuming the next in the Log statement.

Try this:

Log.e("j", "echo: " + userInput);

You call readLine twice in the reader and only print the results of every other call to readLine . You probably want:

        while ((userInput = stdIn.readLine()) != null) {
            //out.println(userInput);
            Log.e("j", "echo: " + userInput);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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