📄 tftpsockettemp.java
字号:
package com.rview.client;import java.net.DatagramSocket;import java.net.SocketException;import java.net.InetAddress;import java.net.SocketAddress;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.*;import java.net.UnknownHostException;import java.net.DatagramPacket;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: </p> * @author not attributable * @version 1.0 */public class TftpSocketTemp extends DatagramSocket implements Runnable{ final static int NONE=0; final static int RRQ=1; final static int WRQ=2; final static int DAT=3; final static int ACK=4; final static int ERR=5; final static int UNKNOWN=6; final static int TIMEOUT=7; protected String fnstring; protected byte[] fn; protected byte[] ftype; protected InetAddress hostaddress; protected int port=69; protected byte[] bytes=new byte[516]; protected byte[] bytesin=new byte[1024]; protected int block=0; protected FileInputStream filein; protected FileOutputStream fileout; protected boolean EOF=false; protected int servercode=0; public static String serverpath=new String("e:\\hyg\\send\\"); public static String clientpath=""; public TftpSocketTemp() throws SocketException { int n=3; do{ n=n+1; } while(n!=ACK); System.out.println(n); } public TftpSocketTemp(int p0) throws SocketException { super(p0); } public TftpSocketTemp(int p0, InetAddress p1) throws SocketException { super(p0, p1); } public TftpSocketTemp(SocketAddress p0) throws SocketException { super(p0); } public TftpSocketTemp(String clientpath,String filen,String type,String host) throws UnknownHostException,SocketException { this.clientpath=clientpath; create( filen, type, host); } public TftpSocketTemp(String filen,String type,String host) throws UnknownHostException,SocketException { create( filen, type, host); } protected boolean sendErrPacket(int code,String msg){ bytes[0]=(byte)((ERR>>8)&0XFF); bytes[1]=(byte)((ERR)&0XFF); bytes[2]=(byte)((code>>8)&0XFF); bytes[3]=(byte)((code)&0XFF); System.arraycopy(msg.getBytes(),0,bytes,4,msg.length()); bytes[4+msg.length()]=0; DatagramPacket dt=new DatagramPacket(bytes,4+msg.length(),hostaddress,port); try { send(dt); } catch (IOException ex) { return false; } return true; } protected boolean sendAck(int block){ bytes[0]=(byte)((ACK>>8)&0XFF); bytes[1]=(byte)((ACK)&0XFF); bytes[2]=(byte)((block>>8)&0XFF); bytes[3]=(byte)((block)&0XFF); DatagramPacket dt=new DatagramPacket(bytes,4,hostaddress,port); try { send(dt); } catch (IOException ex) { return false; } return true; } public void create(String filen,String type,String host) throws UnknownHostException,SocketException { try { filen.getBytes("ISO-8859-1"); ftype=type.getBytes(); } catch (Exception ex) { filen.getBytes(); ftype=type.getBytes(); } fnstring=filen; //setSoTimeout(1500); hostaddress = InetAddress.getByName(host); } public boolean sendpacket(int ptype){ boolean rv=false; int len=2; int l; bytes[0]=(byte)((ptype>>8)&0XFF); bytes[1]=(byte)((ptype)&0XFF); switch(ptype){ case RRQ: case WRQ: System.arraycopy(fn,0,bytes,2,fn.length); bytes[2+fn.length]=0; System.arraycopy(ftype,0,bytes,3+fn.length,ftype.length); bytes[3+fn.length+ftype.length]=0; len+=fn.length+ftype.length+2; break; case DAT: case ACK: bytes[2]=(byte)((block>>8)&0XFF); bytes[3]=(byte)((block)&0XFF); len=len+2; if (ptype==DAT){ byte[] fbuf=new byte[512]; try { l = filein.read(fbuf, 0, 512); } catch (IOException ex1) { l=0; } System.arraycopy(fbuf,0,bytes,len,l); len=len+l; if (l!=512){ EOF=true; } } break; case ERR: bytes[2]=bytes[3]=bytes[4]=0; len=5; break; default: return false; } DatagramPacket dt=new DatagramPacket(bytes,len,hostaddress,port); try { send(dt); } catch (IOException ex) { return false; } return true; } protected int rcvpacket() throws SocketException{ DatagramPacket dt=new DatagramPacket(bytesin,1024); byte[] byt; int blockin; boolean status=false; try { receive(dt); status=true; } catch (IOException ex) { } if (!status) return TIMEOUT; port=dt.getPort(); byt=dt.getData(); int code=(byt[0]<<8)+byt[1]; switch(code){ case RRQ: case WRQ: return code; case ACK: blockin=(byt[2]<<8)+byt[3]; if (blockin!=block) return NONE; block++; return ACK; case DAT: blockin=(byt[2]<<8)+byt[3]; if (blockin>block) return UNKNOWN; if (blockin<block){ sendAck(blockin); return NONE; } try { fileout.write(byt, 4, dt.getLength() - 4); } catch (IOException ex1) { } if (dt.getLength()<516){ EOF=true; } sendpacket(ACK); block++; return DAT; case ERR: String err=new String(byt,4,dt.getLength()); System.out.println(err); return ERR; default: return UNKNOWN; } } public boolean fsend(){ boolean rv=false; try { filein = new FileInputStream(fnstring); } catch (FileNotFoundException ex) { } try{ int n; int retry=0; rv=sendpacket(WRQ); if (!rv) return rv; while(!EOF){ do{ n=rcvpacket(); if (n==TIMEOUT){ if(++retry>5) return false; if (block!=0){ if (!sendpacket(DAT)) return false; continue; } } if (n==ERR) return false; if (n==UNKNOWN){ sendErrPacket(0,"Unknown error"); return false; } }while(n!=ACK); if (!sendpacket(DAT)) return false; } System.out.println("Data send Success!"); return true; } catch(Exception ex){ ex.printStackTrace(); } finally{ streamclose(filein); } return rv; } public void run(){ if (servercode==WRQ){ try { fileout = new FileOutputStream(fnstring); } catch (IOException ex) { sendErrPacket(2,"Access violation"); return; } sendAck(0); block=1; int n; do{ try { n = rcvpacket(); } catch (SocketException ex1) { n=ERR; } if (n==ERR){ streamclose(fileout); return; } if (n!=DAT&&n!=NONE){ sendErrPacket(0,"?"); streamclose(fileout); return; } }while(!EOF); } if (servercode==RRQ){ } } public static void tftpd(){ DatagramSocket sskt; try { sskt = new DatagramSocket(69); } catch (SocketException ex) { System.out.println("unable to start server:"+ex); return; } byte[] buf=new byte[1024]; DatagramPacket pkt=new DatagramPacket(buf,1024); while(true){ try { sskt.setSoTimeout(0); sskt.receive(pkt); int port=pkt.getPort(); byte[] byt=pkt.getData(); String message=byt.toString(); int code=(byt[0]<<8)+byt[1]; if (code!=WRQ&&code!=RRQ){ byte[] errary=new byte[4]; errary[0]=(byte)((ERR>>8)&0XFF); errary[1]=(byte)(ERR&0XFF); errary[2]=(byte)0; errary[3]=(byte)0; errary[4]=(byte)0; pkt.setData(errary); sskt.send(pkt); continue; } int zlen,zlen1; TftpSocketTemp worker=new TftpSocketTemp(); worker.port=pkt.getPort(); worker.hostaddress=pkt.getAddress(); for(zlen=0;byt[zlen+2]!=0;zlen++); worker.fnstring=serverpath+(new String(byt,2,zlen)); worker.fn=worker.fnstring.getBytes(); for(zlen1=0;byt[zlen1+zlen+2]!=0;zlen1++); worker.ftype=new String(byt,zlen+2,zlen1).getBytes(); worker.servercode=code; new Thread(worker).start(); } catch (IOException ex1) { } } } private boolean streamclose(OutputStream s){ boolean rv=true; try { s.close(); } catch (IOException ex) { rv=false; } return rv; } private boolean streamclose(InputStream s){ boolean rv=true; try { s.close(); } catch (IOException ex) { rv=false; } return rv; } public static void main(String[] args) throws SocketException { if (args.length==0){ System.out.println("starting server"); Tftpd tftp = new Tftpd(); tftp.start(); return; } try { String group[]; String clientfile; if(args[0].lastIndexOf("\\")>0){ clientfile=args[0].substring(args[0].lastIndexOf("\\")+1,args[0].length()); } else{ clientfile=args[0]; } TftpSocketTemp sock = new TftpSocketTemp(args[0], args[1], args[2]); sock.fn=clientfile.getBytes(); sock.fsend(); } catch (Exception ex) { } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -