gordo henkel

Greenhorn
+ Follow
since Apr 14, 2003
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by gordo henkel

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.

21 years ago
Thanks...Please see "revised client server problem". I've gotten farther but still can't get it to work.
21 years ago
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 ( );
}
}
21 years ago
If I had Java installed on my current workstation I would certainly run the program. Right now I only have access to notepad and won't be in front of a java enabled station until tomorrow. Just trying to get a start on something and wanted to know if I was heading in the right direction.
Thank you for your enlightening response.
21 years ago
I'm writing a client server program where the client can enter a file name in a JTextField and the server will retrieve the file and send it back to the client. I've got the client side down. Concerning my server program, please advise if my code looks right. The streams I have used are what is required. As the code now stands, will a file be retrieved from the current directory and sent back to the client? You might see extra code that is not relevant to my question; that is for future requirements. Below is code...
Thank you for your assistance.
import java.io.*;
import java.net.*;
public class Server
{
private BufferedWriter output;
private BufferedReader input;
public static void main ( String [ ] args )
{
int counter = 0;
try
{
ServerSocket server = new ServerSocket ( 5000 );
while ( counter < 2 )
{
Socket 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
{
File file = new File ( input );
BufferedReader fileInput = new BufferedReader ( new InputStreamReader
( new FileInputStream ( file ) ) );
BufferedWriter writer = new BufferedWriter ( new FileWriter ( file ) );
while ( fileInput.peek ( ) != '\0' )
{
String line = fileInput.getln ( );
output.println ( line );
}
output.flush ( );
writer.close ( );
}
private void closeConnection ( ) throws IOException
{
output.close ( );
input.close ( );
connection.close ( );
}
}
21 years ago