简体   繁体   中英

ArrayList of Socket doesn't work to send message to Client - Java

I have a Server-Client program where using JLabel ,I'm trying communicate with a particular Client. When I accept any client I add their socket to a ArrayList<Socket> ,then I assign socket = socketList.get(1); to my first JLabel so that the socket will contain the first Client connected to the server. But there is no communication happening. I'm not able to identify the problem.

//server

 private void jLabel2MouseClicked(java.awt.event.MouseEvent evt) {                                     
            PrintWriter out;

            try {
                 socket = socketList.get(2);

            out = new PrintWriter(socket.getOutputStream(), true);
            out.println("pc2");
            } catch (IOException ex) {
            Logger.getLogger(third_frame.class.getName()).log(Level.SEVERE, null, ex);
            }
       }                                    

 public void postListen()
{
    new Thread(new Runnable()
    {
        public void run()
        {
            connect_clients();
        }

    }).start();
  }

  //checking clients connected
    void connect_clients()
    {
        try {
            ServerSocket listener = new ServerSocket(7700);
            jButton1.setText("Server Running!");
            jButton1.setEnabled(false);

            try {
                while (true) {

                    //socket = listener.accept();
                    socketList.add(listener.accept());
                    try {

                            clientIP = socket.getLocalAddress().getHostName();


                    }    
                    finally
                            {

                            }
                }
            }

               finally
                            {

                            }

            }
        catch(IOException ex)
        {
        }
    }

//Client

void connect_server()
    {
        try {
            // TODO code application logic here
            String serverAddress = JOptionPane.showInputDialog(
                    "Enter IP Address of a machine that is\n" +
                            "running the date service on port 9090:");
            s = new Socket(serverAddress, 7700);

            while(true){
            BufferedReader input =
            new BufferedReader(new InputStreamReader(s.getInputStream()));
            String answer = input.readLine();

            System.out.println(answer);
            }

            }


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

Seems to me like you are not initializing your socket and then try to call it, but its hard to know whats wrong without all code and errors.

//socket = listener.accept();
socketList.add(listener.accept());
try {
    clientIP = socket.getLocalAddress().getHostName();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM