This discussion is archived
1 Reply Latest reply: Oct 14, 2013 8:47 AM by jtahlborn RSS

Different data in server and client

987579 Newbie
Currently Being Moderated

I have a mini server program, which accepts clients, reads their nicknames and then periodically sends them for everyone else. That way, user, connected to the server should get all the nicknames of other already connected users. But I have one problem, when first user connects, everything works fine. But when the second user connects, then the first user gets wrong data. I will show you what I mean. Here is server:

public class Server {
  public ServerSocket server;
  public Socket connection;
  public ArrayList<Connected> players = new ArrayList();
  public boolean keepGoing = true;
  public boolean gameStarted;
  public static void main(String[] args) {
       new Server();
  }
  public Server(){
       gameStarted = false;
       // Creates server
       try {
            server = new ServerSocket(4447);     
       } catch (IOException e) {
            e.printStackTrace();     
       }
       //Getting players, adding them to the server's list.
       while(keepGoing){
            try {     
                 connection = server.accept();     
            } catch (IOException e) {     
                 e.printStackTrace();
           }
       Connected c = new Connected(connection);
       players.add(c);
       Thread t = new Thread(c);
       t.start();
       }
       //Server is shutting down
       try {
            connection.close();     
       } catch (IOException e) {
            e.printStackTrace();
       }
  }
  private class Connected implements Runnable{
  public Socket socket;
  public DataInputStream dis;
  public DataOutputStream dos;
  public String nickname;
  public boolean exists;
  public Connected(Socket s){
       socket = s;
       exists = true;
       try {
            dis = new DataInputStream(s.getInputStream());     
            dos = new DataOutputStream(s.getOutputStream());
            nickname = dis.readUTF();
       } catch (IOException e) {
            e.printStackTrace();
       }
  }
  @Override
  public void run() {
       while(exists){
            send();
       }
}
  public void send(){
       try {
            dos.writeInt(players.size());     
            for (Connected c : players){
                 dos.writeUTF(c.nickname);
            }
       } catch (IOException e) {
            e.printStackTrace();
       }
  }
}
}

 

As you can see, it is just simply waits for connection, then adds connected user to the arrayList of all connected users. Creates thread, reads his nickname and then all the time sends it to the other users. The problem is, that when second user connects, players.size() is 2. But (I don't know why) user 1 (first connected) still gets 1 instead of 2. That way it doesn't see any new connections. I also noticed that the same is with second, when third connects.

 

Here is client code:

public class RoomLobby extends JPanel{
  public String nickname;
  public Socket s;
  public DataInputStream dis;
  public DataOutputStream dos;
  public JList list;
  public JScrollPane jsp;
  public Timer timer;
  public DefaultListModel listModel;
  public RoomLobby(String n){
       if(connect()){
            nickname = n;
            try {
                 dos.writeUTF(nickname);     
            } catch (IOException e) {
                 e.printStackTrace();     
            }
            createEnv();     
       }
  timer = new Timer(15,new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent arg0) {
       send();
       get();
       }
  });
       timer.start();
  }
  public void send(){
  }
  public void get(){
       try {
            int count = dis.readInt();
            for(int i=0; i<count; i++){     
                 String n = dis.readUTF();          
                 if(!listModel.contains(n)){
                      listModel.addElement(n);
                 }
            }
       } catch (IOException e) {
           // TODO Auto-generated catch block
            e.printStackTrace();
       }
  }
  public void createEnv(){
       listModel = new DefaultListModel<String>();
       list = new JList<String>(listModel);
       jsp = new JScrollPane(list);
       jsp.setPreferredSize(new Dimension(200,300));
       add(jsp);
       listModel.addElement(nickname);
  }
  public boolean connect(){
       try {
            s = new Socket("localhost",4447);
            dis=new DataInputStream(s.getInputStream());
            dos=new DataOutputStream(s.getOutputStream());
            return true;
       } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
       } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
       }
  }
}

 

Can somebody explain where is the problem? I am trying to fix this since yesterday, and I gave up. Tried almost everything.

  • 1. Re: Different data in server and client
    jtahlborn Expert
    Currently Being Moderated

    your server is not waiting before it sends more info to the client, so it is just spewing the same information over and over as fast as it can go.  you should implement a 2 step process where the client sends a message to the server asking for updated info and then reads the reply.

     

    also, you have a threading issue on the server.  your players list needs to be thread-safe since you are accessing it from multiple threads.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points