import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.net.*; import java.io.*; class FileTrans implements ActionListener{ JFileChooser file; JFrame fileFrame; JPanel panel; JPanel Bpanel; JButton Cancel; JButton Open; File f = null; File FileName; String ip; int port, portW; boolean err = true; ServerSocket socketIn=null; public FileTrans(int p) { int size=0; try { socketIn = new ServerSocket(p); socketIn.setSoTimeout(5000); Socket client = socketIn.accept(); BufferedReader incoming = new BufferedReader(new InputStreamReader(client.getInputStream())); FileName = new File(incoming.readLine()); client.close(); client = socketIn.accept(); incoming = new BufferedReader(new InputStreamReader(client.getInputStream())); size = Integer.parseInt(incoming.readLine()); client.close(); } catch (SocketException e) { } catch (NullPointerException e) { } catch (IOException e) { } Socket client = null; FileOutputStream fileIn=null; try { client = socketIn.accept(); File folder = new File("Recieved Files"); folder.mkdirs(); fileIn = new FileOutputStream("Recieved Files\\" + FileName); DataInputStream incoming = new DataInputStream(client.getInputStream()); int c; while ((c = incoming.read()) != -1) fileIn.write(c); fileIn.close(); client.close(); } catch (SocketException e) { } catch (NullPointerException e) { } catch (IOException e) { } } public FileTrans(String i, int p, boolean s) { fileFrame = new JFrame("Choose File"); file = new JFileChooser(); file.setControlButtonsAreShown(false); Container content = fileFrame.getContentPane(); fileFrame.setSize(600, 100); fileFrame.setLocation(200, 200); panel = new JPanel(); Bpanel = new JPanel(); Open = new JButton("Open"); Cancel = new JButton("Cancel"); Open.addActionListener(this); Cancel.addActionListener(this); panel.add(file); Bpanel.add(Open); Bpanel.add(Cancel); content.add(panel, BorderLayout.NORTH); content.add(Bpanel, BorderLayout.SOUTH); fileFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); fileFrame.pack(); fileFrame.setVisible(true); ip = i; port = p; if(s) portW = port -2; else portW = port-1; } public void actionPerformed(ActionEvent e) { fileFrame.setVisible(false); if(e.getSource() == Cancel) { } else if(e.getSource() == Open) { f = file.getSelectedFile(); sendFile(); } } public void sendFile() { PrintWriter out = null; String name = f.getName(); FileInputStream readIn = null; int bytes=0; try { readIn = new FileInputStream(f); bytes = readIn.available(); } catch (IOException e) { } try { Socket clientSocket = new Socket(ip,portW); out = new PrintWriter(clientSocket.getOutputStream(), true); out.println(" is sending you: " + name + " - " + bytes + " bytes (type \"\\yes\" to accept)"); clientSocket.close(); } catch (UnknownHostException e) { } catch (IOException e) { } for(int i=0; i<20;) { try { int c; Socket clientSocket2 = new Socket(ip,port); out = new PrintWriter(clientSocket2.getOutputStream(), true); out.println(name); clientSocket2.close(); clientSocket2 = new Socket(ip,port); out = new PrintWriter(clientSocket2.getOutputStream(), true); out.println(bytes); clientSocket2.close(); clientSocket2 = new Socket(ip,port); DataOutputStream out2 = new DataOutputStream(clientSocket2.getOutputStream()); while ((c = readIn.read()) != -1) out2.write(c); clientSocket2.close(); i=20; err = false; } catch (UnknownHostException e) { i++; err = true; } catch (IOException e) { i++; err = true; } } try { Socket clientSocket = new Socket(ip,portW); out = new PrintWriter(clientSocket.getOutputStream(), true); if(!err) out.println("File Complete!"); else out.println("Error Sending File!"); clientSocket.close(); } catch (UnknownHostException e) { } catch (IOException e) { } } }