Client code:
Activity
Package Com.example.sockettest;import Java.io.*;import Java.net.socket;import java.net.unknownhostexception;import Android.app.activity;import Android.os.bundle;import Android.text.textutils;import Android.view.View;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.toast;public class Main extends activity implements Onclicklistener {/** * called when the activity is first created. */Private EditText edtmsgcontent; Private Button btnsend; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Initview (); } private void Initview () {Setcontentview (r.layout.main); Edtmsgcontent = (EditText) Findviewbyid (r.id.msgcontent); Btnsend = (Button) Findviewbyid (r.id.btnsend); Btnsend.setonclicklistener (this); } public void OnClick (View bt) {try {String msg = Edtmsgcontent.gettext (). toString (); if (! Textutils.isempty (msg)) sendmsg (msg); else {Toast.maketext (this, "CCCC", Toast.length_long); Edtmsgcontent.requestfocus (); }} catch (Exception e) {e.printstacktrace (); }} public void Sendmsg (Final String msg) throws Unknownhostexception, IOException {new Thread () { @Override public void Run () {try {String IP = "192.168.1.155"; int port = 1818; Socket socket = NULL; Socket = new Socket (IP, port); BufferedWriter writer = new BufferedWriter (New OutputStreamWriter (Socket.getoutputstream ())); Writer.write (msg); Writer.flush (); Writer.close (); Socket.close (); } catch (Unknownhostexception e) { E.printstacktrace (); } catch (IOException e) {e.printstacktrace (); }}}.start (); }}Main.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:orientation=" vertical " android:layout_width=" fill_parent " android:layout_height=" Fill_parent " ><textview android:layout_width=" fill_parent " android:layout_height=" Wrap_ Content " android:text=" Hello " /><edittext android:text=" EditText "android:layout_height=" Wrap_ Content "Android:layout_width=" fill_parent "android:id=" @+id/msgcontent "></edittext><button android: layout_height= "Wrap_content" android:text= "Send" android:layout_width= "fill_parent" android:id= "@+id/btnsend" > </Button></LinearLayout>
Add Network access permissions to the Androidmenifest.xml file:
<uses-permission android:name= "Android.permission.INTERNET"/>
Server:
Instead of Web engineering, run the main function directly.
Package Test;import Java.io.bufferedreader;import Java.io.ioexception;import java.io.inputstreamreader;import Java.io.printwriter;import Java.net.serversocket;import Java.net.socket;public class Server {public static void main ( String[] args) {BufferedReader in; PrintWriter out;try{serversocket ss = new ServerSocket (43211); System.out.println ("1"); Socket socket = ss.accept (); System.out.println ("2"), in = new BufferedReader (New InputStreamReader (Socket.getinputstream ())), out = new PrintWriter (Socket.getoutputstream (), true); while (true) {String line = In.readline (); System.out.println (line), if (Line.equals (")") {break;} Out.println ("Hello:" + line); Out.flush ();} Out.close (); In.close (); Socket.close ();} catch (IOException e) {}}}
Attach source Code link address
Android for socket communication