Rafi-muqaddar
asked on
Please help me to complete this code please
Hi
I wonder if anybody could help me.I desprately need help
Please have a look the following code :-
import java.io.*;
import java.net.*;
class TypeConvertion
{
//Get a short integer from two bytes with offset in the buf
public static short getShort(byte[] buf, int offset)
{
int high = buf[offset] & 0xff;
int low = buf[offset+1] & 0xff;
return (short)(high <<8 | low);
}
//Store a short integer to two bytes with offset in the buf
public static void putShort(byte[] buf, int offset, short value)
{
byte low = (byte)(value & 0xff);
byte high = (byte)(value>>8 & 0xff);
buf[offset] = high;
buf[offset+1] = low;
return;
}
}
public class TFTPClient
{
short seqn;
byte outb[];
byte inb[];
String fname; //The file name
InetAddress server; //sever’s IP address
int sport; //server’s port number
DatagramPacket odp;
DatagramPacket idp;
DatagramSocket sock;
TFTPClient(String sn, String sp, String fn) throws Exception
{
seqn = 0;
outb = new byte[516];
inb = new byte[516];
server = InetAddress.getByName(sn); //Get server’s IP
//Convert port string to port number
sport = Integer.parseInt(sp);
fname = fn;
idp = new DatagramPacket(inb,inb.len gth);
//Create Datagram Socket*/
sock = new DatagramSocket();
}
void SendACK() throws Exception
{
int index = 0;
//Fill ACK opcode =0x0004 field
TypeConversion.putShort(ou tb,index,( short)4);
index + = 2;
//Fill ACK SEQ field
TypeConversion.putShort(bu ff,index,s eqn);/*ACK SEQ*/
index + =2;
/*Generating ACK datagram packet*/
/*...*/
/*Send out the ACK datagram packet*/
/*...*/
}
void SendRRQ() throws Exception
{
int index = 0;
//Fill RRQ opcode = 0x0001 field
TypeConversion.putShort(ou tb,index,( short)1);
index + = 2;
//Fill the file name field
byte[] temp = fname.getBytes();
for(int i = 0;i < temp.length;i++);
outb [i + index] = temp[i];
}
index + = temp.length;
outb[index] = 0; //End of the file name
index + = 1;
//Fill the file data mode, using octet mode
String mode = "octet";
temp = mode.getBytes();
for(int i = 0;i <temp.length; i++)
{
outb[i+index] = temp[i];
}
/*Fill the end indicator of the packet*/
/*...*/
/*Generating ACK packet*/
/*...*/
/*Send out the ACK packet*/
/*...*/
}
void getFile() throws Exception{
/*Create a file*/
/*...*/
/*Using FileOutputStream to open the file*/
/*...*/
int opcode;
short rseq;
int length;
/*Send RRQ packet*/
/*...*/
while(true)
{
/*Receive the packet to idp from the server*/
/*...*/
/*Get OPCODE from the packet*/
opcode=TypeConvertion.getS hort(inb,0 );
if(opcode!=0x0003){ /*DATA*/
continue;
}
/*Get SEQ from the packet*/
rseq = TypeConvertion.getShort(in b,2);
if(rseq==seqn+1){/*it is a new packet*/
/*We don't check the server's IP address*/
/*Store the data into file*/
fos.write(inb,4,length-4);
seqn=rseq;
/*Send ACK*/
/*...*/
if(length<516){/*check end of the file*/
sock.close();
fos.close();
return;
}
continue;
}
if(rseq==seqn){ /*duplicated copy*/
/*Acknowledge the received packet again*/
/*...*/
continue;
}
/*error sequence, do nothing*/
}
}
public void main(String args[]) throws Exception{
if(args.length!=3){
System.out.println("Usage: TFTPClient <servername>
<port> <filename>");
return;
}
TFTPClient tftp=new TFTPClient(args[0],args[1] ,args[2]);
tftp.getFile();
}
}
This is a simple TFTP client whose function is to retrive the file from the TFTP server and store in a local file.Could anybody please fill the empty parts under the comments.(/*.......*/)
Thank you
I wonder if anybody could help me.I desprately need help
Please have a look the following code :-
import java.io.*;
import java.net.*;
class TypeConvertion
{
//Get a short integer from two bytes with offset in the buf
public static short getShort(byte[] buf, int offset)
{
int high = buf[offset] & 0xff;
int low = buf[offset+1] & 0xff;
return (short)(high <<8 | low);
}
//Store a short integer to two bytes with offset in the buf
public static void putShort(byte[] buf, int offset, short value)
{
byte low = (byte)(value & 0xff);
byte high = (byte)(value>>8 & 0xff);
buf[offset] = high;
buf[offset+1] = low;
return;
}
}
public class TFTPClient
{
short seqn;
byte outb[];
byte inb[];
String fname; //The file name
InetAddress server; //sever’s IP address
int sport; //server’s port number
DatagramPacket odp;
DatagramPacket idp;
DatagramSocket sock;
TFTPClient(String sn, String sp, String fn) throws Exception
{
seqn = 0;
outb = new byte[516];
inb = new byte[516];
server = InetAddress.getByName(sn);
//Convert port string to port number
sport = Integer.parseInt(sp);
fname = fn;
idp = new DatagramPacket(inb,inb.len
//Create Datagram Socket*/
sock = new DatagramSocket();
}
void SendACK() throws Exception
{
int index = 0;
//Fill ACK opcode =0x0004 field
TypeConversion.putShort(ou
index + = 2;
//Fill ACK SEQ field
TypeConversion.putShort(bu
index + =2;
/*Generating ACK datagram packet*/
/*...*/
/*Send out the ACK datagram packet*/
/*...*/
}
void SendRRQ() throws Exception
{
int index = 0;
//Fill RRQ opcode = 0x0001 field
TypeConversion.putShort(ou
index + = 2;
//Fill the file name field
byte[] temp = fname.getBytes();
for(int i = 0;i < temp.length;i++);
outb [i + index] = temp[i];
}
index + = temp.length;
outb[index] = 0; //End of the file name
index + = 1;
//Fill the file data mode, using octet mode
String mode = "octet";
temp = mode.getBytes();
for(int i = 0;i <temp.length; i++)
{
outb[i+index] = temp[i];
}
/*Fill the end indicator of the packet*/
/*...*/
/*Generating ACK packet*/
/*...*/
/*Send out the ACK packet*/
/*...*/
}
void getFile() throws Exception{
/*Create a file*/
/*...*/
/*Using FileOutputStream to open the file*/
/*...*/
int opcode;
short rseq;
int length;
/*Send RRQ packet*/
/*...*/
while(true)
{
/*Receive the packet to idp from the server*/
/*...*/
/*Get OPCODE from the packet*/
opcode=TypeConvertion.getS
if(opcode!=0x0003){ /*DATA*/
continue;
}
/*Get SEQ from the packet*/
rseq = TypeConvertion.getShort(in
if(rseq==seqn+1){/*it is a new packet*/
/*We don't check the server's IP address*/
/*Store the data into file*/
fos.write(inb,4,length-4);
seqn=rseq;
/*Send ACK*/
/*...*/
if(length<516){/*check end of the file*/
sock.close();
fos.close();
return;
}
continue;
}
if(rseq==seqn){ /*duplicated copy*/
/*Acknowledge the received packet again*/
/*...*/
continue;
}
/*error sequence, do nothing*/
}
}
public void main(String args[]) throws Exception{
if(args.length!=3){
System.out.println("Usage:
<port> <filename>");
return;
}
TFTPClient tftp=new TFTPClient(args[0],args[1]
tftp.getFile();
}
}
This is a simple TFTP client whose function is to retrive the file from the TFTP server and store in a local file.Could anybody please fill the empty parts under the comments.(/*.......*/)
Thank you
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Well, you would be the best person to write that because you would know what is the 'format' of the acknowledgement to be sent. Have a look at the Datagram sending example that I sent. You can create an array of bytes depending upon the format the of acknowledgement and pass it to the method there in the outbuf[] argument.
Something like:
String sAck = "<ACK>This is the Acknowledgement</ACK>" ;
byte[] buf = sAck.getBytes () ;
Something like:
String sAck = "<ACK>This is the Acknowledgement</ACK>" ;
byte[] buf = sAck.getBytes () ;
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Second correction :
for(int i = 0;i < temp.length;i++);
outb [i + index] = temp[i];
}
Should be :
for(int i = 0;i < temp.length;i++){
outb [i + index] = temp[i];
}
or
for(int i = 0;i < temp.length;i++)
outb [i + index] = temp[i];
for(int i = 0;i < temp.length;i++);
outb [i + index] = temp[i];
}
Should be :
for(int i = 0;i < temp.length;i++){
outb [i + index] = temp[i];
}
or
for(int i = 0;i < temp.length;i++)
outb [i + index] = temp[i];
Simplification :
Replace :
for(int i = 0;i < temp.length;i++)
outb [i + index] = temp[i];
index + = temp.length;
outb[index] = 0; //End of the file name
index + = 1;
By :
for(int i = 0;i < temp.length;i++)
outb [index++] = temp[i];
outb [index++] = 0; //End of the file name
Replace :
for(int i = 0;i < temp.length;i++)
outb [i + index] = temp[i];
index + = temp.length;
outb[index] = 0; //End of the file name
index + = 1;
By :
for(int i = 0;i < temp.length;i++)
outb [index++] = temp[i];
outb [index++] = 0; //End of the file name
String mode = "octet";
temp = mode.getBytes();
for(int i = 0;i <temp.length; i++)
{
outb[i+index] = temp[i];
}
--->
String mode = "octet";
temp = mode.getBytes();
for(int i = 0;i <temp.length; i++)
outb[index++] = temp[i];
/* send the packet (index = length) */
sock.sendPacket(new DatagramPacket(outb, index, server_adr, server_port));
/* server_adr is InetAddress of TFTP server */
http://java.sun.com/j2se/1.4.2/docs/api/java/net/DatagramSocket.html
http://java.sun.com/j2se/1.4.2/docs/api/java/net/DatagramPacket.html
temp = mode.getBytes();
for(int i = 0;i <temp.length; i++)
{
outb[i+index] = temp[i];
}
--->
String mode = "octet";
temp = mode.getBytes();
for(int i = 0;i <temp.length; i++)
outb[index++] = temp[i];
/* send the packet (index = length) */
sock.sendPacket(new DatagramPacket(outb, index, server_adr, server_port));
/* server_adr is InetAddress of TFTP server */
http://java.sun.com/j2se/1.4.2/docs/api/java/net/DatagramSocket.html
http://java.sun.com/j2se/1.4.2/docs/api/java/net/DatagramPacket.html
Split: Webstorm and mayankeagle.
>> Split: Webstorm and mayankeagle.
I agree this suggestion.
I agree this suggestion.
Please proceed with that recommendation.
ASKER
Just Tell me how to write Acknowledgement datagram packet in terms of this program.