Method (TCP) in Android and server communication efficient security integrity

Source: Internet
Author: User

I used to use sockets to achieve emulator and PC communication, card for a few days, finally ask others to finally be able to connect.The reason for the error is that the IP and PORT,IP are to be used native IP (ipconfig in cmd input. Note: Each time the IP is different, ADSL is dynamic IP, port with emulator name, 5554 or 5556.java sever file to be built Javaproject execution. Androidproject cannot execute Java (Run application)

Client code:Java code
Package com.yarin.android.Examples_08_04;   Import Java.io.BufferedReader;   Import Java.io.BufferedWriter;   Import Java.io.InputStreamReader;   Import Java.io.OutputStreamWriter;   Import Java.io.PrintWriter;   Import java.net.InetAddress;     Import Java.net.Socket;   Import android.app.Activity;   Import Android.os.Bundle;   Import Android.util.Log;   Import Android.view.View;   Import Android.view.View.OnClickListener;   Import Android.widget.Button;   Import Android.widget.EditText;     Import Android.widget.TextView;              public class Activity01 extends Activity {private final String Debug_tag = "Activity01";       Private TextView Mtextview=null;       Private EditText Medittext=null;             Private Button Mbutton=null;           @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                      Setcontentview (R.layout.main); Mbutton = (Button) Findviewbyid (r.id.bUTTON01);           mtextview= (TextView) Findviewbyid (R.ID.TEXTVIEW01);                      medittext= (EditText) Findviewbyid (r.id.edittext01);               Login Mbutton.setonclicklistener (new Onclicklistener () {public void OnClick (View v)                   {SOCKET socket = NULL;                    String message = Medittext.gettext (). toString () + "\ r \ n"; try {//create socket SOCKET = new Socket ("116.29.27.138", 555 4);                       View native IP, each boot is different//socket=new socket ("192.168.1.110", 50000); Send message to server printwriter out = new PrintWriter (New BufferedWriter (SOCKET.G                             Etoutputstream ())), true);                                               OUT.PRINTLN (message); Receive messages from server BufferedReader br = new BufferedReader (New InputStreamReader (sockEt.getinputstream ()));                                               String msg = Br.readline ();                       if (msg! = null) {Mtextview.settext (msg);                       } else {mtextview.settext ("Data Error!");                       }//Close stream out.close ();                       Br.close ();                    Close socket Socket.close ();                       } catch (Exception e) {//Todo:handle Exception                   LOG.E (Debug_tag, e.tostring ());       }               }           }); }} package com.yarin.android.Examples_08_04; Import Java.io.BufferedReader; Import Java.io.BufferedWriter; Import Java.io.InputStreamReader; Import Java.io.OutputStreamWriter; Import Java.io.PrintWriter; Import Java.net.InetAddress; Import Java.net.Socket; Import android.app.Activity; Import Android.os.Bundle; Import Android.util.Log; Import Android.view.View; Import Android.view.View.OnClickListener; Import Android.widget.Button; Import Android.widget.EditText; Import Android.widget.TextView;  public class Activity01 extends Activity {private final String Debug_tag = "Activity01"; private TextView mtextview=null; Private EditText Medittext=null;  Private Button Mbutton=null; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.main); Mbutton = (Button) Findviewbyid (R.ID.BUTTON01); mtextview= (TextView) Findviewbyid (R.ID.TEXTVIEW01); medittext= (EditText) Findviewbyid (r.id.edittext01); Login Mbutton.setonclicklistener (new Onclicklistener () {public void OnClick (View v) {socket socket = NULL; String message = Medittext.gettext (). toString () + "\ r \ n"; try {//create socket SOCKET = new Socket ("116.29.27.138", 5554),///view native IP, each boot is different//socket=new socket ("192.168.1.110 ", 50000); Send message to server printwriter out = new PrintWriter (New BufferedWriter (Socket.getoutputstream ())),      true); OUT.PRINTLN (message); Receive messages from server BufferedReader br = new BufferedReader (New InputStreamReader (Socket.getinputstream ())); String msg = Br.readline (); if (msg! = null) {Mtextview.settext (msg);} else {mtextview.settext ("Data Error!");}//Close stream out.close (); Br.close (); Close socket Socket.close (); } catch (Exception e) {//Todo:handle Exception log.e (Debug_tag, e.tostring ());}} });  } }







Server-side code:Java code
Package com.yarin.android.Examples_08_04;   Import Java.io.BufferedReader;   Import Java.io.BufferedWriter;   Import Java.io.InputStreamReader;   Import Java.io.OutputStreamWriter;   Import Java.io.PrintWriter;   Import Java.net.ServerSocket;     Import Java.net.Socket; public class Server implements Runnable {public void run () {try {//create Ser               Versocket ServerSocket serversocket = new ServerSocket (5554);                   while (true) {//accepts client requests for Socket clients = serversocket.accept ();                   System.out.println ("accept"); try {//Receive client message BufferedReader in = new BufferedReader (New in                       Putstreamreader (Client.getinputstream ()));                       String str = in.readline ();                         System.out.println ("read:" + str); Send a message to the server PrinTwriter out = new PrintWriter (new BufferedWriter (New OutputStreamWriter (Client.getoutputstream ())), true);                        OUT.PRINTLN ("Server Message");                       Close flow out.close ();                   In.close ();                       } catch (Exception e) {System.out.println (E.getmessage ());                   E.printstacktrace ();                       } finally {//close client.close ();                   System.out.println ("close");           }}} catch (Exception e) {System.out.println (E.getmessage ()); }}//main function.           Open server public static void main (String a[]) {Thread desktopserverthread = new Thread (new server ());       Desktopserverthread.start ();   }   }


Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Method (TCP) in Android and server communication efficient security integrity

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.