Android Socket Communication

Source: Internet
Author: User

Measured:

Socket socket = new socket (IP, internetconfig.mornitorport);
Send a message to the server
PrintWriter out = new PrintWriter (new BufferedWriter (New OutputStreamWriter (Socket.getoutputstream (), Internetsendthread.stream_decode_mode)), true);
OUT.PRINTLN (msg);

System.out.println (tag+ "end writer to IP =" +ip);
Get the return message from the server
BufferedReader readbuffer = new BufferedReader (New InputStreamReader (Socket.getinputstream (), Internetsendthread.stream_decode_mode));
res = Readbuffer.readline ();

Reference:

Method One:

Mainly the problem of server side and client coding matching

No matter what code you use

As long as the ends match

Just OK.

Do not assume that the default encoding

Try each.

Rather a line of code

Key code

public static final String bm= "GBK"; Globally defined to accommodate other parts of the system

InputStream is;
BufferedReader BR;

BW = new BufferedWriter (new OutputStreamWriter (OS,BM));
br = new BufferedReader (new InputStreamReader (IS,BM));

Bw.write (msg);

msg= new String (B,BM);

OutputStream OS = Socket.getoutputstream ();
Os.write (Str.getbytes (BM));

The code-related sections throughout the project are shown above

The effect is as follows

Method Two:

1. Server-side output:

PrintWriter out = new PrintWriter (new BufferedWriter (New OutputStreamWriter (Socket.getoutputstream (), "Utf-8"), true) ;

2. When the client receives:

BufferedReader in = new BufferedReader (New InputStreamReader (Client.getinputstream (), "UTF-8"));

String read = In.readline ();

Android Socket Communication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.