TCP UDP commonly used network communication mode, the specific content and transmission mode can be Baidu search here no longer repeat,
The main thing I do is to send out the source code, for everyone's reference.
First, after the TCP establishes the connection, both sides of the communication can carry on the data transmission simultaneously, secondly, he is full duplex, in the guarantee reliability, uses the time-out retransmission and the piggyback confirmation mechanism.
Common TCP connection diagrams
Server-side code
try {Boolean Endflag = false; ServerSocket ss = new ServerSocket (12345); while (!endflag) {//waits for client to connect Socket s = ss.accept (); BufferedReader input = new BufferedReader (Newinputstreamreader (S.getinputstream ())); Note that the second parameter data is true will automatically flush, otherwise you need to manually operate Output.flush () PrintWriter output = Newprintwriter (s.getoutputstre AM (), true); String message = Input.readline (); LOG.D ("Tcp Demo", "Message from Client:" +message); OUTPUT.PRINTLN ("message received!"); Output.flush (); if ("ShutDown". Equals (Message)) {endflag=true; } s.close (); } ss.close (); } catch (Unknownhostexception e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); }
Client code:
Try { socket s = new socket ("localhost", 12345); Outgoing stream redirect to socket OutputStream out = S.getoutputstream (); Note that the second parameter data is true will automatically flush, otherwise you need to manually operate Out.flush () printwriter output = new PrintWriter (out, true); Output.println ("Hello ideasandroid!"); BufferedReader input = new BufferedReader (Newinputstreamreader (S . getInputStream ())); Read line (s) String message = Input.readline (); LOG.D ("Tcp Demo", "Message from Server:" + message); S.close (); } catch (Unknownhostexception e) { e.printstacktrace (); } catch (IOException e) { e.printstacktrace ();
UDP Connection Diagram
UDP server-side code:
The UDP server listens on ports Integer port = 12345; The size of the bytes received, the data sent by the client cannot exceed this size byte[] message = new byte[1024]; try {//Establish socket connection Datagramsocket datagramsocket = new Datagramsocket (port); Datagrampacket datagrampacket = new Datagrampacket (message, message.length); try {while (true) {//Prepare to receive data Datagramsocket . receive (Datagrampacket); LOG.D ("UDP Demo", datagrampacket.getaddress (). Gethostaddress (). ToString () + ":" + New String (Datagrampacket.getdata ())); }} catch (IOException e) {e.printstacktrace (); }} catch (SocketException e) {e.printstacktrace (); }
Client code:
public static void Send (String message) {message = (message = = null?) "Hello ideasandroid!": message); int server_port = 12345; Datagramsocket s = null; try {s = new datagramsocket (); } catch (SocketException e) {e.printstacktrace (); } inetaddress local = null; try {//Replace server-side IP local = inetaddress.getbyname ("localhost"); } catch (Unknownhostexception e) {e.printstacktrace (); } int msg_length = Message.length (); byte[] Messagemessagebyte = Message.getbytes (); Datagrampacket p = new Datagrampacket (Messagebyte, Msg_length, Local, server_port); try {s.send (P); } catch (IOException e) {e.printstacktrace (); } }
Android's TCP and UDP transport simple program