Search code examples
javaandroidsocketsserversocket

TCP android Server not recieving first message from Java TCP client


I have a TCP Client in java and TCP server in android. Sending message form Server to Client works fine but when message is sent from Client to server(android) it doesn't shows the message but when same process is repeated second time, server shows the message i.e send button of java client should be pressed twice then only message is shown in the android app.

Java Client Code

//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 Server Code

 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();
    }


}

Solution

  • The problem is the following code part:

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

    In the first line you are reading the line and ignoring it. Therefore you are throwing away every second line.

    The correct way to read and use it for the while loop is this:

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