Results 1 to 11 of 11
Thread: Socket case switch
- 03-21-2012, 06:02 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 12
- Rep Power
- 0
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
Beattie282Last edited by Beattie282; 03-21-2012 at 06:04 PM.
- 03-21-2012, 08:52 PM #2
Re: Socket case switch
how to do a switch / if statment on a 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.
- 03-21-2012, 09:09 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 12
- Rep Power
- 0
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 NormLast edited by Beattie282; 03-21-2012 at 09:11 PM.
- 03-21-2012, 09:17 PM #4
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.
- 03-22-2012, 01:03 AM #5
Member
- Join Date
- Mar 2012
- Posts
- 12
- Rep Power
- 0
Re: Socket case switch
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
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(); } } }
i hope this helps...
thanks beattie282Last edited by Beattie282; 03-22-2012 at 01:58 AM.
- 03-22-2012, 01:26 AM #6
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.
- 03-22-2012, 02:25 PM #7
Member
- Join Date
- Mar 2012
- Posts
- 12
- Rep Power
- 0
Re: Socket case switch
thats what i want to do but the if statment within my code does not seem to get executed :s
- 03-22-2012, 02:39 PM #8
Re: Socket case switch
the if statment within my code does not seem to get 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.
- 03-23-2012, 01:09 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Socket case switch
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,
JosBuild a wall around Donald Trump; I'll pay for it.
- 03-23-2012, 01:42 PM #10
Member
- Join Date
- Mar 2012
- Posts
- 12
- Rep Power
- 0
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");
- 03-23-2012, 01:46 PM #11
Similar Threads
-
about conditonal if else and switch case
By niksipandit in forum New To JavaReplies: 6Last Post: 09-22-2011, 12:53 PM -
java switch case
By aconti in forum New To JavaReplies: 16Last Post: 08-09-2011, 08:05 AM -
Converting if to Switch Case.
By king2be98 in forum New To JavaReplies: 5Last Post: 02-20-2011, 04:46 PM -
if else changes to switch-case?
By noobinoo in forum New To JavaReplies: 1Last Post: 04-23-2010, 06:56 PM -
[SOLVED] Is it possible to have If in a switch case?
By sfe23 in forum New To JavaReplies: 2Last Post: 02-23-2009, 01:34 AM
Bookmarks