user3591497
user3591497

Reputation: 1

InputStream not reading anything

This is my Client side code.Client sends mobile no. to the Servlet. Servlet then sends a string to the client which is to be base64 encoded by Client.Servlet then reads this encoded string from Inputstream.But it reads nothing from the Inputstream.

    private class SendIP extends AsyncTask<Void, Void, String>
{
    EditText et; 
    Integer value;
    protected String doInBackground(Void... params) 
    {

        try
        {

               URL url = new URL("http://10.0.2.2:8080/New/MyServlet");
               URLConnection connection = url.openConnection();
               connection.setDoOutput(true);

               et = (EditText) findViewById(R.id.editText1);
               number = et.getText().toString();
               OutputStreamWriter out=new OutputStreamWriter(connection.getOutputStream());

               out.write(number+"\n");//sending mobile no. to server
               out.flush();
               out.close();

               BufferedReader in=new BufferedReader(new InputStreamReader (connection.getInputStream()));
               message=in.readLine();
               str=in.readLine();//reading the string sent by server

               in.close();
               byte[]data=str.getBytes("UTF-8");
               base64=Base64.encodeToString(data, Base64.DEFAULT);//encoding the string into base64
               OutputStreamWriter out1=new OutputStreamWriter(connection.getOutputStream());
               out1.write(base64+"\n");
               out1.flush();//sending the encoded string to server
               out1.close();
               //out.close();






        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        /*catch(IOException e)
        {
            e.printStackTrace();
        }
        */

This is my Server side code.

       protected void doPost(HttpServletRequest request, HttpServletResponse response) throws       ServletException, IOException {
    // TODO Auto-generated method stub
    //PrintWriter out=response.getWriter();

    try
    {
    byte buf[]=new byte[40];
    byte b[]=new byte[40];

    ServletInputStream sin=request.getInputStream();

    num=sin.readLine(buf, 0, buf.length);//reading mobile no. from client
    String message=new String(buf);
    System.out.println(message);

    //response.setStatus(HttpServletResponse.SC_OK);

    }
        }
          }
      }
OutputStreamWriter writer=new  OutputStreamWriter(response.getOutputStream());
    res="mobile no. received";
    str="hello client";
    writer.write(res+"\n");//sending mobile number to server
    writer.write(str+"\n");//sending string to be encoded to server
    writer.flush();
    num1=sin.readLine(b,0,b.length);//reading the encoded string from client
    base64=new String(b);
    System.out.println(num1);//this prints -1
    sin.close();

    response.setStatus(HttpServletResponse.SC_OK);

    writer.close();

    }
    catch(IOException e)
    {
        response.getWriter().println(e);
    }

Upvotes: 0

Views: 1206

Answers (2)

Stephen C
Stephen C

Reputation: 719679

Three problems:

  • You have set doOutput to true, but you haven't set doInput to true.

  • Your server side seems to be coded for a POST request, but the client side is not setting any method type. (It will send a GET request by default ...)

  • You are not checking the status for the URL connection. If the status indicates an error, then getInputStream() will give you an empty stream. That might happen (for example) if the request never gets to your server-side doPost method ...


UPDATE

Ah ... I see what you are trying to do.

    OutputStreamWriter out1 = 
           new OutputStreamWriter(connection.getOutputStream());

I doubt that that will work. When you closed the original output stream, that should have closed the output side of the underlying Socket. It can't be reopened. And if it didn't close the Socket I would expect the getOutputStream() call to throw

    ProtocolException("Cannot write output after reading input.")

Basically, the HttpURLConnection API is not designed to work like this ...


What you appear to be trying to do is to implement something like "websockets" ... where the client and the server negotiate that the HTTP socket is kept open after the HTTP request and reply and is then used for "other things". If you want to do that, you should look at the Java EE javax.websocket and javax.websocket.server packages; see here.

There are other implementations of websockets too; Google for "java websocket", and a cooperating client and server could do this in other non-standard ways. But this is outside what the HttpURLConnection API or implementation will support.

Upvotes: 1

greenapps
greenapps

Reputation: 11224

What you want is impossible. Well thats what I think. I've never seen such a http communication before. If it would be possible it would be very interesting though.

In one 'session' you want the server to receive something from the client. Send something back. Which the client reads. And then the client sends again data which the server should read.

Upvotes: 0

Related Questions