Witam posiadam następujący kod :
package network;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args) {
Server server = new Server();
Thread t = new Thread(server);
t.start();
try {
Klient klient = new Klient();
} catch (UnknownHostException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
class Server implements Runnable {
ServerSocket gniazdkoServera;
Socket gniazdkoKlienta = null;
public void run() {
try {
gniazdkoServera = new ServerSocket(4444);
gniazdkoKlienta = gniazdkoServera.accept();
ObjectOutputStream out = new ObjectOutputStream(gniazdkoKlienta.getOutputStream());
ObjectInputStream in = new ObjectInputStream(gniazdkoKlienta.getInputStream());
while(true){
try {
if(in.readObject()!=null)
System.out.println(in.readObject().toString());
} catch (IOException ex) {
} catch (ClassNotFoundException ex) {
}
}
} catch (IOException ex) {
System.out.println("Nie udalo sie utworzyc servera");
}
}
}
class Klient {
Socket gniazdko;
ObjectOutputStream out;
ObjectInputStream in;
Klient() throws UnknownHostException, IOException{
gniazdko = new Socket("127.0.0.1", 4444);
out = new ObjectOutputStream(gniazdko.getOutputStream());
in = new ObjectInputStream(gniazdko.getInputStream());
while(true)
{
String wiadomosc = JOptionPane.showInputDialog("Wpisz wiadomosc");
out.writeObject(wiadomosc);
out.flush();
}
}
}
Dochodzi tylko co druga wiadomość do servera, co może być przyczyną i jak to naprawić ?