Hi all, my TFTP server I want to use as a template will not alllow me to send or recieve data. Any advice would be great as I don't know if its possibly a folder access problem on my PC. Here is the code:
public class TFTPS { /** * @param args the command line arguments */ // TODO code application logic here public static final int tftpRRQ=1; public static final int tftpWRQ=2; public static final int tftpDATA=3; public static final int tftpACK=4; public static final int tftpERROR=5; public static final int maxTftpPakLen=516; public static void main(String argv[]) { int port = 69; try { DatagramSocket sock = new DatagramSocket(port); System.out.println("Server Ready. Listening on port: "+sock.getLocalPort()); for(;;) { UdpData reqData = new UdpData(maxTftpPakLen); DatagramPacket reqPak = reqData.mkPacket(); sock.receive(reqPak); if (reqData.getInt(0)==tftpRRQ) { System.out.println("Request from "+reqPak.getAddress()); tftpTransfer t = new tftpTransfer(reqPak); } } } catch(Exception e) { System.out.println("Server terminated"); } } }
class UdpData { private byte data[]; public UdpData(int len) { data = new byte[len]; }; public UdpData(DatagramPacket pack) { data = pack.getData(); } public byte[] getData() { return data; }; public int length() { return data.length; }; public DatagramPacket mkPacket() { return new DatagramPacket(data,data.length); }; public DatagramPacket mkPacket(int len) { return new DatagramPacket(data,len); }; public DatagramPacket mkPacket(InetAddress dest, int port) { return new DatagramPacket(data,data.length,dest,port); }; public DatagramPacket mkPacket(InetAddress dest, int port, int len) { return new DatagramPacket(data,len,dest,port); }; public int getInt(int offset) { // Fix to avoid sign extension // Remember what the MSB is and then turn it off to avoid sign extension. It is // then added back to the sum after everything has been converted to integers. byte lo = data[offset+1]; int signBit = (int)(lo&0x80); lo&=0x7f; // Standard TCP/IP byte order is hiByte followed by loByte (big endian) int hiByte = (int)data[offset]; int loByte = (int)lo; return (hiByte<<8)+signBit+loByte; }; public void putInt(int val, int offset) { int loByte = val%256; int hiByte = val>>>8; // Standard TCP/IP byte order is hiByte followed by loByte (big endian) data[offset] = (byte)hiByte; data[offset+1] = (byte)loByte; }; public String getString(int offset) { StringBuffer strBuf = new StringBuffer(); int curPos = offset; while (data[curPos]!=0) { strBuf.append((char)data[curPos]); curPos++; } return new String(strBuf); }; public void putString(String str, int offset) { str.getBytes(0,str.length(),data,offset); data[offset+str.length()]=0; }; }
class tftpTransfer { protected DatagramSocket sock; protected InetAddress clientIP; protected int clientPort; protected FileInputStream source; protected UdpData dataPak; public tftpTransfer(DatagramPacket reqPak) { try { sock = new DatagramSocket(); clientIP = reqPak.getAddress(); clientPort = reqPak.getPort(); UdpData reqData = new UdpData(reqPak); File srcFile = new File(reqData.getString(2)); if (srcFile.exists() && srcFile.isFile() && srcFile.canRead()) { source = new FileInputStream(srcFile); dataPak = new UdpData(516); // 516==max pak len dataPak.putInt(3,0); // 3==DATA opcode this.start(); //This also seems to be an isssue } } catch (Exception e) { System.out.println("Client failed"); }; } public void run() { int bytesRead = 512; DatagramPacket ack = new DatagramPacket(new byte[4],4); try { for (int blkNum=0; bytesRead==512; blkNum++) { // 512==max data len dataPak.putInt(blkNum,2); bytesRead = source.read(dataPak.getData(),4,512); sock.send(dataPak.mkPacket(clientIP,clientPort,516)); // 516==max data len + header sock.receive(ack); } } catch (Exception e) { System.out.println("Client failed"); } try { source.close(); sock.close(); } catch (Exception e) {}; } }
I have had a few late nights (5am) trying to solve this, to no avail so I have decided to seek advice.