Pessoal eu sou novo por aki e estou com um problema e espero poder contar com todos. Como sou novo na área de Java, vou procurar ser bem específico em relação a minha dúvida. O meu prof. pediu para eu fazer um trab. de copia de arquivo. Eu criei duas pastas, sendo uma ([color=“blue”]CLT[/color]) cliente, e outra, ([color=“red”]SRV[/color]) servidor. O servidor vai ter q ficar ouvindo em uma porta, enquanto que o cliente vai solicitar um arquivo qualquer .txt, e o servidor vai ter q enviar os arquivos q já se encontram na pasta ([color=“red”]SRV[/color]), para a pasta ([color=“blue”]CLT[/color]). Bem eu consegui criar o cliente e a interface do mesmo, mas o servidor ñ está enviando o arquivo que o cliente está pedindo. Resolvi, colocar os dois aki para maiores esclarecimentos.
Este é o cliente:
import java.awt.;
import javax.swing.;
import java.awt.event.;
import java.io.;
import java.net.*;
public class Grupos extends JFrame implements ActionListener
{
JLabel troca = new JLabel(“FTP Troca de Arquivo”);
JTextField pergunta = new JTextField(15);
JButton b1 = new JButton(“Download”);
JTextArea resposta = new JTextArea(5,15);
public Grupos(){
super("Troca de Arquivo");
setSize(250,225);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
resposta.setLineWrap(true);
resposta.setWrapStyleWord(true);
pane.add(troca);
pane.add(pergunta);
b1.addActionListener(this);
pane.add(b1);
pane.add(resposta);
setContentPane(pane);
show();
}
public static void main (String[] args)
{
new Grupos();
}
public void actionPerformed(ActionEvent ae)
{
try {
Socket s = new Socket("127.0.0.1",2025);
InputStreamReader isr = new InputStreamReader(
s.getInputStream());
BufferedReader is = new BufferedReader(isr);
PrintWriter os = new PrintWriter(new
BufferedOutputStream(s.getOutputStream()), false);
String outLine = pergunta.getText();
os.println(outLine);
os.flush();
File f= new File(pergunta.getText());
String inLine;
inLine = is.readLine();
while (inLine.length() > 0) {
//f.append(inLine);
resposta.setText("" + inLine);
inLine = is.readLine();
}
os.close();
is.close();
s.close();
} catch (Exception e) {
resposta.setText("Error: " + e);
}
}
}
E este é o Servidor:
import java.io.;
import java.net.;
public class Central extends Thread {
private ServerSocket sock;
public String nome[];
public Central() {
super("Central Server");
//nome = new nome;
try {
sock = new ServerSocket(2025);
System.out.println("Central Server executando ...");
} catch (IOException e) {
System.err.println("Error: couldn't create socket.");
System.exit(1);
}
}
public void run() {
Socket client = null;
// Look for clients
while (true) {
// Wait for a client
if (sock == null)
return;
try {
client = sock.accept();
} catch (IOException e) {
System.err.println("Error: couldn't connect to client.");
System.exit(1);
}
// Process Client requests
try {
InputStreamReader isr = new InputStreamReader(client.getInputStream());
BufferedReader is = new BufferedReader(isr);
PrintWriter os = new PrintWriter(new BufferedOutputStream(client.getOutputStream()), false);
String outLine = null;
String filename = null;
// Process and output user input
String inLine = is.readLine();
if (inLine.length() > 0) {
filename=inLine;
System.out.println(filename);
}
File f = new File(filename);
if (f.exists()) {
System.out.println("Achei");
os.print("Achei");
FileReader fr=new FileReader(f);
//outLine = fr.ReaderLine();
os.print(outLine);
} else {
System.out.println("Nao Achei");
//os.print(StreamOut, "nao existe arquivo");
}
// Clean up
os.close();
is.close();
} catch (Exception e) {
System.err.println("Error: " + e);
e.printStackTrace();
}
}
}
public static void main(String[] arguments) {
Central server = new Central();
server.start();
}
}
Não sei por onde começar.
Quem poder ajudar eu agradeço.