Ajuda na Lógica do Exercicio

0 respostas
Leandro_Santana

Crie um Quiz usando Sockets e
Threads que no arquivo cliente seja
apresentada ao usuário uma
pergunta e o mesmo deve escolher como resposta
Exercício
?sim? ou ?não?. No servidor deve ser contabilizado
quantos porcento de ?sim? e quantos porcento de
?não? foram respondidos, esta informação deve ser
enviada para os clientes simultaneamente.

Consegui montar a estrutura Servidor e Cliente,porém a lógica de enviar a porcentagem de qtos sim ou não já foram respondidos não estou conseguindo implementar.
Segue código abaixo.

//Servidor

import java.io.*;
import java.net.*;
import java.util.*;
public class Servidor extends Thread {
 private static Vector clientes;
 private Socket conexao;
 private String meuNome;

 public static void main(String args[]) {
	  clientes = new Vector();

	  try {
		    ServerSocket s = new ServerSocket(2222);
		    while (true) {
			    System.out.print("Esperando alguem se conectar...");
			    Socket conexao = s.accept();
			    System.out.println(" Conectou!");
			    Thread t = new Servidor(conexao);
			    t.start();
	   		}
	  }catch (IOException e) {
		   System.out.println("IOException: " + e);
	  }
 }


 public Servidor(Socket s) {
  	conexao = s;
 }

 public void run() {
	  try {
		   BufferedReader entrada = new BufferedReader(new InputStreamReader(conexao.getInputStream()));
		   PrintStream saida = new PrintStream(conexao.getOutputStream());
		   meuNome = entrada.readLine();

		   if (meuNome == null) {return;}
		   clientes.add(saida);

		   String linha = entrada.readLine();
		   while (linha != null && !(linha.trim().equals(""))) {
			    sendToAll(saida, " disse: ", linha);
			    linha = entrada.readLine();
		   }
		   sendToAll(saida, " saiu ", "do chat!");
		   clientes.remove(saida);
		   conexao.close();
	  }catch (IOException e) {
   		   System.out.println("IOException: " + e);
      }
 }


 public void sendToAll(PrintStream saida, String acao,String linha) throws IOException {
	  Enumeration e = clientes.elements();
	  while (e.hasMoreElements()) {
		   PrintStream chat = (PrintStream) e.nextElement();
		   if (chat != saida) {
		   	  chat.println(meuNome + acao + linha);
		   }
	  }
 }
}

//CLIENTE

import java.io.*;
import java.net.*;
public class Cliente extends Thread {
 private Socket conexao;
 private static boolean done = false;
 public static void main(String args[]) {
  float cont=0.0f;
  try {
	   Socket conexao = new Socket("127.0.0.1", 2222);
	   PrintStream saida = new PrintStream(conexao.getOutputStream());
	   BufferedReader teclado = new BufferedReader(new InputStreamReader(System.in));
	   System.out.println("Responda SIM ou NÃO");
	   System.out.print("Você Sabe quem descobriu o Brasil? : ");
		 String resp = teclado.readLine();
		[B]if(resp=="SIM"){
			cont++;
		}
		float s;
		s=(cont/100);

		if(resp=="NÃO"){
			cont++;
		}

		float n;
		n=(cont/100);

	System.out.println("Porcentagem de SIM  " +s  + "\n Porcentagem de Não " + n);

	   saida.println(resp);[/B]
	   Thread t = new Cliente(conexao);
	   t.start();

	   String linha;
	   while (true) {
		    System.out.print("> ");
		    linha = teclado.readLine();
		    if (done) {
		    	break;
		    }
		    saida.println(linha);
	   }
  }catch (IOException e) {
   		System.out.println("IOException: " + e);
  }
 }

 public Cliente(Socket s) {
  	conexao = s;
 }

 public void run() {
  try {
	   BufferedReader entrada = new BufferedReader (new InputStreamReader(conexao.getInputStream()));
	   String linha;
	   while (true) {
		    linha = entrada.readLine();
		    if (linha == null) {
		     System.out.println("Conexão encerrada!");
		     break;
		    }
		    System.out.println();
		    System.out.println(linha);
		    System.out.print("...> ");
	   }
  } catch (IOException e) {
   		System.out.println("IOException: " + e);
  }
  done = true;
 }
}
Criado 24 de outubro de 2012
Respostas 0
Participantes 1