First i'm sorry for my english. :-) It's my first post here.
I have application, something like torrent. I run it in one computer but 2 or more instance. i must ask user what file he want, and send this file to client. if i want its be host-to-host or multi-host. and my problem is: when i send to client list file in directory , he choose one of them and send to server nameFile. then server send this file to client. But when i send listFile i must close bufferedreader because readline() is blocking. but if i close that i don't have connection. Any idea? please for any proposition. this is my code:
Server:
public class Server extends Thread {
private Socket s;
int numerKlienta;
String line;
List<String> list = new ArrayList();
private ServerSocket serverSocket;
String nazwaPliku = "";
PrintWriter outToClient;
String text = "";
String tmp = "";
public Server(int port) throws Exception {
serverSocket = new ServerSocket(port);
while (true) {
Socket clientSocket = serverSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
outToClient = new PrintWriter(clientSocket.getOutputStream(), true);
String path = "C:\\Users\\Ania\\Desktop";
File directory = new File(path);
File[] files = directory.listFiles();
for (int j = 0; j < files.length; j++) {
if (files[j].isFile()) {
text = files[j].getName();
outToClient.println(text);
}
}
//outToClient.flush();
outToClient.close(); //i must close beacuse in client when i writeBytes its blocking next steps
nazwaPliku = inFromClient.readLine();
System.out.println(nazwaPliku);
outToClient.close();
}
}
}
Client:
public class Client {
public Client(String host, int port) throws Exception{
s = new Socket(host, port);
DataOutputStream outToServer= new DataOutputStream(s.getOutputStream());
BufferedReader inFromServer =
new BufferedReader(new
InputStreamReader(s.getInputStream()));
System.out.println("lista plików w katalogu : ");
while ((( odpowiedz = inFromServer.readLine()) != null)){
System.out.println( odpowiedz);
}
//here is blocking and stop
System.out.println(" Jaki plik chcesz przesłać? podaj pełną nazwę");
Scanner sc= new Scanner(System.in);
String nazwaPliku=sc.next();
outToServer.writeBytes(nazwaPliku);
//saveFile(nazwaPliku);
}
And my Main:
public class Main {
public static void main(String[] args) throws Exception {
Server serwer=null;
System.out.println("Czy czy chcesz rozpocząc pracę jako serwer? (t/n)");
Scanner sc = new Scanner(System.in);
String odpowiedz = sc.next();
if (odpowiedz.equals("t")) {
System.out.println(" Na jakim porcie rozpocząc nasłuch?");
sc = new Scanner(System.in);
int portSerwera = sc.nextInt();
serwer = new Server(portSerwera);
//serwer.start();
}
else{
System.out.println("Czy chcesz rozpocząc połączenie z jakimś serwerem? (t/n)");
sc = new Scanner(System.in);
odpowiedz = sc.next();
if (odpowiedz.equals("t")) {
System.out.println("podaj numer portu do połączenia z serwerem");
sc = new Scanner(System.in);
int portKlienta = sc.nextInt();
Client fc = new Client("localhost", portKlienta);