ref:http://blog.chinaunix.net/uid-20771867-id-3416509.html
cmd:telnet localhost 5554
redir add udp:8002:8001 (将PC 8002端口映射为模拟器 8001端口)
模拟器-SERVER:ServerSocket server = new ServerSocket(8001);//zcl:7100改为2888
public static void ReceiveUDP() throws IOException {
// TODO Auto-generated method stub
ReliableUDP udp = new ReliableUDP(null, "172.17.21.231", 8001, 8001);
byte[] revBuf = new byte[1024];
int res = udp.ReadFixedLength(revBuf, 1);
}
PC-CLIENT:Socket socket = new Socket("127.0.0.1", 8002);
《TCP/UDP测试工具》 UDP:127.0.0.1 port 8002 (注意:8002是PC的映射端口)
ok ——》 最终找到原因:是redir add会抢占端口:
ok ——》解决方法:环境设置——关键是一定要使用remotePC,单靠本机无法实现调试
+++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++++
+ localPC + + remotePC +
+ ip:172.17.22.8 + + ip:172.17.21.54 +
+ redir udp:: + + +
+ + + +
+ +++++++++++++++++++++++++++++ + + +
+ + android vm + + + +
+ + + + + +
+ + + + + +
+ + send(DestIP=remotePC, + + + Receive(localPort=) +
+ + localPort=) + + + +
+ + DestPort=) + + + +
+ + --------------------------------------> -
+ + + + + +
+ + + + + +
+ + Receive(localPort=), + + + send( DestIP=localPC +
+ + + + + localPort=) +
+ + + + + DestPort=) +
+ + + + + +
+ + <----------+---<----------+----- +
+ + + + + +
+ + + + + +
+ +++++++++++++++++++++++++++++ + + +
+ + + +
+++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++++
ok ——》code:
public class LEDControlerDriver {
public static void SendUDP() {
// TODO Auto-generated method stub
ReliableUDP udp = new ReliableUDP(null, DestIP, 8001, 8001);//to lydsw 前一个8001是模拟器-SERVER port,后一个8001是pc 接收消息的port,注意!不是pc映射给模拟器的端口(8002)
String s= "32323213";
int res = udp.SendFixedLength(s.getBytes(), s.length());
}
public static void ReceiveUDP() throws IOException {
// TODO Auto-generated method stub
ReliableUDP udp = new ReliableUDP(null, DestIP, 8001, 8001);//to lydsw 前一个8001是模拟器-SERVER port,后一个8001是pc 接收消息的port,注意!不是pc映射给模拟器的端口(8002)
byte[] revBuf = new byte[1];
int res = udp.ReadFixedLength(revBuf, 1);
}
public class ReliableUDP {
public int SendFixedLength(byte[] sendBuf,int nSendLen){
Log.i("UDP Demo", "发送数据(1):");
// check
int res = 1;
if (sendBuf.length!=nSendLen) {
return -2;
}
// create socket
DatagramSocket socket = null;
try {
socket = new DatagramSocket(null); // 指定Null很重要,否则Java会自动随机选个可用端口来绑定
socket.setReuseAddress(true); // DatagramSocket的setReuseAddress(true)方法执行后,可以允许多个DatagramSocket
// 绑定到相同的IP地址和端口,那么发送到此IP地址和端口的数据能够被复制到多个DatagramSocket
socket.setSoTimeout(10000);
socket.bind(new InetSocketAddress(this.LocalPort));
} catch (SocketException e) {
Log.i("UDP Demo", "SendFixedLength:socket create failed1:"+e.getMessage());
return -3;
}
Log.i("UDP Demo", "发送数据(2):");
// 定义一个用于发送的DatagramPacket对象
DatagramPacket outPacket = null;
try {
outPacket = new DatagramPacket(sendBuf , nSendLen , InetAddress.getByName(this.IPaddr) , this.TargetPort);
socket.send(outPacket);
Log.i("UDP Demo", "SendData:UDP发送数据:"+StringHexbyteTransform.bytesToHexString(sendBuf));
} catch (IOException e2) {
Log.i("UDP Demo", "SendData:io failed:"+e2.toString()+":"+e2.getMessage());
res = -52;
}
socket.close();
return res;
}
public int ReadFixedLength(byte[] revBuf,int nRecvLen, int timeout)
{
Log.i("UDP Demo", "Read数据(1):");
// check
int res = 1;
int len = nRecvLen;
if (-1 == nRecvLen) {
len = revBuf.length;
}
// create socket
DatagramSocket socket = null;
try {
socket = new DatagramSocket(null); // 指定Null很重要,否则Java会自动随机选个可用端口来绑定
socket.setReuseAddress(true); // DatagramSocket的setReuseAddress(true)方法执行后,可以允许多个DatagramSocket
// 绑定到相同的IP地址和端口,那么发送到此IP地址和端口的数据能够被复制到多个DatagramSocket
socket.setSoTimeout(timeout);
socket.bind(new InetSocketAddress(this.LocalPort));
} catch (SocketException e) {
Log.i("UDP Demo", "SendFixedLength:socket create failed1:"+e.getMessage());
return -3;
}
Log.i("UDP Demo", "Read数据(2):");
// 定义一个用于发送的DatagramPacket对象
DatagramPacket inPacket = null;
try {
inPacket = new DatagramPacket(revBuf , len);
socket.receive(inPacket);
ExpandRevBuffer(revBuf);
Log.i("UDP Demo", "receive:"+StringHexbyteTransform.bytesToHexString(revBuf));
} catch (IOException e2) {
Log.i("UDP Demo", "SendData:io failed:"+e2.toString()+":"+e2.getMessage());
res = -52;
}
socket.close();
return res;
}