Click here to Skip to main content
11,929,166 members (62,331 online)
Rate this:
Please Sign up or sign in to vote.
See more: Java udp Sockets Wifi Ports
hello everyone, I'd like to ask why is this error occurs.
I am trying to get a string data from a device. I have make this code and it runs well when I try to obtain data from a local server, but it stated this error when it connects to the device. ICMP Port Unreachable
	at Method)
	at udp_test.main(

here is my code
public class udp_test
	public static void main(String[] args) throws IOException
		InetAddress IP = InetAddress.getByName("");
		int port = 5551;
		while (true)
				byte [] datapacket = new byte [4];
				DatagramSocket ds = new DatagramSocket();
				DatagramPacket dp = new DatagramPacket(datapacket, datapacket.length);
				ds.connect(IP, port);
				String strRecv = new String(dp.getData(), 0, dp.getLength()) + " from "
						+ dp.getAddress().getHostAddress() + " port " + dp.getPort();
				try {
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
			catch (SocketException e)
			// TODO Auto-generated catch block

it's simply change the IP address, byte size and port when I connect to my device or should I change something else?

thanks before
Posted 5-May-13 23:23pm
Richard MacCutchan 6-May-13 4:30am
Have you checked the IP and port address of your device, that the device is listening for connections, and that the route across the network is correct? You should be able to test this simply by using the ping command.
evanharijanto 6-May-13 4:51am
yes I have checked my IP and port address of my device. the code stated above is using my device's IP and port. tried also using the ping command and no problem with it. when it goes into coding, then error as stated above.
Richard MacCutchan 6-May-13 5:32am
The message is coming from a router to tell you that there is no path to that device, which is very strange given that you can reach it via ping. I can only suggest you use your debugger to check that the address is not getting corrupted in your program somehow.
Richard MacCutchan 6-May-13 5:48am
I just ran your code on my system to a valid address and got a receive timeout, which is what I would expect.
evanharijanto 6-May-13 6:01am
I also don't know why. I changed the IP, port & byte size to my local server and it works.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151126.1 | Last Updated 6 May 2013
Copyright © CodeProject, 1999-2015
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