Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: Sending Data to a Port in UDP Format

Sending Data to a Port in UDP Format The ByteBuffer Class (from the java.nio package) helps you to write data in UDP by converting them to the respective bytes they generally occupy. This is especially helpful when trying to control network traffic. T

Tutorial Details:

The Code:
import java.net.*;
import java.io.*;
import java.nio.*;

public class SendUDPData{

private String serverIP = \"127.0.0.1\";
private int listeningPort = 10001;
private DatagramSocket datagramSocket = null;

public static void main(String args[]){
SendUDPData sendUDPData = new SendUDPData();
sendUDPData.sendData();
}

private void sendData(){
try{

// Initializing a byte Array
byte[] byteDataArr = new byte[10];

ByteBuffer configDataBuffer = ByteBuffer.wrap(byteDataArr);

// Initializing the variables
int configRackID = 12;
int configPortID = 760;

//populating the configDataBuffer with the required values.
//Will populate the first 4 bytes
configDataBuffer.putInt(configRackID);
//Will populate the second 4 bytes
configDataBuffer.putInt(configPortID);
// and so on...

//Two ways of creating a datagramSocket. Can use the one required
//Will create a DatagramSocket on port 10000
datagramSocket = new DatagramSocket(10000);
//Will create a DatagramSocket on any available port
datagramSocket = new DatagramSocket();
//Will connect to the IPAddress provided by the serverIP and to the
port specified by listeningPort

datagramSocket.connect(InetAddress.getByName(serverIP),listeningPort);

datagramSocket.send(new DatagramPacket(byteDataArr,
byteDataArr.length));

datagramSocket.close();
System.out.println(\"Data sent successfully.\");

}catch(SocketException se){
//handle SocketException
System.out.println(\"SocketException: \"+se);
}catch(UnknownHostException uhe){
//handle UnknownHostException
System.out.println(\"UnknownHostException: \"+uhe);
}catch(IOException ioe){
//handle IOException
System.out.println(\"IOException: \"+ioe);
}catch(Exception e){
//handle Exception
System.out.println(\"Exception: \"+e);
}finally{
if (datagramSocket != null)
datagramSocket.close();
}

}
}


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Sending Data to a Port in UDP Format

View Tutorial:
Sending Data to a Port in UDP Format

Related Tutorials:

Java Q&A, Open Java?
Java Q&A, Open Java?
 
Automating WWW Exploration
Automating WWW Exploration
 
JavaWorld - Distributed applet-based massively parallel processing (DAMPP) - January 1997
JavaWorld - Distributed applet-based massively parallel processing (DAMPP) - January 1997
 
Java gets serial support with the new javax.comm package - JavaWorld - May 1998
Java gets serial support with the new javax.comm package - JavaWorld - May 1998
 
Opening up new ports to Java with javax.comm - JavaWorld - September 1998
Opening up new ports to Java with javax.comm - JavaWorld - September 1998
 
Java gets serial support with the new javax.comm package
Java gets serial support with the new javax.comm package
 
Simple handling of network timeouts - JavaWorld September 1999
Simple handling of network timeouts - JavaWorld September 1999
 
Multicast the chatwaves - JavaWorld October 1999
Multicast the chatwaves - JavaWorld October 1999
 
A promise of easier embedded-systems networking - JavaWorld November 1999
A promise of easier embedded-systems networking - JavaWorld November 1999
 
Leverage legacy systems with a blend of XML, XSL, and Java - JavaWorld October 2000
Leverage legacy systems with a blend of XML, XSL, and Java - JavaWorld October 2000
 
Sockets programming in Java: A tutorial - JavaWorld December 1996
Sockets programming in Java: A tutorial - JavaWorld December 1996
 
Device programming with MIDP, Part 3 - JavaWorld July 2001
Device programming with MIDP, Part 3 - JavaWorld July 2001
 
Jini-like discovery for RMI
Jini-like discovery for RMI
 
Use XML data binding to do your laundry
Use XML data binding to do your laundry
 
Publish and find UDDI tModels with JAXR and WSDL
Publish and find UDDI tModels with JAXR and WSDL
 
Test email components in your software
Test email components in your software
 
SAAJ: No strings attached
SAAJ: No strings attached
 
Sending Data to a Port in UDP Format
Sending Data to a Port in UDP Format The ByteBuffer Class (from the java.nio package) helps you to write data in UDP by converting them to the respective bytes they generally occupy. This is especially helpful when trying to control network traffic. T
 
XML DOM-lite parser and writer
Summary This article provides a simple method for creating valid XML and a simple DOM-like (Document Object Model) parser. This method is useful for small applications that need simple XML functionality without the size and complexity of the full range d
 
SSH Port Forwarding Through a Proxy Server (Community-Submitted Tech Tip)
Learn how to use SSH port forwarding (tunneling) through a proxy server for secure connections.
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.