Rev 212 | Rev 216 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
/*******************************************************************************
* File: P2_Client.java
* Author: Ira W. Snyder (devel@irasnyder.com)
* License: GNU General Public License v2
* Class: CS380 - Computer Networking
*
* Assignment: Project #2
* Date Last Modified: 2006-02-05
*
* Purpose: Implement a simple client allowing the user to send datagrams
* to P2_Server, which will decode them from 4B to 5B encoding. The client
* will retransmit any packets that do not recieve a reply.
******************************************************************************/
import java.io.*;
import java.net.*;
import java.util.*;
public class P2_Client
{
private static int soTimeout = 3000; // 3000 ms timeout = 3 sec
private static DatagramSocket socket;
private static DatagramPacket packet;
private static int portNumber = 1337;
/**
* Method: readPacket()
* Purpose: Read a packet on the currently opened DatagramSocket,
* and return it as a String.
*/
public static String readPacket (int maxlen)
{
byte[] buf = new byte[maxlen];
packet = new DatagramPacket (buf, buf.length);
try {
socket.receive (packet);
} catch (SocketTimeoutException e) {
System.out.println ("Read timed out, requesting retransmit");
return null;
} catch (PortUnreachableException e) {
System.out.println ("Caught PortUnreachableException, are you" +
" sure the server is running?");
System.exit(1);
} catch (IOException e) {
System.out.println ("Caught exception in readPacket()");
e.printStackTrace();
return null;
}
return new String (buf, 0, packet.getLength()/*- 1*/);
}
/**
* Method: writePacket()
* Purpose: Send a packet to localhost:portNumber.
*/
public static boolean writePacket (String msg) throws Exception
{
InetAddress address = InetAddress.getLocalHost();
int port = portNumber;
byte[] buf = msg.getBytes();
packet = new DatagramPacket (buf, buf.length, address, port);
try {
socket.send (packet);
} catch (PortUnreachableException e) {
System.out.println ("Caught PortUnreachableException, are " +
"you sure the server is running?");
System.exit(1);
} catch (IOException e) {
System.out.println ("Caught exception in writePacket()");
e.printStackTrace();
return false;
}
return true;
}
/**
* Method: guaranteedTransmit()
* Purpose: A wrapper around readPacket() and writePacket() that implements
* a reliable transmit mechanism. It allows you to specify a maximum number
* of times to try resending a packet.
*/
public static String guaranteedTransmit (String msg, int maxlen, int maxRetries) throws Exception
{
String reply = null;
int retries = 0;
writePacket (msg);
reply = readPacket (maxlen);
while (reply == null && retries < maxRetries)
{
System.err.println ("Packet lost, retransmitting...");
writePacket (msg);
reply = readPacket (maxlen);
retries++;
}
return reply;
}
/**
* Method: main()
* Purpose: This will accept input from the user, and attempt to send it
* to the corresponding server, which will convert the messages from 4B
* to 5B encoding.
*/
public static void main (String[] args) throws Exception
{
int dropHowMany = 0;
socket = new DatagramSocket();
socket.connect (InetAddress.getLocalHost(), portNumber);
socket.setSoTimeout (soTimeout);
BufferedReader kbd = new BufferedReader (
new InputStreamReader (
System.in));
try {
dropHowMany = Integer.parseInt(args[0]);
} catch (ArrayIndexOutOfBoundsException e) {
System.err.println ("You need to call the program with \'java P2_Client <num>\'");
System.exit(1);
}
String msg = new String("DROP" + dropHowMany);
String reply = guaranteedTransmit (msg, 6, 1);
if (!reply.equals(msg))
{
System.err.println ("Unable to inform the server how often to drop packets");
System.exit(1);
}
System.out.print ("% decode: ");
msg = kbd.readLine();
boolean done = false;
while (!done)
{
reply = guaranteedTransmit (msg, 6, 1);
if (reply.equals("QUIT"))
{
done = true;
continue;
}
System.out.println ("-> reply: " + reply);
System.out.print ("% decode: ");
msg = kbd.readLine();
}
// Close the socket
socket.close();
}
}