wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes datagram packets. UDPReceive sends an acknowledgment when it receives a datagram... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "datagram packets. UDPReceive sends an acknowledgment when it receives a datagram..." Watch "datagram packets. UDPReceive sends an acknowledgment when it receives a datagram..." New topic
Author

datagram packets. UDPReceive sends an acknowledgment when it receives a datagram...

kllew lewd
Greenhorn

Joined: Oct 06, 2013
Posts: 1
Modify the UDPSend and UDPReceive programs of Examples (see below) Example 5-11 and Example 5-12 so that UDPReceive sends an acknowledgment when it receives a datagram, and so that UDPSend doesn't exit until it receives the acknowledgment. The acknowledgment should itself be a datagram, and can contain any data you desire. (You could use the checksum classes of the java.util.zip package, for example, to compute a checksum of the received data and then send this back in the acknowledgment packet.) Use the setSoTimeout( ) method of DatagramSocket so that UDPSend doesn't wait for more than a few seconds to receive the acknowledgment. If the acknowledgment packet is not received before the timeout, UDPSend should assume that the original packet got lost and was not received. Your modified UDPSend should try to resend the packet once or twice, and if it still doesn't receive an acknowledgment, it should exit with an error message.


Example 5-11. UDPSend.java

import java.io.*;
import java.net.*;
/**
* This class sends the specified text or file as a datagram to the
* specified port of the specified host.
**/
public class UDPSend {
public static final String usage =
"Usage: java UDPSend <hostname> <port> <msg>...\n" +
" or: java UDPSend <hostname> <port> -f <file>";
public static void main(String args[ ]) {
try {
// Check the number of arguments
if (args.length < 3)
throw new IllegalArgumentException("Wrong number of args");

// Parse the arguments
String host = args[0];
int port = Integer.parseInt(args[1]);
// Figure out the message to send.
// If the third argument is -f, then send the contents of the file
// specified as the fourth argument. Otherwise, concatenate the
// third and all remaining arguments and send that.
byte[ ] message;
if (args[2].equals("-f")) {
File f = new File(args[3]);
int len = (int)f.length( ); // figure out how big the file is
message = new byte[len]; // create a buffer big enough
FileInputStream in = new FileInputStream(f);
int bytes_read = 0, n;
do { // loop until we've read it all
n = in.read(message, bytes_read, len-bytes_read);
bytes_read += n;
} while((bytes_read < len)&& (n != -1));
}
else { // Otherwise, just combine all the remaining arguments.
String msg = args[2];
for (int i = 3; i < args.length; i++) msg += " " + args[i];
// Convert the message to bytes using UTF-8 encoding
message = msg.getBytes("UTF-8");
}

// 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);
System.err.println(usage);
}
}
}


Example 5-12. UDPReceive.java
import java.io.*;
import java.net.*;
/**
* This program waits to receive datagrams sent to the specified port.
* When it receives one, it displays the sending host and prints the
* contents of the datagram as a string. Then it loops and waits again.
**/
public class UDPReceive {
public static final String usage = "Usage: java UDPReceive <port>";
public static void main(String args[ ]) {
try {
if (args.length != 1)
throw new IllegalArgumentException("Wrong number of args");

// Get the port from the command line
int port = Integer.parseInt(args[0]);

// Create a socket to listen on the port.
DatagramSocket dsocket = new DatagramSocket(port);

// Create a buffer to read datagrams into. If anyone sends us a
// packet containing more than will fit into 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.
for(;;) {
// Wait to receive a datagram
dsocket.receive(packet);
// Decode the bytes of the packet to characters, using the
// UTF-8 encoding, and then display those characters.
String msg = new String(buffer, 0, packet.getLength( ),"UTF-8");
System.out.println(packet.getAddress( ).getHostName( ) +
": " + msg);

// Reset the length of the packet before reusing it.
// Prior to Java 1.1, we'd just create a new packet each time.
packet.setLength(buffer.length);
}
}
catch (Exception e) {
System.err.println(e);
System.err.println(usage);
}
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: datagram packets. UDPReceive sends an acknowledgment when it receives a datagram...