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 7 of 7

Thread: Java socket server problem

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Java socket server problem

    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
    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:
    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


  2. #2
    Member
    Join Date
    Apr 2012
    Location
    Superior, CO, USA
    Posts
    80
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default Re: Java socket server problem

    Quote Originally Posted by Lionlev View Post
    Java Socket server with threads (multiple connections)

    for(int i = 1; i<server.ConnectionArray.size(); i++)


    Would be REALLY awesome if someone gonna help me
    I'm not positive that this is the problem but is Java zero based or one based when it comes to counting? What is the index of the first element in the ArrayList? I see that you're subtracting one sometimes but you're making your life much harder than it needs to be.
    Need Java help? Check out the HotJoe Java Help forums!

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java socket server problem

    Quote Originally Posted by stdunbar View Post
    I'm not positive that this is the problem but is Java zero based or one based when it comes to counting? What is the index of the first element in the ArrayList? I see that you're subtracting one sometimes but you're making your life much harder than it needs to be.
    The code is written in eclipse
    And for the truth, the problem is that I am actionscript 2 user and then I came to that I need a server, I choose java. And this is not fully my code, I followed the tutorial. if you want I can send you the link to the video

  4. #4
    Member
    Join Date
    Apr 2012
    Location
    Superior, CO, USA
    Posts
    80
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default Re: Java socket server problem

    Quote Originally Posted by Lionlev View Post
    The code is written in eclipse
    And for the truth, the problem is that I am actionscript 2 user and then I came to that I need a server, I choose java. And this is not fully my code, I followed the tutorial. if you want I can send you the link to the video
    Well, the code is written in Java. Eclipse is development tool that can help you program in Java. Java is zero based - don't start your loops at one.

    Start with that and see what breaks next
    Need Java help? Check out the HotJoe Java Help forums!

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java socket server problem

    Quote Originally Posted by stdunbar View Post
    Well, the code is written in Java. Eclipse is development tool that can help you program in Java. Java is zero based - don't start your loops at one.

    Start with that and see what breaks next
    Ok, I've fixed the loops but didn't change the problem as I expected, but you was right, 2 same loops were looking stupid xD

  6. #6
    Junior Member
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java socket server problem

    Quote Originally Posted by stdunbar View Post
    Well, the code is written in Java. Eclipse is development tool that can help you program in Java. Java is zero based - don't start your loops at one.

    Start with that and see what breaks next
    Sorry for my English tho.. I am Russian..

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,301
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Java socket server problem

    Quote Originally Posted by Lionlev View Post
    Why wont you start a new topic?
    That post has been moved to its own thread
    Re: Java socket server problem

Similar Threads

  1. Sending zip from server socket
    By moon_werewolf in forum Java Theory & Questions
    Replies: 0
    Last Post: May 30th, 2011, 07:51 AM
  2. Replies: 0
    Last Post: February 24th, 2011, 05:31 AM
  3. Problem reading from server socket
    By glauber in forum Java Networking
    Replies: 0
    Last Post: February 15th, 2011, 11:54 AM
  4. client/server socket
    By Java_dude_101 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: January 18th, 2011, 12:16 AM