Page 1 of 2 12 LastLast
Results 1 to 20 of 36
Like Tree1Likes

Thread: Another socket (multiclient) problem....

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

    Question Another socket (multiclient) problem....

    Hi!
    So basically I've go two classes "Connector" and "ChatServer" it's a socket server with threads. So the problem is when client sends message (loging info, <<username + " " + userpassword>>) the server doesn't get that line until client sends it again but server gets it wrong:
    Java Code:
    Username: username
    User Password: userpassword username
    I don't know how to fix that...
    Help?

    Connector:
    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
    		{
    			
    			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);
    				
    				ChatServer CHAT = new ChatServer(SOCK);
    				Thread X = new Thread(CHAT);
    				X.start();
    			}
    		}
    		catch(Exception X) {System.out.print(X);}
    	}
    }
    ChatServer:
    Java Code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    public class ChatServer implements Runnable {
    	Socket SOCK;
    	private Scanner INPUT;
    	private PrintWriter OUT;
    	String MESSAGE = "";
    	
    	public ChatServer(Socket X){
    		this.SOCK = X;
    	}
    	char EOF = (char)0x00;
    	public void CheckConnection() throws IOException
    	{
    		if(!SOCK.isConnected())
    		{
    			for(int i = 0; i<Connector.ConnectionArray.size(); i++)
    			{
    				if(Connector.ConnectionArray.get(i) == SOCK)
    				{
    					Connector.ConnectionArray.remove(i);
    				}
    			}
    			
    			for(int i = 0; i <Connector.ConnectionArray.size(); i++)
    			{
    				Socket TEMP_SOCK = (Socket) Connector.ConnectionArray.get(i);
    				System.out.println(TEMP_SOCK.getLocalAddress().getHostName() + " dissconnected");
    			}
    		}
    		
    	}
    	public void run(){
    		try
    		{
    			try
    			{
    				INPUT = new Scanner(SOCK.getInputStream());
    				OUT = new PrintWriter(SOCK.getOutputStream());
    				boolean gotusername = false;
    				boolean gotpassword = false;
    				boolean goteverything = false;
    				while(true)
    				{
    					CheckConnection();
    					
    					if(!INPUT.hasNext())
    					{ return;}
    					
    					
    					if(goteverything == false)
    					{
    						String username = INPUT.next();
    						String userpassword = INPUT.next();
    						System.out.println("User's username: " + username +"\n" + "User's password: " + userpassword);
    						goteverything = true;
    					}
    					/*for(int i = 0; i<Connector.ConnectionArray.size(); i++)
    					{
    						//Socket TEMP_SOCK = (Socket) Connector.ConnectionArray.get(i);
    						//PrintWriter TEMP_OUT = new PrintWriter(TEMP_SOCK.getOutputStream());
    						//TEMP_OUT.println("Someone: " + MESSAGE + EOF);
    						//TEMP_OUT.flush();
    					//	System.out.println("Sent to: " + TEMP_SOCK.getLocalAddress().getHostName());
    					}*/
    				}
    			}
    			finally
    			{
    				//SOCK.close();
    			}
    		}
    		catch(Exception X) { System.out.print(X);}
    	}
    
    }
    And client is based on actionscript2 but I don't think that the problem is in the client.

    Hope you will help!
    Last edited by Lionlev; 06-02-2012 at 05:42 PM. Reason: Forgot to mention something

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Another socket (multiclient) problem....

    Your server and client should be in their own true OOP classes, meaning the class is more than a large static main method, but rather each have constructors, non-static variables and non-static methods. Also, please read on Java naming conventions and abide by them, as it is very hard for us to read and understand your code until you do this. Also, have you gone through the sockets tutorials yet because your code posted above suggests that you haven't. Please do this first as it will help you immeasurably.

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

    Default Re: Another socket (multiclient) problem....

    Quote Originally Posted by Fubarable View Post
    Your server and client should be in their own true OOP classes, meaning the class is more than a large static main method, but rather each have constructors, non-static variables and non-static methods. Also, please read on Java naming conventions and abide by them, as it is very hard for us to read and understand your code until you do this. Also, have you gone through the sockets tutorials yet because your code posted above suggests that you haven't. Please do this first as it will help you immeasurably.
    I wouldn't posted this if I didn't read and understood my code.. I was sitting few days trying to figure it out how fix the problem.. I am stuck.. So asking people's help. Got nothing else to do.

    And also my client project is based on actionscript 2. Not on java

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Another socket (multiclient) problem....

    Quote Originally Posted by Lionlev View Post
    I wouldn't posted this if I didn't read and understood my code..
    You misunderstand me... by using non-standard naming conventions, you're making it difficult for us to understand your code. If we're going to be able to help you, we need to know what's going on.

    I was sitting few days trying to figure it out how fix the problem.. I am stuck.. So asking people's help. Got nothing else to do.
    And that's all well and good, and I hope I'm able to help you.

    And also my client project is based on actionscript 2. Not on java
    Ah, an important detail that should probably be in your original post.

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

    Default Re: Another socket (multiclient) problem....

    Quote Originally Posted by Fubarable View Post
    You misunderstand me... by using non-standard naming conventions, you're making it difficult for us to understand your code. If we're going to be able to help you, we need to know what's going on.



    And that's all well and good, and I hope I'm able to help you.



    Ah, an important detail that should probably be in your original post.
    Oh sorry then.. I am just Russian and don't speak advanced English xD

    Anyways, what else do you need to know about the project?
    The flash client is fine, I know actionscript almost like my five fingers.. I don't think that the problem is on the client side...
    Connector class and ChatServer class are in one project named URserver. As you see, connector starting socket server and the loop that waits for a client to connect. When client connects it starts a thread "chatserver" class, and there I have to get the login information from the client. in this case, client has two input boxes user name and password, when client click on login button, client sends to the server login info like this <<username + " " + userpassword>>

    So that's basically it.. and I described the problem already.. I hope that is easier to understand. Thanks for trying to help by the way!

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Another socket (multiclient) problem....

    Moved from Advanced Java.

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

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

    Default Re: Another socket (multiclient) problem....

    Oh comeon... No one gonna help me?

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Another socket (multiclient) problem....

    Have you fixed up your code as suggested earlier so it is more readable?
    What does "actionscript 2" have to do with this program?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Another socket (multiclient) problem....

    Quote Originally Posted by Norm View Post
    Have you fixed up your code as suggested earlier so it is more readable?
    What does "actionscript 2" have to do with this program?
    As I said, actionscript 2 is a client. I've made a game in flash, and trying to make it online by making java sockets, it is working, but not as I expect.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Another socket (multiclient) problem....

    Try debugging the code by changing how you are reading the input to use InputStream class's read() method and print out every byte it reads.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Another socket (multiclient) problem....

    Quote Originally Posted by Norm View Post
    Try debugging the code by changing how you are reading the input to use InputStream class's read() method and print out every byte it reads.
    I've tried that, it stops printing when it reaches the
    Java Code:
    if(goteverything == false)
    for some reason it doesn't do that loop until I click on login (in client) second time

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Another socket (multiclient) problem....

    it stops printing when it reaches the
    Is goteverything true or false when you get to that statement?
    Does the loop go forever or does the return statement cause an exit?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Another socket (multiclient) problem....

    Quote Originally Posted by Norm View Post
    Is goteverything true or false when you get to that statement?
    Does the loop go forever or does the return statement cause an exit?
    When the script starts the goteverything is false until it completes the loop

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Another socket (multiclient) problem....

    I dont' understand what you are saying. Add lots of println statements to the code to show where it executes and hwo the values of the variables change. Copy the print out and the code that generated the print outs.

    You need to create a client program for testing with that you can post here for others to test with. Without a test program there is no way to see what is happening.

    I changed the port to 8080 and used the browser with your server URL=http://127.0.0.1:8080/
    and got this print out: ( I added a loop to read and print what was received)

    Running: java Connector

    Server launched.
    User id 1 connected.
    run
    User's username: GET
    User's password: /
    HTTP/1.1
    Host: 127.0.0.1:8080
    Connection: keep-alive
    User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Encoding: gzip,deflate,sdch
    Accept-Language: en-US,en;q=0.8
    Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
    Cookie: wowbb=%7C%7C%7C%7C-240
    Last edited by Norm; 06-05-2012 at 03:10 AM.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Another socket (multiclient) problem....

    Quote Originally Posted by Norm View Post
    I dont' understand what you are saying. Add lots of println statements to the code to show where it executes and hwo the values of the variables change. Copy the print out and the code that generated the print outs.

    You need to create a client program for testing with that you can post here for others to test with. Without a test program there is no way to see what is happening.

    I changed the port to 8080 and used the browser with your server URL=http://127.0.0.1:8080/
    and got this print out: ( I added a loop to read and print what was received)

    Running: java Connector

    Server launched.
    User id 1 connected.
    run
    User's username: GET
    User's password: /
    HTTP/1.1
    Host: 127.0.0.1:8080
    Connection: keep-alive
    User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Encoding: gzip,deflate,sdch
    Accept-Language: en-US,en;q=0.8
    Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
    Cookie: wowbb=%7C%7C%7C%7C-240
    I have a client lol,
    http://www.swfcabin.com/open/1338908375
    All this client does is sends <<username + " "+ userpassword>> once you click on "login" button.

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Another socket (multiclient) problem....

    The browser worked as a simple client for testing the server code. It looks like the server worked.
    Can you post the source for client code? I don't like going to 3rd party sites.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Another socket (multiclient) problem....

    Quote Originally Posted by Norm View Post
    The browser worked as a simple client for testing the server code. It looks like the server worked.
    Can you post the source for client code? I don't like going to 3rd party sites.
    You've got adobe flash?
    testClient.fla

    It's a project source

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

    Default Re: Another socket (multiclient) problem....

    Quote Originally Posted by Norm View Post
    The browser worked as a simple client for testing the server code. It looks like the server worked.
    Can you post the source for client code? I don't like going to 3rd party sites.
    I am sorry if I am disturbing you, I just really need to fix it..

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Another socket (multiclient) problem....

    Sorry, I don't have adobe flash.

    What did the program read when you changed it to use the read() method to read the bytes one by one and print them as they were read?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Another socket (multiclient) problem....

    Quote Originally Posted by Norm View Post
    Sorry, I don't have adobe flash.

    What did the program read when you changed it to use the read() method to read the bytes one by one and print them as they were read?
    Client doesn't read anything right now, it only sends

    Do you have skype so it would be easier to talk?

Page 1 of 2 12 LastLast

Similar Threads

  1. multiclient/server program
    By maxi in forum Networking
    Replies: 12
    Last Post: 09-30-2011, 05:19 PM
  2. how to do multiclient private chat using socets and threads?
    By tanu.kansal in forum Threads and Synchronization
    Replies: 27
    Last Post: 08-25-2011, 05:31 PM
  3. MultiClient with Single Server
    By prince87 in forum Threads and Synchronization
    Replies: 1
    Last Post: 05-01-2011, 01:03 AM
  4. Socket Connection problem
    By xpan in forum Networking
    Replies: 4
    Last Post: 02-14-2010, 04:32 PM
  5. multiclient copy
    By thuyduong89dt in forum Networking
    Replies: 0
    Last Post: 12-04-2009, 01:46 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
  •