English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article takes an example to describe the implementation of TCP and UDP client communication functions in Android programming. Shared with everyone for reference, as follows:
In the process of Android development, it is inevitable that you need to develop TCP/Programs for UDP communication, the following two pieces of code respectively introduce TCP/An instance of UCP passing through:
Code one: TCP Communication:
private void tcpdata() {}} try { Socket s = new Socket("192.168.0.25, 65500); // outgoing stream redirect to socket OutputStream out = s.getOutputStream(); // Note that the second parameter is set to true, which will automatically flush, otherwise you need to manually operate out.flush(). PrintWriter output = new PrintWriter(out, true); output.println("Hello IdeasAndroid! Fake IP is:", + SIMCardToIP("13512345006")); InputStream inputStream = s.getInputStream(); DataInputStream input = new DataInputStream(inputStream); byte[] b = new byte[10000]; int length = input.read(b); inputReader = new InputStreamReader(inputStream); String Msg = new String(b, 0, length, "gb2312"); Toast.makeText(TcpTest.this, Msg, 1000).show(); Log.d("Tcp Demo", "message From Server:", + Msg); s.close(); } e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
The following code is the process of UDP communication:
public String send(String msg) { StringBuilder sb = new StringBuilder(); InetAddress local = null; try { local = InetAddress.getByName("192.168.0.25"); // Local testing } e.printStackTrace(); } try { dSocket = new DatagramSocket(); // Note that you must set the permissions in the configuration file first, otherwise it will throw an insufficient permissions exception. } e.printStackTrace(); } int msg_len = msg == null ? 0 : msg.length;63; 0 : msg.length(); DatagramPacket dPacket = new DatagramPacket(msg.getBytes(), msg_len, local, SERVER_PORT); try { dSocket.send(dPacket); } catch (IOException e) { e.printStackTrace(); } try { dSocket.receive(dPacket); sb.append(new String(dPacket.getData())); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } dSocket.close(); return sb.toString(); }
Readers who are interested in more content related to Android can check the special topics on this site: 'Summary of Android Communication Methods', 'Android Development Tutorial for Beginners and Advanced', 'Summary of Android Debugging Techniques and Common Problem Solving Methods', 'Summary of Android Multimedia Operation Techniques (audio, video, recording, etc.)', 'Summary of Android Basic Component Usage', 'Summary of Android View View Techniques', 'Summary of Android Layout Layout Techniques', and 'Summary of Android Widget Usage'.
I hope the content described in this article will be helpful to everyone in Android program design.
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal responsibility. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please report via email to codebox.com (replace # with @ when sending an email) and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.