Home Client server do not comunicate properly
Reply: 0

Client server do not comunicate properly

Elena Elena
1#
Elena Elena Published in 2018-01-13 02:49:44Z

I am trying to create a client/server program with Java where clients send simple string to server and each client connected to server gets this String. So I created 2 server classes for server which create new thread for each client and listen to them. Also there is a List of client sockets. I want to use it to send to each client the String which was sent by one of clients. I use foreach loop for that. The problem is that updates aren't send to all clients.

This is the server class:

@Override
public void run() {

     List<String> lista_utilizatori = new ArrayList<String>();


    try {
        ServerSocket ss=new ServerSocket(9000);

        while(true)
    {
        senderSocket=ss.accept();
        InputStream is=senderSocket.getInputStream();
        ObjectInputStream ois=new ObjectInputStream(is);
        String nume_utilizator = (String) ois.readObject();
        nume_user = new String(nume_utilizator);
        clients.add(senderSocket);

        int aux = 0;
        for(String user:lista_utilizatori){
            if (user.equalsIgnoreCase(nume_utilizator))
            {
                aux= 1;
            }
        }
        if (aux  == 0)
        {
            lista_utilizatori.add(nume_utilizator);

        }

        String number = String.valueOf(aux);
        String result = nume_user + "," + number;



        for(Socket s : clients){
        OutputStream os=s.getOutputStream();
        ObjectOutputStream oos=new ObjectOutputStream(os);
        oos.writeObject(result);
        System.out.println("Am primit de la " + s.toString());            
        }


      }

And this is client code

public class IncomingReader implements Runnable
{
     @Override
    public void run() 
    {
         try {
             s=new Socket("localhost",9000);
             OutputStream os=s.getOutputStream();
             ObjectOutputStream oos=new ObjectOutputStream(os);
             String nume_utilizator = textfield_Username.getText().trim();
             oos.writeObject(nume_utilizator);

             numele_unic = new String(nume_utilizator);
             InputStream is=s.getInputStream();
             ObjectInputStream ois=new ObjectInputStream(is);
             String getString =(String) ois.readObject();
             String[] a = getString.split(",");
             String nume = a[0];
             System.out.println("AM PRIMIT NUME: " + nume);

             int variabila_decizie = Integer.parseInt(a[1]);

             if ( variabila_decizie == 1)
             {
                 textArea_listUsers.setText(" " + nume_utilizator + " nu reprezinta un nume valid! ");
             }
             else
             {
                 StringBuilder sb = new StringBuilder();
                 listaUtilizatori.add(nume);
                 System.out.println(listaUtilizatori);

                 int i=1;
                 for(String user:listaUtilizatori){
                     System.out.println(nume_utilizator);
                     sb.append(""+ i +" . "+user+ "\n");
                     i++;
                 }
                 textArea_listUsers.setText(sb.toString());

             }} catch (IOException ex) {
             Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
         } catch (ClassNotFoundException ex) {
             Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
         }

    }}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.306689 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO