0

Am new to android and i have an issue with socket programming.

class ClientAsycTask extends AsyncTask<String,Void,String>{
    ProgressDialog progress;
    @Override
    protected void onPreExecute() {
        progress = new ProgressDialog(MainActivity.this);
        progress.setMessage("Connection Server Please wait");
        progress.setIndeterminate(true);
        progress.show();
    }

    @Override
    protected String doInBackground(String... params) {
        String result = null;
        Socket socket = new Socket();
        String host = "systemteq.gotdns.com";
        Integer port = 4999;
        DataOutputStream s_out = null;
        BufferedReader s_in = null;
        try{
            socket.connect(new InetSocketAddress(host, port));
            s_out = new DataOutputStream( socket.getOutputStream());
            s_in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            byte[] messages = new byte[]{0x10,0x01,0x03,0x00,0x00};
            s_out.write(messages);
            String response;
            while((response = s_in.readLine())!=null){
                result += response;
            }
            //result = "Successfully"+s_in.read();//s_in.readLine();
            socket.close();

        }catch (Exception e){
            Toast.makeText(MainActivity.this,"systemteq.gotdns.com:4999 not connected",Toast.LENGTH_SHORT).show();
        }
        return result;
    }

    @Override
    protected void onPostExecute(String s) {
        if(progress.isShowing()){
            progress.dismiss();
        }
        Toast.makeText(MainActivity.this,"Connected "+s,Toast.LENGTH_SHORT).show();
        tv.setText(s);
    }
}

Above code sends a hexadecimal code to server and server responds with some hexadecimal code, lets say 10 01 20 10 00.[five bytes].

But when i run above code progress dialog never close. after working around i found that s_in.readLine() has some issue reading the response from server.

I have went through lots of links but i was not successful while solving this issue. Kindly help me to crack this.

UPDATE:-

After using DataInputStream:-

s_in = new DataInputStream(socket.getInputStream());
            byte[] messages = new byte[]{0x10,0x01,0x03,0x00,0x00};
            s_out.write(messages);
            String response;
            System.out.println("START");
            result = s_in.readUTF();

It still does not take response byte. Though request is successful.

Update 2

byte m = s_in.readByte();
result = Byte.toString(m);

Now am getting byte value but i should get 5 bytes , instead of getting 5 bytes am getting only one. Please help me with this thing

0

1 Answer 1

3

If you are waiting for binary data, do not use InputStreamReader, try DataInputStream instead.
The readLine() waits for '\n', '\r', "\r\n" or the end of the reader.

UPDATE: if you know exact number of bytes, read them into array.

byte[] buf = new byte[5];
s_in.read(buf);
Sign up to request clarification or add additional context in comments.

4 Comments

If the server sends you bytes, why you trying always read them as String? Read bytes to byte[] buffer. It is easy. Strings are more complicated.
Hai @Eugene Krivenja thanks, this works fine now but now it is taking only one byte at a time. as i know it will return 5 bytes. do you have any idea
@RKD No, readUTF() does not do the same thing as readLine(), as the most cursory glance at the documentation will show.
hi @EJP thanx , actually my response when i use readByte is 24 when i convert this to hex it is 18 which is then first byte of the response. Response is 18 06 05 00 00. Can you just tell me how can i take all this byte

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.