File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes revised - client server problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "revised - client server problem" Watch "revised - client server problem" New topic
Author

revised - client server problem

gordo henkel
Greenhorn

Joined: Apr 14, 2003
Posts: 5
OK, I've gotten to a workstation with Java and have gotten this far on my own, but now I need a little help.
My program is supposed to allow a client to type in a file name in a JTextField and have the server return the file contents. The program does absolutely nothing. Both java files compile with no errors. First I run the server program. Then I open another MS-DOS window and run the client program. The GUI displays as expected. When I type in a file name (one that I created in the same directory to test the program) in the JTextField and press Enter, nothing happens.
Can you tell me why? The streams I am using to communicate between server and client and to read from a file are what I am required to use. Is there a problem with my streams? Is my connection on the client side coded correctly? Below is the code for server.java and client.java.
Thank you very much!

import java.io.*;
import java.net.*;
public class Server
{
private BufferedWriter output;
private BufferedReader input;
private Socket connection;
String str;
public void runServer ( )
{
int counter = 0;
try
{
ServerSocket server = new ServerSocket ( 8189 );
while ( counter < 2 )
{
connection = server.accept ( );
getStreams ( );
processConnection ( );
closeConnection ( );
counter++;
}
}
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
{
String fileName = input.readLine ( );
File file = new File ( fileName );
BufferedReader fileInput = new BufferedReader ( new InputStreamReader
( new FileInputStream ( file ) ) );
BufferedWriter writer = new BufferedWriter ( new FileWriter ( file ) );
while ( ( str = fileInput.readLine ( ) ) != null )
{
output.write ( str );
output.newLine ( );
}
output.flush ( );
writer.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 JButton saveButton;
private JTextArea displayArea;
private BufferedWriter output;
private BufferedReader input;
private Socket connection;
public Client ( )
{
super ( "Client" );
Container container = getContentPane ( );
JPanel panel = new JPanel ( );
JLabel displayLabel = new JLabel ( "Enter file name to retrieve: " );
panel.add ( displayLabel );
enterField = new JTextField ( 10 );
enterField.setEnabled ( false );
enterField.addActionListener (
new ActionListener ( )
{
public void actionPerformed ( ActionEvent event )
{
sendFile ( enterField.getText ( ) );
}

}
);
panel.add ( enterField );
saveButton = new JButton ( "Save Changes" );
saveButton.addActionListener (
new ActionListener ( )
{
public void actionPerformed ( ActionEvent event )
{
sendFile ( event.getActionCommand ( ) );
}

}
);
panel.add ( saveButton );
container.add ( panel, BorderLayout.NORTH );
displayArea = new JTextArea ( );
container.add ( new JScrollPane ( displayArea ), BorderLayout.CENTER );
setSize ( 500, 400 );
setVisible ( true );
}
public void runClient ( )
{
try
{
connection = new Socket ( InetAddress.getLocalHost ( ), 8189 );
getStreams ( );
processConnection ( );
closeConnection ( );
}
catch ( EOFException eofException )
{
System.out.println ( "Server 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
{
String message;
enterField.setEnabled ( true );
while ( ( message = input.readLine ( ) ) != null)
{
displayArea.append ( message + "\n" );
}
}
private void closeConnection ( ) throws IOException
{
output.close ( );
input.close ( );
connection.close ( );
}
private void sendFile ( String message )
{
try
{
output.write ( message );
output.flush ( );
}
catch ( IOException ioException )
{
displayArea.append ( "\nError writing" );
}
}
public static void main ( String [ ] args )
{
Client application = new Client ( );
application.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
application.runClient ( );
}
}
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

I recommend putting System.out.printlns in your code to see where your problems are. (And surround your code with [ CODE ] ... [ /CODE ] tags when posting here to enhance readability).
I noticed that your code is blocking at the

in the server; that is, that line of code never terminates. I am not overly knowledgable when i comes to Socket communication, so maybe someone else who is can elaborate on why this might happen.
Also, in you client code, your sendFile method always sends "Save Changes" instead of the filename that I typed into the text field.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: revised - client server problem