Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
the server which is in my emulator dose never receive the server class is this :

Java
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 :

Java
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 >>> ");
                        
            
        }
}
Posted
Updated 16-Jul-19 8:59am
v3

1 solution

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).
 
Share this answer
 
Comments
farid wahed 7-Mar-13 9:47am    
Thanks, but I carried all these suggestions but still do not receive , if i send from my pc to the emulator the pc server receive the message but from pc to android dose not receive what should i do ?
H.Brydon 7-Mar-13 18:16pm    
Determine where the network connection is not complete. Does the same traffic on TCP work? Can you confirm that the UDP packet arrives in different parts of the network?

...that is what debugging is all about!
farid wahed 8-Mar-13 6:01am    
i found the problem it is the emulator not receive but i try it in my phone and it is work ^_^

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900