The Artima Developer Community
Sponsored Link

Java Answers Forum
CLIENT SERVER PROBLEM

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
gordon

Posts: 2
Nickname: gordo
Registered: Apr, 2003

CLIENT SERVER PROBLEM Posted: Apr 14, 2003 5:38 PM
Reply to this message Reply
Advertisement
OK, I've 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 ( );
}

}

Topic: Please ask who have the practice answer of the Think in Java Second Previous Topic   Next Topic Topic: Think in Java Second Edition

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use