package server;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
//import java.lang.Class;
import java.util.ArrayList;
import java.util.List;
//import java.applet.AppletContext;
//import java.util.HashMap;
//import java.util.Map;
//import java.util.Map.Entry;
public class Server {
private static final int PORT = 80;
private static ServerSocket server_socket = null;
private static Socket client_socket = null;
private InputStream data_in = null;
private OutputStream data_out = null;
public List<String> data_out_2 = new ArrayList<String>();
public static boolean flag = true;
public static String APP_PATH = "localhost:"+PORT;//APP_PATH
public static void main(String[] args) throws IOException{
Server server_ = new Server();
//boolean flag = true;
server_.connect();
}
public void connect() throws IOException{
server_socket = null;
try {
server_socket = new ServerSocket(PORT);
}
catch(IOException e){
System.err.println("Nie mozna utworzyc gniazda na porcie: "+PORT);
System.exit(1);
}
System.out.print("Trwa łączenie...");
Thread th = new Thread(new Runnable(){
@Override
public void run(){
System.out.println("\n Nasluch na porcie: "+PORT);
}
});
th.start();
client_socket = null;
try{
client_socket = server_socket.accept();
}
catch(IOException e){
Server.flag = false;
System.err.println("Nasluch nie powiodl sie.");
th.interrupt();
System.exit(2);
}
final PrintWriter data_pwr = new PrintWriter(client_socket.getOutputStream(), true);
final BufferedReader data_brr = new BufferedReader( new InputStreamReader( client_socket.getInputStream() ) );
/**
*Dane testowe
*/
final View view_1 = new View();
this.data_out_2.add(0,"5");
this.data_out_2.add(1,"6");
this.data_out_2.add(2,"7");
this.data_out_2.add(3,"8");
this.data_out_2.add(4,"9");
//wypelniony html danymi
final String data_out_3 = view_1.account_list(data_out_2);
th = new Thread(new Runnable() {
@Override
public void run() {
try{
Thread.sleep(3000);
while(true){
//data_pwr.println("response");
String input = data_brr.readLine();
if( input.equals("end") ){
Server.flag = false;
}
data_pwr.println(data_out_3);
//System.out.print(data_out_3);
data_pwr.close();
data_brr.close();
client_socket.close();
server_socket.close();
break;
}
}
catch(Exception e){
Server.flag = false;
System.err.println("Niespodziewany blad.");
}
}
});
th.start();
}
}
Serwer się wyłącza po pierwszym żądaniu przeglądarki, próbowałem wywoływać connect() w pętli(serwer uruchomiony, automatyczne wywołanie connect() w main(), żadanie->obsługa żądania->wyłączenie serwera i wywołanie connect()... ale potem mam wyjątek: Niespodziewany blad pomimo że wszystko jest zamykane tyle że w wątku.