Preciso de um código de programa que um cliente peça( digite) uma página que está no servidor
e o servidor que contém este arquivo html envie para o cliente.
O cliente deve exibir este arquivo html.
Agradeço dede já.
Aplicativo Cliente Servidor
P
5 Respostas
Paulo,
E qual é a sua dúvida?
O que você já fez?
Diga ai as dúvidas, que vamos o ajudando.
Amigo só podemos te ajudar se vc postar algum tipo de código ou esclarecer melhor a sua dúvida… Obrigado
[ ] ´s
Uma dica é vc trabalhar com as Classes ServerSocket e Socket
P
O PROBLEMA TODO É COMO ENVIAR O ARQUIVO QUE ESTÁ NO SERVIDOR PARA O CLIENTE QUANDO O CLIENTE SOLICITAR O TAL ARQUIVO
import <a href="http://java.io">java.io</a>.<em>;
import <a href="http://java.net">java.net</a>.</em>;
import java.awt.<em>;
import java.awt.event.</em>;
import javax.swing.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class Client extends JFrame {
private JTextField enterField;
private JTextArea displayArea;
private ObjectOutputStream output;
private ObjectInputStream input;
private String message = “”;
private String chatServer;
private Socket client;
// initialize chatServer and set up GUI
public Client( String host )
{
super( “Client” );
// set server to which this client connects
chatServer = host;
Container container = getContentPane();
// create enterField and register listener
enterField = new JTextField();
enterField.setEnabled( false );
enterField.addActionListener(
new ActionListener() {
// send message to server
public void actionPerformed( ActionEvent event )
{
sendData( event.getActionCommand() );
}
} // end anonymous inner class
); // end call to addActionListener
container.add( enterField, BorderLayout.NORTH );
// create displayArea
displayArea = new JTextArea();
container.add( new JScrollPane( displayArea ),
BorderLayout.CENTER );
setSize( 300, 150 );
setVisible( true );
}
// connect to server and process messages from server
public void runClient()
{
// connect to server, get streams, process connection
try {
// Step 1: Create a Socket to make connection
connectToServer();
// Step 2: Get the input and output streams
getStreams();
// Step 3: Process connection
processConnection();
// Step 4: Close connection
closeConnection();
}
// server closed connection
catch ( EOFException eofException ) {
System.out.println( "Server terminated connection" );
}
// process problems communicating with server
catch ( IOException ioException ) {
ioException.printStackTrace();
}
}
// get streams to send and receive data
private void getStreams() throws IOException
{
// set up output stream for objects
output = new ObjectOutputStream(
client.getOutputStream() );
// flush output buffer to send header information
output.flush();
// set up input stream for objects
input = new ObjectInputStream(
client.getInputStream() );
displayArea.append( "\nGot I/O streams\n" );
}
// connect to server
private void connectToServer() throws IOException
{
displayArea.setText( “Attempting connection\n” );
// create Socket to make connection to server
client = new Socket(
InetAddress.getByName( chatServer ), 5000 );
// display connection information
displayArea.append( "Connected to: " +
client.getInetAddress().getHostName() );
}
// process connection with server
private void processConnection() throws IOException
{
// enable enterField so client user can send messages
enterField.setEnabled( true );
// process messages sent from server
do {
// read message and display it
try {
message = ( String ) input.readObject();
displayArea.append( "\n" + message );
displayArea.setCaretPosition(
displayArea.getText().length() );
}
// catch problems reading from server
catch ( ClassNotFoundException classNotFoundException ) {
displayArea.append( "\nUnknown object type received" );
}
} while ( !message.equals( "SERVER>>> TERMINATE" ) );
} // end method process connection
// close streams and socket
private void closeConnection() throws IOException
{
displayArea.append( “\nClosing connection” );
output.close();
input.close();
client.close();
}
// send message to server
private void sendData( String message )
{
// send object to server
try {
output.writeObject( "CLIENT>>> " + message );
output.flush();
displayArea.append( “\nCLIENT>>>” + message );
}
// process problems sending object
catch ( IOException ioException ) {
displayArea.append( "\nError writing object" );
}
}
// execute application
public static void main( String args[] )
{
Client application;
if ( args.length == 0 )
application = new Client( "127.0.0.1" );
else
application = new Client( args[ 0 ] );
application.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE );
application.runClient();
}
} // end class Client
import java.awt.<em>;
import java.awt.event.</em>;
import <a href="http://java.net">java.net</a>.<em>;
import <a href="http://java.io">java.io</a>.</em>;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class ReadServerFile extends JFrame {
private JTextField enterField;
private JEditorPane contentsArea;
// set up GUI
public ReadServerFile()
{
super( “Simple Web Browser” );
Container container = getContentPane();
// create enterField and register its listener
enterField = new JTextField( "Enter file URL here" );
enterField.addActionListener(
new ActionListener() {
// get document specified by user
public void actionPerformed( ActionEvent event )
{
getThePage( event.getActionCommand() );
}
} // end anonymous inner class
); // end call to addActionListener
container.add( enterField, BorderLayout.NORTH );
// create contentsArea and register HyperlinkEvent listener
contentsArea = new JEditorPane();
contentsArea.setEditable( false );
contentsArea.addHyperlinkListener(
new HyperlinkListener() {
// if user clicked hyperlink, go to specified page
public void hyperlinkUpdate( HyperlinkEvent event )
{
if ( event.getEventType() ==
HyperlinkEvent.EventType.ACTIVATED )
getThePage( event.getURL().toString() );
}
} // end anonymous inner class
); // end call to addHyperlinkListener
container.add( new JScrollPane( contentsArea ),
BorderLayout.CENTER );
setSize( 400, 300 );
setVisible( true );
}
// load document; change mouse cursor to indicate status
private void getThePage( String location )
{
// change mouse cursor to WAIT_CURSOR
setCursor( Cursor.getPredefinedCursor(
Cursor.WAIT_CURSOR ) );
// load document into contentsArea and display location in
// enterField
try {
contentsArea.setPage( location );
enterField.setText( location );
}
// process problems loading document
catch ( IOException ioException ) {
JOptionPane.showMessageDialog( this,
"Error retrieving specified URL",
"Bad URL", JOptionPane.ERROR_MESSAGE );
}
setCursor( Cursor.getPredefinedCursor(
Cursor.DEFAULT_CURSOR ) );
}
// begin application execution
public static void main( String args[] )
{
ReadServerFile application = new ReadServerFile();
application.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE );
}
} // end class ReadServerFile
import <a href="http://java.io">java.io</a>.<em>;
import <a href="http://java.net">java.net</a>.</em>;
import java.awt.<em>;
import java.awt.event.</em>;
import javax.swing.*;
public class Server extends JFrame {
private JTextField enterField;
private JTextArea displayArea;
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;
private int counter = 1;
// set up GUI
public Server()
{
super( “Server” );
Container container = getContentPane();
// create enterField and register listener
enterField = new JTextField();
enterField.setEnabled( false );
enterField.addActionListener(
new ActionListener() {
// send message to client
public void actionPerformed( ActionEvent event )
{
sendData( event.getActionCommand() );
}
} // end anonymous inner class
); // end call to addActionListener
container.add( enterField, BorderLayout.NORTH );
// create displayArea
displayArea = new JTextArea();
container.add( new JScrollPane( displayArea ),
BorderLayout.CENTER );
setSize( 300, 150 );
setVisible( true );
}
// set up and run server
public void runServer()
{
// set up server to receive connections;
// process connections
try {
// Step 1: Create a ServerSocket.
server = new ServerSocket( 5000, 100 );
while ( true ) {
// Step 2: Wait for a connection.
waitForConnection();
// Step 3: Get input and output streams.
getStreams();
// Step 4: Process connection.
processConnection();
// Step 5: Close connection.
closeConnection();
++counter;
}
}
// process EOFException when client closes connection
catch ( EOFException eofException ) {
System.out.println( "Client terminated connection" );
}
// process problems with I/O
catch ( IOException ioException ) {
ioException.printStackTrace();
}
}
// wait for connection to arrive, then display connection info
private void waitForConnection() throws IOException
{
displayArea.setText( “Waiting for connection\n” );
// allow server to accept a connection
connection = server.accept();
displayArea.append( "Connection " + counter +
" received from: " +
connection.getInetAddress().getHostName() );
}
// get streams to send and receive data
private void getStreams() throws IOException
{
// set up output stream for objects
output = new ObjectOutputStream(
connection.getOutputStream() );
// flush output buffer to send header information
output.flush();
// set up input stream for objects
input = new ObjectInputStream(
connection.getInputStream() );
displayArea.append( "\nGot I/O streams\n" );
}
// process connection with client
private void processConnection() throws IOException
{
// send connection successful message to client
String message = “SERVER>>> Connection successful”;
output.writeObject( message );
output.flush();
// enable enterField so server user can send messages
enterField.setEnabled( true );
// process messages sent from client
do {
// read message and display it
try {
message = ( String ) input.readObject();
displayArea.append( "\n" + message );
displayArea.setCaretPosition(
displayArea.getText().length() );
}
// catch problems reading from client
catch ( ClassNotFoundException classNotFoundException ) {
displayArea.append( "\nUnknown object type received" );
}
} while ( !message.equals( "CLIENT>>> TERMINATE" ) );
}
// close streams and socket
private void closeConnection() throws IOException
{
displayArea.append( “\nUser terminated connection” );
enterField.setEnabled( false );
output.close();
input.close();
connection.close();
}
// send message to client
private void sendData( String message )
{
// send object to client
try {
output.writeObject( "SERVER>>> " + message );
output.flush();
displayArea.append( “\nSERVER>>>” + message );
}
// process problems sending object
catch ( IOException ioException ) {
displayArea.append( "\nError writing object" );
}
}
// execute application
public static void main( String args[] )
{
Server application = new Server();
application.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE );
application.runServer();
}
} // end class Server
H
bom
Criado 14 de maio de 2007
Ultima resposta 5 de ago. de 2009
Respostas 5
Participantes 5
Alura O que é Python? — um guia completo para iniciar nessa linguagem de programação Acesse agora o guia sobre Python e inicie sua jornada nessa linguagem de programação: o que é e para que serve, sua sintaxe e como iniciar nela!
Casa do Codigo Engenharia de Prompt para Devs: Um guia para aprender a... Por Ricardo Pupo Larguesa — Casa do Codigo