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

    Unhappy Java socket server problem (Multipleconnections/threads)

    [FIXED]

    Java Socket server with threads (multiple connections)
    Ok so I've got two classes, (main)"server" and "threader". I've got client program and it's working great (the problem is not in the client program) (also it's client is actionsctipt2.0) How it should work:
    Launching the server, launching few clients and start chatting with eachother, each client sends message to the server and server sends it to all the clients.

    So the problem is that it's working incorrectly. When I launch server and then two same clients, and start chatting, server sends client's messages only to one of the clients. (It's quite hard to explain) and if I launch only one client, server doesn't output any on this client's messages. If I launch 3 clients, only two of them receiving the messages
    Here is server class
    Java Code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    public class server 
    {
    	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
    		{
    			
    			final int PORT = 2807;
    			ServerSocket SERVER = new ServerSocket(PORT);
    			System.out.println("Server launched.");
    			int userid = 0;
    			
    			while(true)
    			{
    				Socket SOCK = SERVER.accept();
    				ConnectionArray.add(SOCK);
    				userid += 1;
    				System.out.println("User id " + userid +" connected.");
    				
    				//AddUserName(SOCK);
    				
    				threader CHAT = new threader(SOCK);
    				Thread X = new Thread(CHAT);
    				X.start();
    			}
    		}
    		catch(Exception X) {System.out.print(X);}
    	}
    }
    Here is threader class:
    Java Code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    public class threader implements Runnable {
    	Socket SOCK;
    	private Scanner INPUT;
    	private PrintWriter OUT;
    	String MESSAGE = "";
    	
    	public threader(Socket X){
    		this.SOCK = X;
    	}
    	char EOF = (char)0x00;
    	public void CheckConnection() throws IOException
    	{
    		if(!SOCK.isConnected())
    		{
    			for(int i = 1; i<server.ConnectionArray.size(); i++)
    			{
    				if(server.ConnectionArray.get(i) == SOCK)
    				{
    					server.ConnectionArray.remove(i);
    				}
    			}
    			
    			for(int i = 1; i <server.ConnectionArray.size(); i++)
    			{
    				Socket TEMP_SOCK = (Socket) server.ConnectionArray.get(i-1);
    				PrintWriter TEMP_OUT = new PrintWriter(TEMP_SOCK.getOutputStream());
    				TEMP_OUT.println(TEMP_SOCK.getLocalAddress().getHostName() + " dissconnected" + EOF);
    				TEMP_OUT.flush();
    				
    				System.out.println(TEMP_SOCK.getLocalAddress().getHostName() + " dissconnected");
    			}
    		}
    		
    	}
    	public void run(){
    		try
    		{
    			try
    			{
    				INPUT = new Scanner(SOCK.getInputStream());
    				OUT = new PrintWriter(SOCK.getOutputStream());
    				
    				while(true)
    				{
    					CheckConnection();
    					
    					if(!INPUT.hasNext())
    					{ return;}
    					
    					MESSAGE = INPUT.nextLine();
    					
    					System.out.println("User: "+ MESSAGE);
    					
    					for(int i = 1; i<server.ConnectionArray.size(); i++)
    					{
    						Socket TEMP_SOCK = (Socket) server.ConnectionArray.get(i-1);
    						PrintWriter TEMP_OUT = new PrintWriter(TEMP_SOCK.getOutputStream());
    						TEMP_OUT.println("Someone: " + MESSAGE.trim() + EOF);
    						TEMP_OUT.flush();
    					//	System.out.println("Sent to: " + TEMP_SOCK.getLocalAddress().getHostName());
    					}
    				}
    			}
    			finally
    			{
    				//SOCK.close();
    			}
    		}
    		catch(Exception X) { System.out.print(X);}
    	}
    
    }

    Would be REALLY awesome if someone gonna help me, I already was in many forums, but none helped me..
    Last edited by Lionlev; 05-28-2012 at 12:55 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,734
    Blog Entries
    7
    Rep Power
    21

    Default Re: Java socket server problem (Multipleconnections/threads)

    Check your loop boundaries; e.g. in the loop starting at line #58 your code doesn't send anything to the last socket in the ConnectionArray; the other loops have similar problems.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: Java socket server problem (Multipleconnections/threads)

    Quote Originally Posted by JosAH View Post
    Check your loop boundaries; e.g. in the loop starting at line #58 your code doesn't send anything to the last socket in the ConnectionArray; the other loops have similar problems.

    kind regards,

    Jos
    DUDE! THANKS!!!!!
    get(i-1); I removed -1, don't know why it was there..

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

    Default Re: Java socket server problem (Multipleconnections/threads)

    Quote Originally Posted by JosAH View Post
    Check your loop boundaries; e.g. in the loop starting at line #58 your code doesn't send anything to the last socket in the ConnectionArray; the other loops have similar problems.

    kind regards,

    Jos
    But now, the problem is when user dissconnects, other players get empty message "Someone: " and I don't want it to say anything
    Last edited by Lionlev; 05-27-2012 at 09:29 PM.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,734
    Blog Entries
    7
    Rep Power
    21

    Default Re: Java socket server problem (Multipleconnections/threads)

    Quote Originally Posted by Lionlev View Post
    DUDE! THANKS!!!!!
    get(i-1); I removed -1, don't know why it was there..
    And make your loops start at zero, i.e.

    Java Code:
       for(int i = 0; i<server.ConnectionArray.size(); i++) 
          ...
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Java Server Socket Problem...
    By santafan in forum Advanced Java
    Replies: 5
    Last Post: 12-30-2011, 06:13 PM
  2. Problem reading from server socket
    By glauber in forum Advanced Java
    Replies: 5
    Last Post: 02-17-2011, 01:11 PM
  3. Java Socket server with C client problem
    By rnvrnv in forum Networking
    Replies: 6
    Last Post: 11-09-2010, 01:47 AM
  4. Problem on server side (Socket Programming)
    By ersachinjain in forum Networking
    Replies: 9
    Last Post: 05-06-2010, 05:21 PM
  5. Client Server socket problem - help needed
    By kellaw in forum Threads and Synchronization
    Replies: 6
    Last Post: 10-03-2008, 07:49 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
  •