问题在android中接收UDP消息 [英] problem receive UDP message in android

查看:64
本文介绍了问题在android中接收UDP消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

the server which is in my emulator dose never receive the server class is this :




public class Server implements Runnable {
 
        
        public static final String SERVERIP = "192.168.1.102"; // 'Within' the emulator!
        public static final int SERVERPORT = 2060;
         
        public void run() {
                
        	            Log.e("receive","new socket");
                        DatagramSocket socket = null;
						try {
							socket = new DatagramSocket(SERVERPORT);
						} catch (SocketException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} 
                       
                        byte[] buf = new byte[1024];
                        Log.e("receive","new packet");
                        DatagramPacket packet = new DatagramPacket(buf, buf.length);
                        try {
							socket.setSoTimeout(10000);
						} catch (SocketException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
                       
                        Log.e("receive","receive !");
                        try {
							socket.receive(packet);
							Log.e("receive",new String(packet.getData()));
														
						}catch (SocketTimeoutException e){Log.e("receive","time out");}
                        catch (IOException e) {Log.e("receive","error 1");
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
                        
                        Log.e("receive","end");
                        
                        
                        
                
        }
}




and the sender is :




public class UDPClient
{
        public static void main(String args[]) throws IOException
        {
        	InetAddress ip=InetAddress.getByName("192.168.1.102");
        	DatagramSocket socket=new DatagramSocket();
            byte[] outData = ("helloooo").getBytes();
            
            DatagramPacket out = new DatagramPacket(outData,outData.length,ip ,2060);
            socket.send(out);
            System.out.println("Send >>> ");
                        
            
        }
}

推荐答案

UDP的一个功能是通常在第1页的任何描述中用粗体字母书写,不保证传送UDP数据包。发件人使用Hail Mary吐出数据,协议堆栈中没有任何层可以回顾。



有关进一步成功的几点建议:

- 如果你的听众没有得到预期的数据包,请再次发送

- 确保在发送者之前启动了监听程序进程

- 这是否代码在同一台机器上与发送器/接收器一起工作?

- 确保你的LAN没有对指定端口进行UDP过滤(我的确实如此)

- 你是确定听众真的没有收到包裹? (您正在使用的记录器中发生了什么?是否刷新了所有缓冲区?在调试器中运行侦听器以查看它是否正在捕获任何数据。)
One of the features of UDP which is usually written in large bold letters on page 1 of any description is that UDP packets are not guaranteed to be delivered. The sender spits out the data with a "Hail Mary" and no layer in the protocol stack ever looks back.

Several suggestions for further success:
- If your listener doesn''t get an expected packet, send it again
- Make sure that the listener process is started before the sender
- Does this code work with sender/receiver on the same machine?
- Make sure your LAN doesn''t have UDP filtering for the specified port (mine does)
- Are you sure the listener really didn''t get the packet? (What happens in the logger you are using? Are all buffers flushed? Run the listener in the debugger to see if it is capturing any data).


这篇关于问题在android中接收UDP消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆