【问题标题】:TCP android Server not recieving first message from Java TCP clientTCP android服务器没有收到来自Java TCP客户端的第一条消息
【发布时间】:2018-04-27 15:54:53
【问题描述】:

我在 java 中有一个 TCP 客户端,在 android 中有一个 TCP 服务器。将消息表单服务器发送到客户端工作正常,但是当消息从客户端发送到服务器(android)时,它不显示消息,但是当第二次重复相同的过程时,服务器显示消息,即 java 客户端的发送按钮应该被按下两次然后只有消息显示在 android 应用程序中。

Java 客户端代码

//btn handelar to send message form textarea
private class ButtonHandler implements ActionListener
{
    public void actionPerformed (ActionEvent event)
    {
        String outputLine = txArea.getText ();
        System.out.println ("Client > " + outputLine);
        out.println (outputLine);
        out.flush();


    }
}

   //receiving message
public void run () throws IOException
{
    Socket socket = new Socket ("192.168.123.3",1234);
    BufferedReader in = new BufferedReader (new InputStreamReader (socket.getInputStream ()));
    out = new PrintWriter (socket.getOutputStream (), true);

    String inputLine;

    while ((inputLine = in.readLine ()) != null)
    {
        System.out.println ("Client <  " + inputLine);
        rxArea.setText (inputLine);
    }


    out.close();
    in.close();
    socket.close();
}

Android 服务器代码

 public void runTcpServer(){

    handel = new Handler();
    try {

        int a = Integer.parseInt(new SettingDialog().port);
        //creating a socket to listen on a given port
        Log.i("NW LOG","PORT OPENED SUCESSFULLY in "+a);
        final ServerSocket serverSocket = new ServerSocket(a);

        Thread listenConnection = new Thread(new Runnable() {
            @Override
            public void run() {

                try {

                    //accepting the incoming socket connection request
                    Socket workstationSocket = serverSocket.accept();
                    //reading the incoming content
                   BufferedReader  readerIn = new BufferedReader(new InputStreamReader(workstationSocket.getInputStream()));

                   outMessage = new PrintStream(workstationSocket.getOutputStream(),true);


                    Log.i("NW LOG","WATING FOR MSG");
                    while(readerIn.readLine()!=null){

                        final String incomingMsg = readerIn.readLine();

                        //setting incoming message to UI thread using handler
                        handel.post(new Runnable() {
                            @Override
                            public void run() {
                                setMessage(IN,incomingMsg);
                            }
                        });


                    }
                    outMessage.close();
                    readerIn.close();
                    workstationSocket.close();
                    serverSocket.close();



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


            }
        });listenConnection.start();

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


}

【问题讨论】:

  • but when message is sent from Client to server ???没有尝试发送内容的代码。也不在客户端。也不是在服务器端。客户端服务器仅尝试读取行。如果什么都没有发送,那将不会发生。

标签: java android sockets serversocket


【解决方案1】:

问题出在以下代码部分:

while(readerIn.readLine()!=null){
    final String incomingMsg = readerIn.readLine();
    ....

在第一行中,您正在阅读该行并忽略它。因此,您每隔一行就扔掉一次。

在 while 循环中读取和使用它的正确方法是:

String incomingMsg;
while((incomingMsg = readerIn.readLine())!=null){
      ....
}

【讨论】:

  • 谢谢!这帮助了我,但你能详细说明这是怎么发生的吗?我没明白。在检查 while 条件后,如果条件满足,它会进入循环,那么它如何跳过第一个值?我们只是在检查 reader 的值,而不是在内部或外部清除或做任何事情。
  • 每次调用readLine() 都会消耗一行,即使您没有将返回值分配给变量。因此,如果你在每个 while 循环中调用它两次,你只会得到每 2 行。
猜你喜欢
  • 2018-06-05
  • 1970-01-01
  • 1970-01-01
  • 2013-05-12
  • 2014-08-26
  • 1970-01-01
  • 2014-09-08
  • 2017-09-02
  • 2015-12-17
相关资源
最近更新 更多