Simple TCP and UDP transmission programs for Android
The network communication mode that is often used for tcp udp. The specific content and transmission mode can be searched by Baidu,
I am mainly sending the source code for your reference.
First, after the TCP connection is established, both parties can transmit data at the same time, and secondly, it is full-duplex. In terms of reliability, timeout retransmission and RST validation mechanisms are used.
Common tcp connection Diagram
Server code <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3ryb25np1_vcd4kpha + PHN0cm9uZz48L3N0cm9uZz48cHJlIGNsYXNzPQ = "brush: java;"> try {Boolean endFlag = false; ServerSocket ss = new ServerSocket (12345); while (! EndFlag) {// wait for the client to connect to Socket s = ss. accept (); BufferedReader input = new BufferedReader (newInputStreamReader (s. getInputStream (); // note that if the second parameter is set to true, it will be automatically flush; otherwise, you need to manually operate the output. flush () PrintWriter output = newPrintWriter (s. getOutputStream (), true); String message = input. readLine (); Log. d ("Tcp Demo", "message from Client:" + message); output. println ("message received ed! "); // 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 if the second parameter is set to true, it will be 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 DiagramUDP server code:
// UDP server listening port Integer port = 12345; // the size of the received byte. The data sent by the client cannot exceed this size byte [] message = new byte [1024]; try {// establish a 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 it with the server IP address 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 ();}}