Results 1 to 1 of 1
Thread: Receiving UDP pockets
-
Receiving UDP pockets
This Java tip shows how to receive UDP packets.
Java Code:import java.net.DatagramPacket; import java.net.DatagramSocket; public class UDPReceive { public static void main(String args[]) { try { int port = 90; // Create a socket to listen on the port. DatagramSocket dsocket = new DatagramSocket(port); // Create a buffer to read datagrams into. If a // packet is larger than this buffer, the // excess will simply be discarded! byte[] buffer = new byte[2048]; // Create a packet to receive data into the buffer DatagramPacket packet = new DatagramPacket(buffer, buffer.length); // Now loop forever, waiting to receive packets and printing them. while (true) { // Wait to receive a datagram dsocket.receive(packet); // Convert the contents to a string, and display them String msg = new String(buffer, 0, packet.getLength()); System.out.println(packet.getAddress().getHostName() + ": " + msg); // Reset the length of the packet before reusing it. packet.setLength(buffer.length); } } catch (Exception e) { System.err.println(e); } } }
Similar Threads
-
Sending out UDP pockets
By Java Tip in forum java.netReplies: 0Last Post: 04-07-2008, 08:07 PM -
Finding out the client who is receiving the response.
By v.ranjith in forum Advanced JavaReplies: 2Last Post: 01-29-2008, 07:03 AM -
Nokia 5300 freezes when playing sound after receiving a call
By presto in forum CLDC and MIDPReplies: 2Last Post: 10-31-2007, 03:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks