import java.io.*;
import java.net.*;
import java.util.Date;
//import sun.nio.cs.US_ASCII;
import java.util.StringTokenizer;
//import java.util.regex.ASCII;
public class Application1 {
public static final int PORT = 69;
public static final int DGRAM_BUF_LEN = 512;
public static void main(String[] args){
DatagramSocket socket = null;
try {
socket = new DatagramSocket(PORT);
} catch (SocketException e) {
e.printStackTrace();
System.exit(3);
}
while (true) {//to run server for infinite time
try {
byte[] buf = new byte[DGRAM_BUF_LEN];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
//Byte[] data = socket.receive(packet);
String pckt = new String( packet.getData()) ;
//String tokenstr = "\0";
String strData[]= new String[2] ;
StringTokenizer tokenize = new StringTokenizer(pckt,"\0");
int i=0;
while (tokenize.hasMoreTokens()) {
strData[i]=(tokenize.nextToken());
i++;
}
int opcode = pckt.codePointAt(1);
System.out.println(pckt+" \n \t Packet recieved with OPCODE "+opcode);
String date = new Date().toString();
// buf = date.getBytes ();
DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(new File("c:\\text.txt"))));
//data packet
buf[0]=0;
buf[1]=(byte)03;//opcode
buf[2]=0;
buf[3]=(byte)1;//block no
in.read(buf,4,500);//data so that it only transfer only 1 packet
/*2 bytes 2bytes n bytes
opcode block data*/
System.out.write(buf);
// get client info
InetAddress clientAddr = packet.getAddress();
int port = packet.getPort();
// prepare packet for return to client
packet = new DatagramPacket(buf, buf.length, clientAddr, port);
socket.send(packet);
} catch(IOException e) {
e.printStackTrace();
}
}
}
}
July 30, 2006
JAVA TFTP server
Subscribe to:
Post Comments (Atom)
1 comment:
جشن آزادی مبارک
Post a Comment