Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java socket udp Wifi port
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.
 
java.net.PortUnreachableException: ICMP Port Unreachable
	at java.net.DualStackPlainDatagramSocketImpl.socketReceiveOrPeekData(Native Method)
	at java.net.DualStackPlainDatagramSocketImpl.receive0(DualStackPlainDatagramSocketImpl.java:105)
	at java.net.AbstractPlainDatagramSocketImpl.receive(AbstractPlainDatagramSocketImpl.java:145)
	at java.net.DatagramSocket.receive(DatagramSocket.java:786)
	at udp_test.main(udp_test.java:28)
 
here is my code
public class udp_test
{
	public static void main(String[] args) throws IOException
	{
		InetAddress IP = InetAddress.getByName("192.168.1.2");
		int port = 5551;
		
		while (true)
		{
			try
			{
				byte [] datapacket = new byte [4];
				
				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);
				
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			catch (SocketException e)
			{
			// TODO Auto-generated catch block
			e.printStackTrace();
			}	
		}	
	}
}
 
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
Comments
Richard MacCutchan at 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 at 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 at 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 at 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 at 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)



Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 6 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