简体   繁体   English

android上的套接字,第二个请求消失

[英]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. 我在写简单的软件,PC是套接字服务器,android连接到它,然后无论我在PC上用键盘写什么,android都可以获取它并简单地记录日志。

Android code: package com.example.socket; Android代码: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: 这是我的PC Java代码:

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 在Android调试器中,我只会得到第二个请求,我的意思是,我只会得到: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. 您正在使用两个BufferedReader S,一个in和一个stdIn无论是从套接字读取。 You are consuming the current line first in the while loop, then consuming the next in the Log statement. 您先在while循环中使用当前行,然后在Log语句中使用下一行。

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 . 您在阅读器中两次调用readLine ,并且仅打印对readLine的所有其他调用的结果。 You probably want: 您可能想要:

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

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

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