package net; import java.net.*; import java.io.*; /** * 采用UDP协议,编写一个Java网络应用程序,该应用分服务器端程序和客户端程序两部分。 * 客户端指定一个服务器上的文件名,让服务器发回该文件的内容,或者提示文件不存在。(20分) * (服务端程序和客户端程序分别命名为Server.java和Client.java) * * @since 2010-9-30 * @start 9:45 * @end 12:00 * */ public class TwentySix_UDP { public static void main(String[] args){ try{ UDPServer server=new UDPServer(); server.init(); server.start(); UDPClient client=new UDPClient(); //client.init(); client.start(); } catch(Exception e){ e.printStackTrace(); } } } class UDPServer{ DatagramSocket ds=null; byte[] data=new byte[10240]; DatagramPacket dp=null; String fileName=""; File file=null; FileInputStream fis=null; public void init()throws Exception{ ds=new DatagramSocket(5555); } public void start(){ Thread t=new Thread(){ public void run(){ try{ while(true){ data=new byte[10240]; dp=new DatagramPacket(data,data.length); ds.receive(dp); if(dp!=null){ fileName="src/net/"+new String(dp.getData()).trim(); System.out.println("Server parpe send file"+fileName); file=new File(fileName); if(file.exists()&&file.isFile()){ fis=new FileInputStream(file); while(fis.read(data)!=-1){ dp=new DatagramPacket(data,data.length,InetAddress.getLocalHost(),6666); ds.send(dp); Thread.sleep(200); } dp=new DatagramPacket("end".getBytes(),"end".length(), InetAddress.getLocalHost(),6666); ds.send(dp); fis.close(); } else{ dp=new DatagramPacket("null".getBytes(),"null".length(),InetAddress.getLocalHost(),6666); ds.send(dp); } } } } catch(Exception e){ e.printStackTrace(); } } }; t.start(); } } class UDPClient { DatagramSocket ds=null; DatagramPacket dp=null; String fileName="" ; Thread t=null; byte[] data=new byte[10240]; File file=null; FileOutputStream fos=null; String mark=""; /** * @deprecated * */ public void init()throws Exception{ t=new Thread(){ public void run(){ try{ ds=new DatagramSocket(6666); while(true){ synchronized(this){ mark="" ; dp=new DatagramPacket(fileName.getBytes(),fileName.length(),InetAddress.getLocalHost(),5555); ds.send(dp); ds.receive(dp); while(!mark.startsWith("end")){ if(mark.startsWith("null")){ System.out.println("File not exist!"); fos.close(); break; } if(file==null){ file=new File(""+System.currentTimeMillis()+fileName); fos=new FileOutputStream(file); } fos.write(data); fos.flush(); ds.receive(dp); mark=new String(dp.getData()); } if(mark.startsWith("end")){ System.out.println("File saved as:`"+file.getAbsolutePath() ); file=null; } this.wait(); } } } catch(Exception e){ e.printStackTrace(); } } }; } public void start()throws Exception{ while(true){ System.out.println("Please type the file name.EXP(t50.jpg)"); fileName=new BufferedReader(new InputStreamReader(System.in)).readLine(); ClientThread ct=new ClientThread(fileName); ct.start(); } } } class ClientThread extends Thread{ File file=null; String fileName=""; FileOutputStream fos=null; DatagramSocket ds=null; DatagramPacket dp=null; byte[] data=new byte[10240]; ClientThread(String fileName){ this.fileName=fileName; } public void run(){ file=new File(fileName); try{ fos=new FileOutputStream(file); ds=new DatagramSocket(6666); ds.setSoTimeout(3000); dp=new DatagramPacket(fileName.getBytes(),fileName.length(),InetAddress.getLocalHost(),5555); ds.send(dp); dp=new DatagramPacket(data,data.length,InetAddress.getLocalHost(),5555); ds.receive(dp); while(!new String(dp.getData()).startsWith("end")){ fos.write(dp.getData()); ds.receive(dp); } fos.flush(); fos.close(); } catch(SocketException se){ try{ fos.flush(); fos.close(); } catch(Exception ie){} } catch(Exception e){ } System.out.println("file Saved as `"+file.getAbsolutePath()); } }