Use SOCKET to send messages in Android

Source: Internet
Author: User

Source code
Http://www.eoeandroid.com/thread-114907-1-1.html

Self-implemented Android curve chart
Http://www.eoeandroid.com/thread-207218-1-1.html

Android game physical engine source code
Http://www.eoeandroid.com/thread-207445-1-1.html

Socketserver class

 Package  Com. Socket. server;  Import Java. Io .* ;  Import Java.net .* ;  Import  Android. content. context;  Import  Android. content. intent; Public   Class  Socketserver {serversocket sever; Context context;  Public Socketserver (context, Int  Port ){  Try  {  //  Instantiate the serversocket input port number Sever = New  Serversocket (port );  This . Context = Context ;} Catch  (Ioexception e) {e. printstacktrace ();}}  Public   Void  Controlactionanswermsg (string Str ){  Final Intent intent = New  Intent (); intent. setaction ( "Socket_action" ); Intent. putextra ( "Message" , STR); context. sendbroadcast (intent );}  Public   Void  Beginlisten (){ //  Put the socket listener into the thread to prevent the main thread from being affected.  New  Thread (){  Public   Void  Run (){  //  Because it is a process of continuous listening, it uses an endless loop.  While ( True  ){  Try  {  //  Here is the listener operation  Final Socket socket = Sever. Accept (); bufferedreader in;  Try  {  //  Read the stream information here after listening to the message In = New Bufferedreader ( New  Inputstreamreader (socket. getinputstream (), UTF-8" ));  //  Put the stream information in printwriter and read it out. Printwriter out = New  Printwriter (socket. getoutputstream ()); While (! Socket. isclosed () {string STR;  //  Read string STR = In. Readline (); Out. println ( "Hello! World !! "+ Str); Out. Flush ();  If (STR = Null | Str. Equals ("end" ))  Break  ; System. Out. println (STR); controlactionanswermsg (STR);} socket. Close ();}  Catch (Ioexception e) {e. printstacktrace ();}}  Catch  (Ioexception e) {e. printstacktrace () ;}}}. start ();}} 

Clientsocket class

 Package  Com. Socket. PC;  Import Java. Io .* ;  Import Java.net .* ;  Public   Class  Clientsocket {  Static  Socket Client; Public Clientsocket (string site, Int  Port ){  Try  {  //  Input the IP address and port number to instantiate the socket. The port numbers on both sides must be consistent. Client = New  Socket (site, Port); system. Out. println ( "Client is created! Site: "+ site +" port: "+ Port );}  Catch  (Unknownhostexception e) {e. printstacktrace ();}  Catch (Ioexception e) {e. printstacktrace ();}}  Public  String sendmsg (string MSG ){  Try  {  //  Bufferedreader in = new bufferedreader (New inputstreamreader (  //  Client. getinputstream ()));  //  Convert the string to the output stream and send it out. Printwriter out = New  Printwriter (client. getoutputstream (); Out. println (MSG); Out. Flush (); //  Return in. Readline (); } Catch  (Ioexception e) {e. printstacktrace ();}  Return "" ;}  Public   Void  Closesocket (){  Try  {Client. Close ();}  Catch  (Ioexception e) {e. printstacktrace ();}}} 

Server and client source code

 

Related Article

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.