Hi.
Im creating a client-server programm, in which the server has to only send a requested file by the client. Im having problems on specifying the file.. i tried, but had no luck. The server can handle multiple connections (or at least should). As fas as i have understood, i should make the client send the file name of interest of the server, and then, the server should send the file. The client has a GUI, its code is here too.
How does everything look overall?
The server part.
MyServer code:
import java.net.*; import java.io.*; class SendData implements Runnable { String location="studenti02.txt"; Socket newCon; MyClient client; SendData(Socket newCon) { this.newCon=newCon; System.out.println("Thread started!"); run(); } public void run() { try { System.out.println("Using port: "+newCon.getPort()); System.out.println("Adress: "+ newCon.getInetAddress().getHostAddress()); File file=new File(location); byte [] bytearray=new byte[(int)file.length()]; FileInputStream fileInput = new FileInputStream(file); BufferedInputStream bufferedIn=new BufferedInputStream(fileInput); bufferedIn.read(bytearray,0,bytearray.length); OutputStream out= newCon.getOutputStream(); out.write(bytearray,0,bytearray.length); out.flush(); out.close(); bufferedIn.close(); newCon.close(); } catch(IOException e) { System.out.println(e.getStackTrace()); } } } public class MyServer { int port; String location; ServerSocket socket=null; MyServer(int port) { this.port=port; System.out.println("Server started"); try { socket=new ServerSocket(port); } catch(IOException e) { System.out.println(e.getStackTrace()); } while(true) { try { Socket newCon=socket.accept(); SendData conection=new SendData(newCon); } catch(IOException er) { System.out.println(er.getStackTrace()); } } } public static void main(String[] args) { MyServer server=new MyServer(1999); } }
MyClient Code:
import java.net.*; import java.io.*; public class MyClient { ClientFrame window; ErrorMessage err; String host; int port; int filesize=6022386; int bytesRead; int current=0; Socket server; MyClient() { System.out.println("Client started"); } public void serverCheck() { server.isConnected(); } public void setHost(String host) { this.host=host; } public void setPort(int port) { this.port=port; } public void RecieveFile() { try { Socket server=new Socket(host, port); server.setSoTimeout(1000); byte [] bytearray=new byte[filesize]; InputStream input=server.getInputStream(); FileOutputStream out=new FileOutputStream("data.txt"); BufferedOutputStream buffer=new BufferedOutputStream(out); bytesRead = input.read(bytearray,0,bytearray.length); current=bytesRead; do { bytesRead=input.read(bytearray,current,(bytearray.length-current)); if(bytesRead>=0) current+=bytesRead; }while(bytesRead>-1); buffer.write(bytearray,0,current); buffer.flush(); buffer.close(); server.close(); } catch(ConnectException b) { err=new ErrorMessage("Could not connect", "Bug found", window); err.setVisible(true); } catch(IOException e) { err=new ErrorMessage("Something is wrong..", "Bug found", window); err.setVisible(true); } } public static void main(String[] args) { MyClient client=new MyClient(); ClientFrame frame=new ClientFrame(client); } }
ClientFrame Code:
package mess; import javax.swing.*; import javax.swing.border.EmptyBorder; import java.awt.*; import java.awt.event.*; @SuppressWarnings("serial") public class ClientFrame extends JFrame { ErrorMessage dialog; private JTextField host; private JTextField socket; private JTextField file; private JButton button; private MyClient client; ClientFrame(MyClient client) { this.client=client; StartUI(); } public void StartUI() { setTitle("Client"); JPanel textPanel=new JPanel(new GridLayout(3,1)); add(textPanel, BorderLayout.CENTER); JPanel info=new JPanel(new GridLayout(3,1)); add(info, BorderLayout.WEST); host=new JTextField(20); JTextField hostLabel=new JTextField("Host"); hostLabel.setEditable(false); hostLabel.setBorder(new EmptyBorder(3,3,3,3)); socket=new JTextField(20); JTextField socketLabel=new JTextField("Socket"); socketLabel.setEditable(false); socketLabel.setBorder(new EmptyBorder(3,3,3,3)); file=new JTextField(30); JTextField fileLabel=new JTextField("File"); fileLabel.setEditable(false); fileLabel.setBorder(new EmptyBorder(3,3,3,3)); textPanel.add(host); textPanel.add(socket); textPanel.add(file); info.add(hostLabel); info.add(socketLabel); info.add(fileLabel); button=new JButton("Request"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(host.getText().equals("") || socket.getText().equals("") || file.getText().equals("")) { dialog=new ErrorMessage(" Something is missing! ","Excuse me, but..", null); dialog.setVisible(true); } else { try { client.setHost(host.getText()); client.setPort(Integer.parseInt(socket.getText())); client.RecieveFile(); } catch(NumberFormatException eror) { dialog=new ErrorMessage("Do you know how to use this?","I have to ask..", null); dialog.setVisible(true); } } } }); JPanel butPan=new JPanel(); add(butPan, BorderLayout.EAST); butPan.add(button, BorderLayout.NORTH); setLocationRelativeTo(null); pack(); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } }
ErrorMessage Code:
import javax.swing.*; import javax.swing.border.EmptyBorder; import java.awt.*; import java.awt.event.*; public class ErrorMessage extends JDialog { String message; String windowMes; ClientFrame parent; JTextField errorMes; public void Start() { JPanel forMessage=new JPanel(); add(forMessage, BorderLayout.CENTER); errorMes=new JTextField(); errorMes.setText(message); errorMes.setEditable(false); errorMes.setBorder(new EmptyBorder(10,10,10,10)); forMessage.add(errorMes); JPanel forButton=new JPanel(); add(forButton, BorderLayout.SOUTH); JButton ok=new JButton("Ok"); ok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setVisible(false); } }); forButton.add(ok); pack(); setLocationRelativeTo(null); } ErrorMessage(String message, String windowMes, ClientFrame parent) { super(parent, windowMes,true); this.message=message; Start(); } }