Results 1 to 11 of 11
  1. #1
    Beattie282 is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Question Socket case switch

    Hello peeps,

    just a quick question to see if any one knows how to do a switch / if statment on a socket.

    for e.g i want to make it so a user can either login or sign up but i only want to use one Socket, but create muttiple threds if this is possible

    so i want it to be a if statment basically but within the socket so the socket works out what thred to create based on either a parsed bit of texted sent from the client or a virable of sorts.

    if any of you have any better ideas to approach such things ill be willing to learn such things,

    i have added a bit of code i have been trying but it doesnt seem to want to work for me.....

    Java Code:
    try {
    	ClientInput = new BufferedReader(new InputStreamReader(Clientsocket.getInputStream()));
    	PrintStream ClientOutput = new PrintStream(Clientsocket.getOutputStream());
    	System.out.println("A connection");	
    	//reading line from the client
    	String ClientBuffer = ClientInput.readLine();
    					
    	if (ClientBuffer != null){
    		System.out.println("Client Sent us: [" + ClientBuffer + "]");
    	}
    	    	ClientOutput.println("OK!");
    		String sockettype = "" + ClientBuffer + "";
    		String delims = "//+";
    		String[] tokens = sockettype.split(delims);   
    	        String socketchoice = tokens[0].toString();
    			
    	if (socketchoice == "1"){
    		System.out.println("Login");  // this will later call the login class
    	}else if(socketchoice == "2"){
    		System.out.println("Register");  // this will later call the Register class
    	}//end else 
    	} catch (IOException e) {
    		e.printStackTrace();
    	}//end catch
    thanks in advanced
    Beattie282
    Last edited by Beattie282; 03-21-2012 at 05:04 PM.

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

    Default Re: Socket case switch

    how to do a switch / if statment on a socket.
    Not sure that question makes sense. A Socket is a class for communicating with another socket.
    Are you talking about what to send to the other socket depending on what choices the user makes?

    What are the multiple threads for?
    Sockets do not create threads.

    Can you explain what you are trying to do?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Beattie282 is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: Socket case switch

    well basically i am making a chat program kind of like msn for my uni project,

    i need a sign up form for new users and i need a login form both need to interact with the backend database,

    i want the sign up form to goto the same socket as the login form.

    so if you look at my code that is the constructor for the threads to enable mutiple clients, ie a thread for each client.

    this is where i want a IF statment or a switch depending on whats best i should have not used the terms "switch / if statment"

    so if the user clicks "login" it goes to the socket gets a new thred setup for it but a thread which deals with logins.
    and if the user clicks "sign up" then it goes to the same socket as the login form but it loads a thread to deal with the Sign up process i.e. check emails and such.

    i hope this little diagram helps

    login -----| |-thred------ login database stuff
    socket
    signup ---| |-thred------ sign up database stuff

    ok the Socket bit is meant to be in the middle of the | | it wont align :(

    thank you Norm
    Last edited by Beattie282; 03-21-2012 at 08:11 PM.

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

    Default Re: Socket case switch

    Is this a client or a server you are talking about?
    What does the socket connect to?
    What is sent over the connection?
    I don't understand what you mean by "the socket gets a new thread ". A socket is a way to communicate. A thread getting a socket would make more sense.

    The user makes a choice: login or signup;
    a thread is created to handle that task;
    a connection to a server is made through a socket;
    a message is built and sent and a response received.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Beattie282 is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: Socket case switch

    Quote Originally Posted by Norm View Post
    Is this a client or a server you are talking about?
    The user makes a choice: login or signup;
    a thread is created to handle that task;
    a connection to a server is made through a socket;
    a message is built and sent and a response received.
    Its a server,

    the only reason i called it a thread was becuase of these tbh,

    http://docs.oracle.com/javase/tutori...verThread.java
    http://docs.oracle.com/javase/tutori...ltiServer.java

    also the links above are what i have basically setup on my server.

    • The client opens the socket connection to the MultiServer class
    • then that run a new instance of the MultiServerThred class
    • basically in the MultiServerThred i want a if statment or a switch to deceide what function the server will perform ie db row insersion or the password & email check.


    the reason it is setup like this is to handle many clients at once, and the data that gets sent via the socket from the client will be one of two things either

    signup
    login

    REGISTER CODE getting sent across the socket from the client
    Java Code:
    ServerRegOutput.println("" + register + "");// sending a number using int so the server knows it should do with the data below
    ServerOutput.println("" + username + "//" + password + "//" + email + "");// sending another string across to pass the server info for its db querey
    I havent written the login code yet i just because i want to be able to get the IF statment working first

    MY MULTISERVER THRED CLASS
    this is the class that actually handles the connection, if you look below you will see my if statments and i have written comments.
    Java Code:
    public class MultiThred extends Thread{
    		
    		Socket Clientsocket = null;
    		public MultiThred(Socket socket) {
    			super ("MultiThred");
    			this.Clientsocket = socket;
    		}
    		public void run(){
    				// accepting the socket connection it prints any errors
    				
    		        //setting up the read and write sections to and from the client
    				BufferedReader ClientInput;
    				try {
    					ClientInput = new BufferedReader(new InputStreamReader(Clientsocket.getInputStream()));
    					PrintStream ClientOutput = new PrintStream(Clientsocket.getOutputStream());
    					System.out.println("A connection");	
    					//reading line from the client
    					String ClientBuffer = ClientInput.readLine();
    					
    				    	if (ClientBuffer != null){
    							System.out.println("Client Sent us: " + ClientBuffer + "");
                                                            ClientOutput.println("OK!");
    						String sockettype = "" + ClientBuffer + "";
    						String delims = "//+";
    						String[] tokens = sockettype.split(delims);   
    					    String socketchoice = tokens[0].toString();
    						
    						if (socketchoice == "1"){
    							System.out.println("Login");   // here is where the server would do the check pass and email 
    						}else if(socketchoice == "2"){
    							System.out.println("Register"); // here is where the server would do the signup
    						}
    				    	}
    				    	
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    				
    		}
    }
    like i say i have no idea if my approach is the right way to be doing it but i couldent find anyother good examples,

    i hope this helps...

    thanks beattie282
    Last edited by Beattie282; 03-22-2012 at 12:58 AM.

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

    Default Re: Socket case switch

    One way to do it:
    The server gets a connection,
    starts a thread to service the connection
    the new thread, reads the input from client
    and decides what processing needs to be done to handle this request.
    The processing is done and a response is sent to the client.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Beattie282 is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: Socket case switch

    thats what i want to do but the if statment within my code does not seem to get executed :s

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

    Default Re: Socket case switch

    the if statment within my code does not seem to get executed
    Do you mean the condition is never true or that the statement is not executed?
    The only way I can see that it would not be executed would be if an exception is thrown in the try/catch block between the try statement and the if statement.

    Add some printlns to the code to show the values of the variables and to show where the execution flow goes.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Socket case switch

    Quote Originally Posted by Beattie282 View Post
    if (socketchoice == "1"){
    Your question doesn't make any sense because a Socket isn't an integer numeric type (for switch-statements), nor is it a boolean type (for if-conditions). Your code is faulty too: you can't compare Strings with the == operator; use the .equals( ... ) method instead. I'm moving this thread to the New To Java forum section.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Beattie282 is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: Socket case switch

    yea i figured this out this morning when playing about with it, i assumed the == was a comparison and it did not throw any errors so i assumed i would work. but i have it working now,

    it parses my string pulls off the first item make a choice based on that then uses the other parsed items to fill in the database :)

    Java Code:
    if (ClientBuffer != null){
    	System.out.println("Client Sent us: " + ClientBuffer + "");
    	ClientOutput.println("OK!");
    	String sockettype = "" + ClientBuffer + "";
    	String delims = "//";
    	String[] tokens = sockettype.split(delims);   
            socketchoice = tokens[0].toString();
    						    
    if (socketchoice.equals("1")){
    	System.out.println("Login");
    }else if(socketchoice.equals("2")){
    	System.out.println("Register");
    thanks for all the help peeps

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

    Default Re: Socket case switch

    the if statment within my code does not seem to get executed
    I guess your question was not about whether the if statement executed. It always was executing, but it was never true.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. about conditonal if else and switch case
    By niksipandit in forum New To Java
    Replies: 6
    Last Post: 09-22-2011, 11:53 AM
  2. java switch case
    By aconti in forum New To Java
    Replies: 16
    Last Post: 08-09-2011, 07:05 AM
  3. Converting if to Switch Case.
    By king2be98 in forum New To Java
    Replies: 5
    Last Post: 02-20-2011, 03:46 PM
  4. if else changes to switch-case?
    By noobinoo in forum New To Java
    Replies: 1
    Last Post: 04-23-2010, 05:56 PM
  5. Replies: 2
    Last Post: 02-23-2009, 12:34 AM

Tags for this Thread

Posting Permissions

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