Click here to Skip to main content
15,908,013 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all, I'd like to ask a question.

I would like to receive a data from an UDP port and display the data as string.

here is my trial code, but it doesn't works.

it is stated in the error message that it can't bind and also stated socket.bind(native method)
I am still don't understand the meaning of the native method.

Java
public class App
{
 public static void main (String[] args) throws IOException
 {
  byte buffer[] = new byte[1024];
  
  InetAddress address = InetAddress.getByName("192.168.0.199");
  int port = 53;
  
  DatagramPacket p = new DatagramPacket(buffer, buffer.length, address, port);
  
  DatagramSocket ds = new DatagramSocket(port);
  
  ds.receive(p);
  
  System.out.println(new String(p.getData(), 0, p.getLength()));
  
  
 }
 
}


please kindly help this newbie :)
Posted
Updated 2-May-13 22:39pm
v4
Comments
[no name] 2-May-13 7:49am    
"it doesn't works" it not at all a helpful description of a problem.
AlphaDeltaTheta 2-May-13 9:25am    
Please give the stack trace... It will help to trace your issue more conveniently

Well, your code is absolutely correct there is no issues with it.
You are encountering a bind exception. Such exceptions occur if the underlying operating system is unable to create an socket at the specified port. It might be for the following reasons:

1. The port 53 is already in use. In this case try another port.
2. You may not have permissions to open a socket. If you are under a POSIX (UNIX, Linux, BSD, Mac etc.) environment, then you require to be root to open a connection on a privileged port (ports <1024).

Try using another port (54321 for eg.) be sure that it is open. If you are under a windows machine you can use Resmon.exe to monitor ports. For Unix you need the equivalent command which unfortunately I'm unable to recollect (have been away for months from linux... excuse me).
 
Share this answer
 
Well, your has no issues with it. Except for the thing that the constructor used to create a datagram socket for receiving must not include the ip address. It should be like this
Java
DatagramPacket p = new DatagramPacket(buffer, buffer.length);


You are encountering a bind exception. Such exceptions occur if the underlying operating system is unable to create an socket at the specified port. It might be for the following reasons:

1. The port 53 is already in use. In this case try another port.
2. You may not have permissions to open a socket. If you are under a POSIX (UNIX, Linux, BSD, Mac etc.) environment, then you require to be root to open a connection on a privileged port (ports <1024).

Try using another port (54321 for eg.) be sure that it is open. If you are under a windows machine you can use Resmon.exe to monitor ports. For Unix you need the equivalent command which unfortunately I'm unable to recollect (have been away for months from linux... excuse me).
 
Share this answer
 
solved!! thanks everyone!!
here is the code I have made after some confusing period.
Java
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class udp_test
{
	public static void main(String[] args) throws IOException
	{
		InetAddress IP = InetAddress.getByName("192.168.0.199");
		int port = 53;
		
		try
		{
			byte [] datapacket = new byte [128];
				
			DatagramSocket ds = new DatagramSocket();
			DatagramPacket dp = new DatagramPacket(datapacket, datapacket.length);
			
			ds.setSoTimeout(100);
			ds.connect(IP, port);
			ds.send(dp);
			ds.isConnected();
			
			ds.receive(dp);
			ds.close();
			
			String strRecv = new String(dp.getData(), 0, dp.getLength()) + " from " + dp.getAddress().getHostAddress() + " port " + dp.getPort();        
			
			System.out.println(strRecv);
		}
		catch (SocketException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
 
Share this answer
 
v2

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