Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void start() throws IOException {
- // ================================
- // A implementar
- // ================================
- try {
- serverSocket = new ServerSocket(PORT);
- while(!end){
- clientSocket = serverSocket.accept();
- //processem la petició del client.
- proccesClientRequest(clientSocket);
- //tanquem el socket temporal per atendre el client
- }
- //tanquem el socket principal
- if(serverSocket!=null && !serverSocket.isClosed()){
- //serverSocket.close();
- }
- } catch (IOException ex) {
- System.out.println("Excepció; "+ ex);
- }
- }
- private void proccesClientRequest(Socket clientSocket) {
- try {
- in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
- out= new PrintStream(clientSocket.getOutputStream());
- //processem el missatge del client i generem la resposta.
- NomJugador=in.readLine();
- Byte dataToSend = processData(NomJugador);
- out.println(dataToSend);
- out.flush();
- System.out.println(dataToSend.intValue());
- if (dataToSend.intValue()==Constants.OK){
- out.println(jug.getCodi());
- out.flush();
- mPartida.comunicaATotsElsJugadorsEstatPartida();
- }
- } catch (IOException ex) {
- Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- private Byte processData(String NomJugador) {
- Byte byteToSend;
- if(NomJugador.length()==0){
- byteToSend = Constants.ERROR;
- }else{
- if (mPartida.acceptaJugadors()){
- byteToSend = Constants.OK;
- try {
- jug = new Jugador(NomJugador,clientSocket,mPartida);
- mPartida.afegirJugador(jug);
- Thread t = new Thread(jug);
- t.start();
- } catch (IOException ex) {
- Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
- }
- }else {
- byteToSend = Constants.ERROR;
- }
- }
- return byteToSend;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement