简体   繁体   中英

Client-server socket communication

Problem

How to send UTF-8 data between the server and the client, if I can use on client only

inputStream.read()

?

Docs

Reads a single byte from this stream and returns it as an integer in the range from 0 to 255. Returns -1 if the end of the stream has been reached.

Without reader.readLine() and any another. (With reader I cant see end of stream)

Help please!

(full code:)

int c;
String str = new String();
while ((c = inputStream.read( )) != -1)
{
    char ch = (char)c;
    if(ch == '\n')
    {
        Log.v("", str);
        final String data = str;

        runOnUiThread(new Runnable()
        {
            @Override
            public void run()
            {
                String put[] = data.split("#");
                try
                {
                    //cmd parsing
                }
                catch(Exception e)
                {
                    //stop connection
                }
            }
        });
        str = "";
    }else{
        str += Character.toString(ch);
    }
}
//Communication error

Help please

You might want to take a look at this previous post. There's a couple of good options on there. The read() method can be overloaded with different parameters, so you can read one byte, or n bytes. Check out the full documentation here . Basically, you'll have to read in the raw bytes, then convert them to ASCII characters. Also, I'm curious as to why you can't use BufferedReader or an equivalent class?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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