Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: ServerSocket send data to all Sockets

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default ServerSocket send data to all Sockets

    I'm working on my first maultiplayer game in java. But I have a problem. Only the latest connected socket get any data, and I have no idea why. I tried to make a loop that would send the collected data to all the players but only the newest one gets it.

    Here is my code
    public void run()
       {
          while(true)
          {
             String data = "";
             for(Player player : Player.getPlayers())
             {
                player.move();
                data += player.getData();
             }
     
             data += "done"+in.newline;
     
             for(Player player : Player.getPlayers())
             {
                Socket socket = player.getSocket();
     
                if(!socket.isClosed())
                {
                   try
                   {
                      OutputStream out = socket.getOutputStream();
                      out.write(data.getBytes());
                      out.flush();
                   } catch (IOException e) {
                      e.printStackTrace();
                   }
                }
             }
     
             try{Thread.sleep(10);}catch(Exception e){}
          }
       }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: ServerSocket send data to all Sockets

    only the newest one gets it.
    sounds like you need to save the connections to all the clients that have connected in a collection of some kind so you can use that connection to send something to each of them.

    It's hard to say what the problem is without seeing all the code. What is in Player? What does getPlayers() return? Print it out to see for sure.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: ServerSocket send data to all Sockets

    Quote Originally Posted by Norm View Post
    sounds like you need to save the connections to all the clients that have connected in a collection of some kind so you can use that connection to send something to each of them.

    It's hard to say what the problem is without seeing all the code. What is in Player? What does getPlayers() return? Print it out to see for sure.
    This is what i have in the main server class.
    	public void run()
    	{
    		while(true)
    		{
    			Socket join = null;
     
    			try 
    			{
    				join = serverSocket.accept();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
     
    			if(join != null)
    			{
    				System.out.println(join.toString());
    				Player.getPlayers().add(new Player(join));
    			}
    		}
    	}

    The Player class extends Entity which is just this
    public class Entity
    {
    	int x, y;
    	public void setX(int i){ x = i; }
    	public void setY(int i){ y = i; }
    	public int getX(){ return x; }
    	public int getY(){ return y; }
    }

    And this is Player class
    public class Player extends Entity implements Runnable 
    {
    	private static Socket socket;
    	private static List<Player> players  = new ArrayList<Player>();
    	private Thread thread;
     
    	private int move = 0, steps;
     
    	public Player(Socket s)
    	{		
    		setX(64);
    		setY(64);
    		socket = s;
     
    		thread = new Thread(this);
    		thread.start();
    	}
     
    	public Socket getSocket()
    	{
    		return socket;
    	}
     
    	public static List<Player> getPlayers()
    	{
    		return players;
    	}
     
    	public String getData()
    	{
    		return getX()+":"+getY()+in.newline;
    	}
     
    	public void move(int dir)
    	{
    		if(move == 0)
    		{
    			move = dir;
    			steps = 32;
    		}
    	}
     
    	public void move()
    	{
    		if(move == 1){ setY(getY()+1); }
    		if(move == 2){ setY(getY()-1); }
    		if(move == 3){ setX(getX()-1); }
    		if(move == 4){ setX(getX()+1); }
     
    		if(steps != 0)
    		{
    			steps--;
    		}
     
    		if(steps == 0 && move != 0)
    		{
    			move=0;
    		}
    	}
     
    	public void run()
    	{
    		while(getSocket().isConnected() && !getSocket().isClosed())
    		{
    			Scanner scan = null;
     
    			try 
    			{
    				scan = new Scanner(getSocket().getInputStream());
    				while(scan.hasNextLine())
    				{
    					String line = scan.nextLine();
    					String[] raw = line.split("\\:");
    					move(Integer.parseInt(raw[0]));
    				}
     
    			} catch (IOException e) {
    				e.printStackTrace();
    			}finally{
    				if(scan != null)
    				{
    					scan.close();
    				}
    			}
    		}
    	}
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: ServerSocket send data to all Sockets

    Have you tried debugging the code by adding printlns to show the values of variables when they are changed and used. For example, players. When is a Player object added to it? How many Player objects are in it before the for loop?
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    moon_werewolf (September 10th, 2012)

  6. #5
    Junior Member
    Join Date
    May 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: ServerSocket send data to all Sockets

    Quote Originally Posted by Norm View Post
    Have you tried debugging the code by adding printlns to show the values of variables when they are changed and used. For example, players. When is a Player object added to it? How many Player objects are in it before the for loop?
    Thanks. It helped me locate the problem. In the Player.java I had this code private static Socket socket; which sould have been this private Socket socket;

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: ServerSocket send data to all Sockets

    Yes, static will do that. Each Player would be sharing the one socket. Wouldn't that show up in the testing if the last client got several copies of the same thing (one copy for each client)?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Multiple clients and one server. How to send data from one client to the others
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 1
    Last Post: March 21st, 2012, 07:29 PM
  2. How do embedded java applets send and recv data?
    By MRnobody in forum Java Networking
    Replies: 1
    Last Post: March 1st, 2012, 05:36 AM
  3. Replies: 1
    Last Post: August 12th, 2011, 10:09 AM
  4. Send and Receive data from socket simultaneously
    By bigmac025 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: March 25th, 2011, 05:18 PM
  5. Problem on sending vectors from Java server to C# client
    By MS_Dark in forum Java Networking
    Replies: 2
    Last Post: July 7th, 2009, 02:35 PM