Gente, estou modificando um tutorial de chat que achei na net :
http://forum.codecall.net/topic/67704-server-and-client-chat-tutorial/
--
Eu estou fazendo uma lista de usuarios pro servidor mandar o que um usuario escreve pra todos os outros!
Porém , nao esta dando certo.
O servidor recebe as mensagens mas nao envia para os usuários.
Podem me ajudar por favor? Estou sem idéias;
Essa aqui é minha única classe no Cliente:/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Control;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
import javax.swing.JOptionPane;
/**
*
* @author André
*/
public class Main {
private static int PORT = 65530;
private static String HOST = "localhost";
Socket socket;
public static void main(String args[]) {
Main m = new Main();
m.HOST = JOptionPane.showInputDialog("Insert IP of Server Chat : ");
m.EnviandoMensagem(JOptionPane.showInputDialog("Say Something : "));
}
public void EnviandoMensagem(String message) {
try {
Socket socket = new Socket(HOST, PORT);
socket.setKeepAlive(true);
PrintWriter out = new PrintWriter(socket.getOutputStream());
out.println("" + message);
out.flush();
while (true) {
Scanner in = new Scanner(socket.getInputStream());
String input = in.nextLine();
if(input != null) {
JOptionPane.showMessageDialog(null,input);
System.out.println("Msg Received : " + input);
}
}
} catch (Exception e) {
System.out.println(e.fillInStackTrace());
}
}
}
// ABAIXO ESTAO AS CLASSES DO PROJETO SERVIDOR
Minha Classes Dos Servidores estão assim :
package serverchat;
import java.net.Socket;
public class Usuarios {
private String nomeDeUsuario;
private Socket socket;
public Usuarios(String usuario, Socket s) {
nomeDeUsuario = usuario;
socket = s;
}
public String getNomeDeUsuario() {
return nomeDeUsuario;
}
public void setNomeDeUsuario(String nomeDeUsuario) {
this.nomeDeUsuario = nomeDeUsuario;
}
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
}
Classe Client Do Servidor
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package serverchat;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author André
*/
public class Client implements Runnable {
private Socket socket;
public ArrayList<Usuarios> listaUsuarios = new ArrayList<>();
public Client(Socket s)
{
socket = s;
Usuarios usuario = new Usuarios("" + socket.getLocalSocketAddress(),socket);
listaUsuarios.add(usuario);
}
@Override
public void run() {
try{
Scanner in = new Scanner(socket.getInputStream());
PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
while(true)
{
if(in.hasNext())
{
String input = in.nextLine();
System.out.println(socket.getLocalSocketAddress() + " said : " + input);
for(int i = 0;i< listaUsuarios.size();i++)
{
Socket aux = listaUsuarios.get(i).getSocket();
PrintWriter pwAux = new PrintWriter(aux.getOutputStream());
pwAux.println(listaUsuarios.get(i).getNomeDeUsuario() + " said: " + input);
pwAux.flush();
}
//printWriter.println(socket.getLocalSocketAddress() + "said :" + input);
//printWriter.flush();
}
}
}catch(Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
E a main do Servidor :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ServerSide;
import java.net.ServerSocket;
import java.net.Socket;
import serverchat.Client;
/**
*
* @author André
*/
public class Main {
static int PORT = 65530;
public static void main(String args[]) {
try {
ServerSocket server = new ServerSocket(PORT);
System.out.println("Server Opened...");
while (true)
{
Socket s = server.accept();
System.out.println("Client Connected : " + s.getLocalAddress().getHostName());
Client chat = new Client(s);
Thread t = new Thread(chat);
t.start();
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
Eu sei que são bastantes classes, mas estou sem idéia do que pode ser...
Podem me dar uma ajuda?