Results 1 to 4 of 4
  1. #1
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Unhappy How do I send string to all clients that connected?

    Ok I am sorry for disturbing again...
    I have no idea how do I send string to all the clients that connected to the server
    Client is made with flash (don't need do do anything here)
    All I need to do is just to send string to the clients that's all.

    So I've got 3 classes, one is main that listening for connections one is thread that does stuff to the client that connected and another thread that I want to work with

    So yeah, I am just going to put the main class's code that I got:
    Java Code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    public class Connector 
    {
    	public static ArrayList<Socket> ConnectionArray = new ArrayList<Socket>();
    	public static ArrayList<String> CurrentUsers = new ArrayList<String>();
    	public static void main(String[] args) throws IOException
    	{
    		try{//THIS IS STARTING THE CLASS THAT WE ARE WORKING ON----
    			serverMessage srMsg = new serverMessage();
    			Thread srMsgt = new Thread(srMsg);
    			srMsgt.start();
    		} catch(Exception E){ }
    		try
    		{
    			
    			final int PORT = 2807;
    			ServerSocket SERVER = new ServerSocket(PORT);
    			System.out.println("Server launched.");
    			while(true)
    			{
    				Socket SOCK = SERVER.accept();
    				ConnectionArray.add(SOCK);
    				System.out.println("New user connected.");
    				ChatServer CHAT = new ChatServer(SOCK);
    				Thread X = new Thread(CHAT);
    				X.start();
    			}
    		}
    		catch(Exception X) {System.out.print(X);}
    	}
    }
    Soooo yeah... Waiting for replies!
    Last edited by Lionlev; 06-18-2012 at 09:22 PM.
    WARNING I am Russian so it's possible that I wont understand you correctly...

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: How do I send string to all clients that connected?

    Have a list of clients. As each client connects to the server, save needed info about the client in a list.
    Loop through the list and send the String to each client.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: How do I send string to all clients that connected?

    Quote Originally Posted by Norm View Post
    Have a list of clients. As each client connects to the server, save needed info about the client in a list.
    Loop through the list and send the String to each client.
    Thanks, I quite managed to figure it out, I forgot that I got
    public static ArrayList<Socket> ConnectionArray = new ArrayList<Socket>();
    array that holds it.
    One thing I don't know how to check if someone disconnects..
    WARNING I am Russian so it's possible that I wont understand you correctly...

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: How do I send string to all clients that connected?

    how to check if someone disconnects..
    Try doing some testing/debugging by having a couple of clients and have one disconnect. Print out all the values of the connections for the two clients and see what is different.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Send a string from serial port via bluetooth
    By spacer in forum Networking
    Replies: 3
    Last Post: 12-10-2009, 08:00 AM
  2. string wont send to JList
    By Ekul in forum New To Java
    Replies: 2
    Last Post: 11-12-2009, 07:02 PM
  3. How to send folder through a connected socket
    By Chris555 in forum Networking
    Replies: 0
    Last Post: 07-18-2009, 11:41 AM
  4. Using JSP, how can I call send sms from connected mobile
    By java student in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 12-10-2008, 06:11 AM
  5. Replies: 0
    Last Post: 06-11-2008, 11:10 AM

Posting Permissions

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