Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Sending UDP Packet

  1. Oct 22, 2005 #1
    All right, guys. The code below is supposed to send UDP packets over the ports 0 - 40. However, when I run it, it doesn't seem to send anything. I'm guessing it's one of two things: I'm missing some essential part of the code, or that something's wrong with the address. Anybody have any ideas?
    Code (Text):
    import java.net.*;
    import java.io.*;
    public class datagram
    public static void main(String args[]) throws Exception
    int i;
    byte[] buf = new byte[256];
    DatagramSocket socket;
    DatagramPacket packet;
    InetAddress address = InetAddress.getByName("localhost");
    for(i = 0; i <= 40; i++)
    socket = new DatagramSocket(i);
    packet = new DatagramPacket(buf, buf.length, address, i);
  2. jcsd
  3. Oct 22, 2005 #2
    Don't use port numbers in the range 1-1024, because they are reserved by the operating system for other services. Couldn't you use bigger numbers, such as 5000?
  4. Oct 22, 2005 #3
    Wouldn't the packets be sent back or rejected anyway?
  5. Oct 22, 2005 #4
    the packets won't be sent back because udp is not a reliable protcol unlike tcp, there is no guarantee that the packet will be received.
  6. Oct 24, 2005 #5
    The behavior depends on the operating system, but it is guaranteed that those ports (i.e. 1-40) can't be accessed. By the way, port 0 doesn't exist.

    UDP is a best-effort protocol, meaning that the most is done to ensure (although not guarantee) a reliable delivery. In practice it is rare that UDP fails.
  7. Oct 24, 2005 #6
    Well according to RFC's it is reserved, in general networking port 0 is the same as a broadcast address in IP, if you code an app to use port 0 it will try to find the first available UDP port

    Explanation of port 0

    becuase of this 'feature' many security experts recomend that port 0 is blocked
    Last edited: Oct 24, 2005
  8. Oct 25, 2005 #7
    I think I got it, guys. Thanks for the help. For one thing I was sending them to localhost. I don't think that would work well on sending packets. Also, I did change the port numbers (by the way, I was told to use ports 0 - 40) to 40 - 80 and it worked just fine. I still think the school's security had some problems with us sending out packets, but eventually we all got it to work. Thanks again!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook