2 Replies - 1212 Views - Last Post: 24 September 2012 - 12:42 PM Rate Topic: -----

#1 polska03  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 302
  • Joined: 28-November 09

send UDP packet

Posted 24 September 2012 - 10:10 AM

Hello I want to send a UDP packet. This code compiles fine, but when I run it I can't see any UDP packet being sent the client ( I am running on a packet sniffer, wireshark, on the host computer). Is something wrong with the code? or am I missing something here. So this code runs on a computer that I want to send a UDP packet to the host (which has packet sniffing software), but the UDP packet doesn't show.
import java.io.*;
import java.net.*;

public class UDPSend {
  public static void main(String args[]) {
    try {
      String host = "myhostname.com";
      int port = 50500; //random port I chose

      byte[] message = "Java Source and Support".getBytes();

      // Get the internet address of the specified host
      InetAddress address = InetAddress.getByName(host);

      // Initialize a datagram packet with data and address
      DatagramPacket packet = new DatagramPacket(message, message.length,
          address, port);

      // Create a datagram socket, send the packet through it, close it.
      DatagramSocket dsocket = new DatagramSocket();
      dsocket.send(packet);
      dsocket.close();
    } catch (Exception e) {
      System.err.println(e);
    }
  }
}



Is This A Good Question/Topic? 0
  • +

Replies To: send UDP packet

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3445
  • View blog
  • Posts: 15,607
  • Joined: 20-September 08

Re: send UDP packet

Posted 24 September 2012 - 12:37 PM

Forget wireshark for the moment - is the packet received?
Was This Post Helpful? 0
  • +
  • -

#3 polska03  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 302
  • Joined: 28-November 09

Re: send UDP packet

Posted 24 September 2012 - 12:42 PM

With the code above, there was no recieved packet. I got it to work by giving the ip address of the host instead of the name. So
String host = "111.111.111.111";
instead of
String host = "www.name.com";
. For some reason this change now makes the UDP packet show up in wireshark.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1