E ae galera, blz?
Estou com um prob aki.
Tenho um programa com 2 classes q consiste em :
Um server q recebe dois tipos de pedidos - 1º solicitação de um arquivo :
Verificar se o arquivo existe e retorna o conteúdo ao client.
2º atualização do arquivo, o client modifica o conteúdo e devolve o arquivo para ser salvo no server.
Segue as 2 classes :
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
public class FileCheckerServer extends JFrame {
private ServerSocket server;
private Socket connection;
private ObjectInputStream input;
private ObjectOutputStream output;
private JTextArea area;
private String archieveContents = "Conteúdo do arquivo:\n";
private File arquivo;
public FileCheckerServer()
{
super ( "FileCheckerServer" );
area = new JTextArea();
area.setEditable ( false );
getContentPane().add ( new JScrollPane ( area ),
BorderLayout.CENTER );
setSize ( 200, 200 );
setVisible ( true );
}
public void activate()
{
try {
server = new ServerSocket ( 5000, 1 );
while ( true ) {
waitConnection();
getStreams();
processConnection();
closeConnection();
}
}
catch ( ClassNotFoundException cnfex ) {
cnfex.printStackTrace();
}
catch ( EOFException eofex ) {
System.err.println ( "Erro em EOFEX" );
}
catch ( IOException ioex ) {
ioex.printStackTrace();
}
}
private void waitConnection() throws IOException
{
area.append ( "Espeando conecção do client\n" );
connection = server.accept();
area.append ( "conecção feita com " +
connection.getInetAddress() );
}
private void getStreams() throws IOException
{
output = new ObjectOutputStream (
connection.getOutputStream() );
output.flush();
input = new ObjectInputStream (
connection.getInputStream() );
}
private void processConnection() throws IOException, EOFException,
ClassNotFoundException
{
String checker = input.readLine();
if ( checker.regionMatches ( 0, "C:\"", 3, 3 ) ) {
arquivo = new File ( input.readLine() );
if ( arquivo.exists() ) {
area.append ( "Arquivo existente, enviando ao client\n" );
if ( !arquivo.isDirectory() ) {
BufferedReader reader = new BufferedReader (
new FileReader ( arquivo ) );
String temp;
while ( ( temp = input.readLine() ) != null )
archieveContents += temp + "\n";
}
else {
String[] conteudo = arquivo.list();
for ( int i = 0; i < conteudo.length; i++ )
archieveContents += conteudo [ i ] + "\n";
}
}
area.append ( "\nArquivo carregado para o buffer" );
output.writeObject ( archieveContents );
output.flush();
}
else {
output = new ObjectOutputStream (
new FileOutputStream ( arquivo ) );
output.reset();
output.writeObject ( input.readObject() );
output.flush();
}
}
private void closeConnection() throws IOException
{
area.append ( "\nTransferência completa, fechando conecção" );
input.close();
output.close();
connection.close();
}
public static void main ( String[] args )
{
FileCheckerServer app = new FileCheckerServer();
app.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
app.activate();
}
}
E o client :
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FileCheckerClient extends JFrame {
private JTextField inputField;
private JTextArea contentArea;
private ObjectInputStream input;
private ObjectOutputStream output;
private String serverAddress = "localhost";
private Socket client;
private int choose;
private static int ok = 1;
private JButton atualizar;
public FileCheckerClient()
{
super ( "FileCheckerFile" );
inputField = new JTextField ( "Digite o caminho completo " +
"ao arquivo" );
inputField.addActionListener (
new ActionListener()
{
public void actionPerformed ( ActionEvent e )
{
procurarArquivo( e.getActionCommand() );
}
}
);
getContentPane().add ( inputField, BorderLayout.NORTH );
contentArea = new JTextArea();
contentArea.setEditable ( false );
getContentPane().add ( new JScrollPane (
contentArea ), BorderLayout.CENTER );
atualizar = new JButton ( "Atualizar Arquivo" );
atualizar.setEnabled ( false );
atualizar.addActionListener (
new ActionListener()
{
public void actionPerformed ( ActionEvent e )
{
atualizarArquivo();
}
}
);
getContentPane().add ( atualizar, BorderLayout.SOUTH );
setSize ( 400, 300 );
setVisible ( true );
}
public void ativar()
{
try {
while ( true ) {
connectServer();
getStreams();
processConnection();
closeConnection();
}
}
catch ( EOFException eofex ) {
System.err.println ( "Erro no fim do arquivo [ client ] " );
}
catch ( IOException ioex ) {
ioex.printStackTrace();
}
}
private void connectServer() throws IOException
{
setTitle ( "Estabelecendo conecção..." );
client = new Socket (
InetAddress.getByName (
serverAddress ), 5000 );
setTitle ( "Conecção Estabelecida com sucesso " );
}
private void getStreams() throws IOException
{
output = new ObjectOutputStream (
client.getOutputStream() );
output.flush();
input = new ObjectInputStream (
client.getInputStream() );
setTitle ( "I/O Ok" );
}
private void processConnection() throws IOException
{
contentArea.setEditable ( true );
try {
contentArea.append ( ( String ) input.readObject() );
}
catch ( ClassNotFoundException cnfex ) {
System.err.println ( "Erro no cnfex do client" );
cnfex.printStackTrace();
}
}
private void closeConnection() throws IOException
{
if ( ok < 1 ) {
input.close();
output.close();
client.close();
}
}
private void procurarArquivo ( String arquivo )
{
try {
output = new ObjectOutputStream (
client.getOutputStream() );
output.writeObject ( arquivo );
output.flush();
setTitle ( "Arquivo encaminhado ao Server " +
"para busca" );
contentArea.append ( "Procurando arquivo..." );
}
catch ( IOException ioex ) {
ioex.printStackTrace();
}
atualizar.setEnabled ( true );
}
private void atualizarArquivo()
{
try {
String novo = contentArea.getText();
output.writeObject ( novo );
output.flush();
ok = 0;
}
catch ( IOException ioex ) {
System.err.println ( "Erro ao atualizar arquivo" );
ioex.printStackTrace();
}
setTitle ( "Done" );
}
public static void main ( String[] args )
{
FileCheckerClient app = new FileCheckerClient();
app.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
app.ativar();
}
}
O server starta normalmente, os dois se conectam, porem, quando o client solicita um arquivo ( actionPerformed do inputField ) dá o seguinte erro :
java.io.StreamCorruptedException
at java.io.ObjectInputStream$BlockDataInputStream.readBlockHeader(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.refill(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.read(Unknown Source)
at java.io.DataInputStream.readLine(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readLine(Unknown Source)
at java.io.ObjectInputStream.readLine(Unknown Source)
at FileCheckerServer.processConnection(FileCheckerServer.java:90)
at FileCheckerServer.activate(FileCheckerServer.java:45)
at FileCheckerServer.main(FileCheckerServer.java:153)
ps :
Pq n dá pra recortar e colar direto do eclipse pra k ?