Hello.
I really hope someone can help me solve this terrible problem I'm having. In advance, thank you for your time and patience.
I have a server that needs to be listening to different clients. Once it gets a message from any client, it needs to forward that information to all of them. My problem is that it only forwards the message to the lastest client, that is the one that started a connection after all the others. For the others, I get a "java.io.StreamCorruptedException: invalid type code: AC" exception, instead of the message that they're supposed to receive.
I have no idea how to solve it, and would appreciate it a lot if someone could help me as soon as possible, since it is due tomorrow. Thanks again.
I ommit the GUI-related code, except, of course, the ActionPerformed's.
Server code:
import java.io.* ;
import java.net.* ;
import java.util.* ;
public class WebServer implements Runnable {
Socket s;
Socket entranteCopia[] = new Socket[10];
ObjectOutputStream out[] = null;
static int conexiones = 0;
String mensaje = "";
public WebServer(Socket s, Socket entranteCopia[]){
this.entranteCopia = entranteCopia;
this.s=s;
}
public static void main (String args[]) throws Exception{
int a = 0;
ServerSocket serverSocket = new ServerSocket (9091);
Socket entrante[] = new Socket[10];
while(true){
entrante[a] = serverSocket.accept();
System.out.println("Conexion establecida con " + entrante[a].getInetAddress().getHostName());
Thread thread = new Thread(new WebServer(entrante[a],entrante));
conexiones++;
thread.start();
a++;
}
}
public void run () {
try {
out = new ObjectOutputStream[conexiones];
ObjectInputStream in = new ObjectInputStream(s.getInputStream());
//BufferedReader in = new BufferedReader ( new InputStreamReader(s.getInputStream()) );
//DataOutputStream datos[] = new DataOutputStream[conexiones];
for(int h=0;h<conexiones;h++){
out[h] = new ObjectOutputStream(entranteCopia[h].getOutputStream());
out[h].flush();
}
while(true){
try{
mensaje = (String)in.readObject();
System.out.println(mensaje);
// se envia el mensaje a todos los chats:
for(int h=0;h<out.length;h++){
try{
out[h].writeObject(mensaje);
out[h].flush();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
catch(ClassNotFoundException classnot){
}
}
} catch (IOException io){
}
}
}
Client code:
public class chat extends javax.swing.JFrame implements Runnable {
Socket s = null;
ObjectOutputStream out = null;
ObjectInputStream in = null;
String nickname = "";
public void run(){
String mensaje = "";
while(true){
if(in != null){
try {
mensaje = (String)in.readObject();
System.out.println(mensaje);
jTextArea1.append(mensaje + "\n");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
/** Creates new form chat */
public chat() {
initComponents();
Thread t = new Thread(this);
t.start();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String message = nickname + " dice: " + jTextField3.getText();
try{
out.writeObject(message);
out.flush();
}
catch(IOException ioException){
ioException.printStackTrace();
}
jTextField3.setText("");
jTextField3.requestFocus();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
nickname = jTextField4.getText();
String host = "";
int puerto = 0;
host = jTextField1.getText();
puerto = Integer.parseInt(jTextField2.getText());
try {
s = new Socket(host, puerto);
out = new ObjectOutputStream(s.getOutputStream());
out.flush();
in = new ObjectInputStream(s.getInputStream());
} catch (UnknownHostException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
jTextArea1.append("Conexion establecida.\n");
jButton2.setEnabled(true);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new chat().setVisible(true);
}
});
}