Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By Gatsu

Thread: TCP socket - chat server

  1. #1
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default TCP socket - chat server

    Hi, so im attempting to make a chat server that stores incoming connections in an ArrayList so that we can send chat messages to all those clients when receiving a string.

    Here in this server I just add the connecting sockets to a list and loops trough them for every incoming message so that all clients can recieve it. Would this work well?

    Java Code:
        /**
         * handle i/o from the clients
         */
        private boolean handleClientRequest(Socket clientHandlerSocket) throws IOException
        {
            userList(clientHandlerSocket);
            
            for(int i = 0 ; i < mConnected.size() ; i++)
            {
                currentSocket = mConnected.get(i);
                
                DataOutputStream out = new DataOutputStream(currentSocket.getOutputStream()); //output stream
                BufferedReader in = new BufferedReader(new InputStreamReader(currentSocket.getInputStream())); //input stream
                
                mInText = in.readLine();
                
                if(mInText.startsWith("QUIT"))
                {
                    mRun = false;
                }
                else
                {
                    if(currentSocket.isConnected() == true) //check if socket is still connected
                    {
                        out.writeBytes(mInText);
                    }
                    else //remove a socket from the ArrayList of connected sockets
                    {
                        System.out.println("Removed disconnected socket: " + mConnected.get(i));
                        mConnected.remove(i);
                        i -= 1;
                    }
                }
                
                //close streams
                out.flush();
                out.close();
                in.close();
            }
            
            return mRun;
        }
        
        /**
         * list all connected users
         */
        private void userList(Socket clientHandlerSocket)
        {
            if(!mConnected.contains(clientHandlerSocket))
            {
                mConnected.add(clientHandlerSocket);
                System.out.println("socket added: " + clientHandlerSocket);
            }
        }
    Last edited by Gatsu; 03-01-2013 at 04:38 AM.
    daovanhiep.humg likes this.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default Re: TCP socket - chat server

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Help Needed! Android chat - pc chat server
    By thornt in forum Android
    Replies: 3
    Last Post: 04-13-2012, 12:50 PM
  2. Chat Server
    By mrbrendano in forum New To Java
    Replies: 1
    Last Post: 09-19-2011, 11:08 PM
  3. TCP Socket Chat-Receive msg on JTextArea
    By sQu3aKy in forum Networking
    Replies: 1
    Last Post: 03-05-2011, 02:30 PM
  4. Simple Chat Server socket coding issues
    By TheGadgetCat in forum Networking
    Replies: 14
    Last Post: 12-17-2010, 12:54 PM
  5. Using Socket Programming in RMI to create chat application
    By dhaval.rajput.p in forum New To Java
    Replies: 0
    Last Post: 07-14-2009, 06:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •