Correct method to start threads and socket programming

Discussion in 'Java' started by ClintWilliam, Nov 30, 2015.

  1. ClintWilliam

    ClintWilliam

    Joined:
    Nov 30, 2015
    Messages:
    2
    Likes Received:
    0
    Hi. My name is Clint this is my first time here, anyway I have a chat app can you simply my code for me because I'm not sure what will happen if a million clients connect.
    <server side> My main class listen for incoming socket connection requests from socket clients, then I assign each socket object to a thread and construct the thread with the socket object because later on I want the clients to send and receive data to each other.

    the main listens and accept connection...
    Code (Text):
    try{
                serverSocket=new ServerSocket(serverPort);
                exe = Executors.newFixedThreadPool(NTHREADS);
                while(true){
                    try{
                        clientSocket=serverSocket.accept();
                        int si;
                        for(si=0;si<NTHREADS;si++){
                            if(client[si]==null){
                                client[si]=new ClientThread(clientSocket,client,gui,si,server,methods);
                                exe.execute(client[si]);
                                break;
                            }
                        }
                        if(si==NTHREADS){
                            PrintStream os = new PrintStream(clientSocket.getOutputStream());
                            os.println("99");
                            os.close();
                            clientSocket.close();
                        }
                    } catch(IOException ioe){
                        gui.textArea1.append("\nError accepting connection:"+ioe.getMessage()+"\n");
                    }
                }
            }catch(IOException e){
                gui.textArea1.append("\nError starting Server on port "+serverPort+"\n");
                gui.textArea1.append(e.getMessage()+"\n");
            }
    What I would really want this code to do is accept up to an infinite number of socket clients ==> assign each to a thread and I want to be able to call any socket client's output or inpustream to communicate. I pray my question is clear enough to answer. Thank you in advance. Ps. How would you do that, what you think I'm trying to do...
     
    ClintWilliam, Nov 30, 2015
    #1
    1. Advertisements

  2. ClintWilliam

    ClintWilliam

    Joined:
    Nov 30, 2015
    Messages:
    2
    Likes Received:
    0
    If my query is unclear allow me to simplify...I want to know if my executor code looks right and how can I keep track, like a list array, of all threads. It's a client socket connecting and I want to be able to call the output / input stream of thread-X from thread-Y and write / read over it, respectively. Later on I am going to show you the client thread and ask you more, for now just that. Please tell me if I'm unclear or are in the wrong forum.
    I
     
    ClintWilliam, Dec 3, 2015
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.