Ajuda em Socket

3 respostas
Saulo.Java

Pessoal estou criando um chat simples socket , mas não consigo retorna a mensagens enviada pelo cliente.

public class ClientTest extends JFrame {
	Container panel = getContentPane();
	final JTextField send   = new JTextField(40);
	JButton	   ok   = new JButton("Enviar");
	final JTextArea  area = new JTextArea(15,50);
	DataInputStream reader;
	DataOutputStream write;
	Socket socket;
	
	
	public  ClientTest() throws IOException{
				
		area.enable(false);
		panel.setLayout(new FlowLayout());
		panel.add(area);
		panel.add(send);
		panel.add(ok);
		
		socket = new Socket("localhost",4444);
		reader = new DataInputStream(socket.getInputStream());
		write =new DataOutputStream(socket.getOutputStream());
		
		ok.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				new Thread(){

					public void run() {
							try {
								String s1 = send.getText();
								
								DataOutputStream out = new DataOutputStream(socket.getOutputStream());
								
								out.writeChars(s1);
								out.flush();
								
								send.setText("");
								send.requestFocus();
							} catch (IOException e) {
								e.printStackTrace();
							}
							
					}
				}.start();
			}});
		
		
		 
		
	}
	
	public static void main(String[] args) throws IOException {
		frame.setSize(580, 320);
		frame.setVisible(true);
		
		frame.addWindowListener( new WindowAdapter(){
			public void windowClosing(WindowEvent arg0) {
				System.exit(0);
			}
		});
	}
	
	
	public class OutReader implements Runnable {

		public void run() {
			String msn;
			try {
				while((msn = String.valueOf(reader.readChar()))!= null){
					area.append(msn+"\n");
				}
			} catch (IOException e) {
				e.printStackTrace();
			}				

		}
	}


}
public class ServerTest {
	ArrayList clients;

	public class ClienteHandler implements Runnable {
		DataInputStream reader;
		Socket socket;
		
		public ClienteHandler(Socket clientSocket) throws IOException {
			DataInputStream isReader = new DataInputStream(clientSocket
					.getInputStream());
			reader = new DataInputStream(isReader);
			System.out.print(reader.readChar()+"\n");
			
		}

		public void run() {
			String message;
			try {
				while ((message = String.valueOf(reader.readChar())) != null) {
					System.out.println("READ " + message);
					tellEveryone(message);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) throws IOException {
		new ServerTest().go();
	}

	public void go() {
		try {
			ArrayList clients = new ArrayList();

			ServerSocket serverSocket = new ServerSocket(4444);

			System.out.println("Server Running Port on 4444....");

			while (true) {
				Socket socket = serverSocket.accept();
				DataOutputStream write = new DataOutputStream(socket
						.getOutputStream());
				clients.add(write);
				// new ServerMessenger(socket).Messenger();
				Thread t = new Thread(new ClienteHandler(socket));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void tellEveryone(String message) throws IOException {
		Iterator it = clients.iterator();

		while (true) {
			DataOutputStream write = (DataOutputStream) it.next();
			write.flush();
		}

	}

}

3 Respostas

G

Mostra a classe ClienteHandler ai pra nóis…

Saulo.Java
public class ClienteHandler implements Runnable {
		DataInputStream reader;
		Socket socket;
		
		public ClienteHandler(Socket clientSocket) throws IOException {
			DataInputStream isReader = new DataInputStream(clientSocket
					.getInputStream());
			reader = new DataInputStream(isReader);
			System.out.print(reader.readChar()+"\n");
			
		}

		public void run() {
			String message;
			try {
				while ((message = String.valueOf(reader.readChar())) != null) {
					System.out.println("READ " + message);
					tellEveryone(message);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
G

Tu não tem que dar um “start” nisso aqui:

Thread t = new Thread(new ClienteHandler(socket));

da um t.start() pra ver se rola.

Criado 18 de julho de 2009
Ultima resposta 20 de jul. de 2009
Respostas 3
Participantes 2