The Artima Developer Community
Sponsored Link

Java Answers Forum
can't get server to send file to client

1 reply on 1 page. Most recent reply: Apr 20, 2003 12:04 PM by Mara Simon

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 1 reply on 1 page
gordon

Posts: 2
Nickname: gordo
Registered: Apr, 2003

can't get server to send file to client Posted: Apr 17, 2003 6:50 AM
Reply to this message Reply
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 ( );
}
}


Mara Simon

Posts: 2
Nickname: miresi
Registered: Apr, 2003

Re: can't get server to send file to client Posted: Apr 20, 2003 12:04 PM
Reply to this message Reply
> 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
> rminated connection" );
> }
>
> catch ( IOException ioException )
> {
> ioException.printStackTrace ( );
> }
> }
>
> private void getStreams ( ) throws IOException
> {
> output = new BufferedWriter ( new OutputStreamWriter
> riter ( connection.getOutputStream ( ) ) );
> output.flush ( );
> input = new BufferedReader ( new InputStreamReader (
> der ( connection.getInputStream ( ) ) );
> }
>
> private void processConnection ( ) throws IOException
> {
> message = ( String ) input.readLine;
> File file = new File ( message );
> BufferedReader fileInput = new BufferedReader ( new
> ( 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
> 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
> rminated connection" );
> }
>
> catch ( IOException ioException )
> {
> ioException.printStackTrace ( );
> }
> }
>
> private void connectToServer ( ) throws IOException
> {
> displayArea.setText ( "Attempting connection\n" );
> connection = new Socket ( InetAddress.getLocalHost (
> ost ( ), 8189 );
> displayArea.append ( "Connected to: " +
> : " + client.getInetAddress ( ).getHostName ( ) );
> }
>
> private void getStreams ( ) throws IOException
> {
> output = new BufferedWriter ( new OutputStreamWriter
> riter ( connection.getOutputStream ( ) ) );
> output.flush ( );
> input = new BufferedReader ( new InputStreamReader (
> der ( 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 (
> ion ( JFrame.EXIT_ON_CLOSE );
> application.runClient ( );
> }
> }
>


Shouldn't the first line of that method be
message = (String)input.readLine();

You could try something like this to see if the server got access to the port;

while (true) {
try {
sock = new ServerSocket(8189);
} catch (IOException e) {
System.out.println ("Could not listen on port: " + e);
}

Flat View: This topic has 1 reply on 1 page
Topic: Converting a float to object Previous Topic   Next Topic Topic: Editing a data file

Sponsored Links



Google
  Web Artima.com   

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