Exercise caution when developing a UDP data transmission system between Android phones
TIPS:
1. The communication between mobile UDP and a fixed IP is not a big problem, but it may also cause a slight packet loss. The problem cannot be found here. Do you think there are special considerations for UDP implementation at the android network layer.
2. UDP communication between mobile phones and UDP is basically unreliable. Therefore, it is very unreliable to see the mobile phone send video data packets through RTP. It will not be able to receive data for a while, and you will be able to receive data for a while. Currently, no reason is found.
3. If you want to transfer the RTP video between mobile phones, you can transfer the RTP data packet through the server.
Paste client:
package com.jouhu.udptestclient;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class UDPTestClientActivity extends Activity {
/** Called when the activity is first created. */
public EditText et;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.button1);
et = (EditText)findViewById(R.id.editText1);
et.setText("5006");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String data = "just a test";
// TODO Auto-generated method stub
DatagramSocket socket;
try {
socket = new DatagramSocket();
//socket.setBroadcast(true);
InetAddress serverIP = InetAddress.getByName("192.168.1.109");
DatagramPacket packet = new DatagramPacket(data.getBytes(), data.length(),serverIP,Integer.parseInt(et.getText().toString()));
socket.send(packet);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
The server adopts the handler + thread model, which I will elaborate on separately. This is a common model that can solve the problem of communication between threads and the UI.
-End-
The same address: http://doandroid.info/2012/03/14/%E5%BC%80%E5%8F%91android%E6%89%8B%E6%9C%BA%E9%97%B4udp%E4%BC%A0%E8%BE%93%E6%95%B0%E6%8D% AE %E7%B3%BB%E7%BB%9F%E8%A6%81%E6%85%8E%E9%87%8D/
You are welcome to repost and indicate the source.