gordon
Posts: 2
Nickname: gordo
Registered: Apr, 2003
can't get server to send file to client
Posted: Apr 17, 2003 6:50 AM
Advertisement
On the client side, when I type in a filename in the JTextField and press enter, nothing happens. I want the server to get the file and send the contents back to the client. The client is connecting; I believe my problem is in my processConnection method in the server file. Please advise. The code for both server.java and client.java is below. import java.io.*; import java.net.*; public class Server { private BufferedWriter output; private BufferedReader input; private ServerSocket server; private Socket connection; private String message; public void runServer ( ) { try { server = new ServerSocket ( 8189 ); connection = server.accept ( ); getStreams ( ); processConnection ( ); closeConnection ( ); } catch ( EOFException eofException ) { System.out.println ( "Client terminated connection" ); } catch ( IOException ioException ) { ioException.printStackTrace ( ); } } private void getStreams ( ) throws IOException { output = new BufferedWriter ( new OutputStreamWriter ( connection.getOutputStream ( ) ) ); output.flush ( ); input = new BufferedReader ( new InputStreamReader ( connection.getInputStream ( ) ) ); } private void processConnection ( ) throws IOException { message = ( String ) input.readLine; File file = new File ( message ); BufferedReader fileInput = new BufferedReader ( new InputStreamReader ( new FileInputStream ( file ) ) ); String str; while ( ( str = fileInput.readLine ( ) ) != null ) { output.write ( str ); output.newLine ( ); } output.flush ( ); fileInput.close ( ); } private void closeConnection ( ) throws IOException { output.close ( ); input.close ( ); connection.close ( ); } public static void main ( String [ ] args ) { Server application = new Server ( ); application.runServer ( ); } }
import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Client extends JFrame { private JTextField enterField; private JTextArea displayArea; private BufferedWriter output; private BufferedReader input; private Socket connection; private String message; public Client ( ) { super ( "Client" ); Container container = getContentPane ( ); enterField = new JTextField ( 10 ); enterField.addActionListener ( new ActionListener ( ) { public void actionPerformed ( ActionEvent event ) { sendFile ( enterField.getText ( ) ); } } ); container.add ( enterField, BorderLayout.NORTH ); displayArea = new JTextArea ( ); container.add ( displayArea, BorderLayout.CENTER ); setSize ( 500, 400 ); setVisible ( true ); } public void runClient ( ) { try { connectToServer ( ); getStreams ( ); processConnection ( ); closeConnection ( ); } catch ( EOFException eofException ) { System.out.println ( "Server terminated connection" ); } catch ( IOException ioException ) { ioException.printStackTrace ( ); } } private void connectToServer ( ) throws IOException { displayArea.setText ( "Attempting connection\n" ); connection = new Socket ( InetAddress.getLocalHost ( ), 8189 ); displayArea.append ( "Connected to: " + client.getInetAddress ( ).getHostName ( ) ); } private void getStreams ( ) throws IOException { output = new BufferedWriter ( new OutputStreamWriter ( connection.getOutputStream ( ) ) ); output.flush ( ); input = new BufferedReader ( new InputStreamReader ( connection.getInputStream ( ) ) ); displayArea.append ( "\nGot I/O streams\n" ); } private void processConnection ( ) throws IOException { message = ( String ) input.readLine; displayArea.append ( "\n" + message ); } private void closeConnection ( ) throws IOException { displayArea.append ( "\nClosing connection" ); output.close ( ); input.close ( ); connection.close ( ); } private void sendFile ( String message ) { try { output.write ( message ); output.newLine ( ); output.flush ( ); } catch ( IOException ioException ) { displayArea.append ( "\nError writing string" ); } } public static void main ( String [ ] args ) { Client application = new Client ( ); application.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE ); application.runClient ( ); } }