Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java socket udp TCP/IP port
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.
 
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 Smile | :)
Posted 2-May-13 2:45am
Edited 2-May-13 23:39pm
v4
Comments
ThePhantomUpvoter at 2-May-13 7:49am
   
"it doesn't works" it not at all a helpful description of a problem.
Member 8371841 at 2-May-13 9:25am
   
Please give the stack trace... It will help to trace your issue more conveniently
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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).
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
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).
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

solved!! thanks everyone!!
here is the code I have made after some confusing period.
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();
		}
	}
}
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 390
1 Jochen Arndt 165
2 Richard MacCutchan 135
3 DamithSL 95
4 Garth J Lancaster 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,032
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,220


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 3 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100