id flow



  • Good night. Java's doing a month.

    I'm writing a client, a server app. Looks like a normal chat. Each new compound is designated id♪ Value recorded ArrayList♪ Every new client opens in a separate flow.

    I can't understand. idto send a message to a selected client?

    How to trigger a method writeUTFrelated to this id?

    public class myLogic implements Runnable {
        Socket socket = null;
        public myLogic(Socket s){
            socket = s;
        }
    
    @Override
    public void run() {
        try{
        DataOutputStream dout = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
        DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while(true){
            String line = br.readLine();
            dout.writeUTF(line);
            String line1 = dis.readUTF();
            dout.flush();
            dis.close();
        }
        }catch(Exception e){}
    
    }
    

    }

    public class SelectorTest {
    public static void main(String args[]) throws IOException{
    int port = 9007;
    ServerSocket ss = new ServerSocket(port);
    while(true){
    int id = 0;
    Socket s = ss.accept();
    ArrayList AL = new ArrayList();
    AL.add(s);
    System.out.println(AL.toString());
    System.out.println(AL.containsAll(AL));
    AL.set(id, s);
    Runnable r = new myLogic(s);
    Thread thread = new Thread(r);
    thread.start();
    }
    }
    };



  • Use it. Map

    public class SelectorTest {
        public static void main(String args[]) throws IOException{
            int port = 9007;
            ServerSocket ss = new ServerSocket(port);
    
        Map<Integer, Socket> AL = new HashMap<>(); // тут будим хранит сокеты
        Integer id = 0;
    
        while(true){
            id++;
            Socket s = ss.accept();
            AL.put(id,s);
            ...
        }
    }
    

    };

    for receipt

    AL.get(id);




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2