Forums Register Login

revised - client server problem

+Pie Number of slices to send: Send
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 ( );
}
}
+Pie Number of slices to send: Send
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.
bacon. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1032 times.
Similar Threads
can't get server to send file to client
Writing to files using socket server
Help!!!!!! GridBag Layout constraints
can't get server to send file to client
client and server network
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 22:27:54.