Android socket Communication (client) sends data

Source: Internet
Author: User
Tags server port

/** Clientsocket Communication Class **/

public class Clientsocket {

   /** server address */    private String serverurl= "192.168.124.214";   /** server port */    private int serverport=8888;   /* Send command thread */    class Sendcommandthread extends thread{    &N Bsp   private string command;        public sendcommandthread (string acommand) {      & nbsp     This.command = acommand;       }        public void run () {  &N Bsp        try{               socket socket = new Socket (ServerURL , ServerPort);               printwriter out = new PrintWriter (socket.getoutputstr EAM ());               out.print (command);               out.flush ();           }catch (unknownhostexception e) {      &N Bsp    } catch (IOException E) {           }       }   }    /** send file thread **/    class Sendfilethread extends thread{        private byte bytebuffer[] = NE W byte[1024];        Private outputstream outsocket;        Private BYTEARRAYOUTPU TStream myoutputstream;        public sendfilethread (Bytearrayoutputstream myoutputstream) {            This.myoutputstream = myoutputstream;            try {&NB Sp               myoutputstream.close ();           } catch (IOException e) {                e.printstacktrace ();           }&N Bsp      }        public void run () {            try {&NBSp               SOCKET socket = new Socket (serverurl,serverport);      &NB Sp         Outsocket = Socket.getoutputstream ();               // Write header information                string msg = Java.net.URLEncoder.encode ("Phonevideo", "Utf-8" );                byte[] buffer = msg.getbytes ();        &NBSP ;       outsocket.write (buffer);                Bytearrayinputstream InputStream = new Bytearrayinputstream (Myoutputstream.tobytearray ());              &N Bsp int amount;                while ((Amount = Inputstream.read (bytebuffer))! =-1) {& nbsp                   Outsocket.write (bytebuffer,0,amount);      & nbsp &nbsp      }                Myoutputstream.flush ();                myoutputstream.close ();                SOCKET.C Lose ();           } catch (IOException e) {                E.printstacktrace ();           }       }      & nbsp }}

Android Socket Communication (client) Send data

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.