Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void run(){
- String inputLine;
- try
- {
- //input stream reader
- //get socket writing and reading streams
- BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- DataOutputStream out = new DataOutputStream(socket.getOutputStream());
- out.writeBytes(simpleprotocol.createMessage("", "Welcome to my Server") + "\n");
- //Now start reading input from client
- while((inputLine = in.readLine()) != null) {
- //reply with the same message, adding some text
- chooseFunction(inputLine);
- System.out.println("");
- System.out.println(inputLine);
- System.out.println("");
- System.out.println("");
- out.writeBytes((inputLine) +"\n");
- }
- //client disconnected, so close socket
- // socket.close();
- }
- catch (IOException e)
- {
- System.out.println("IOException on socket : " + e);
- e.printStackTrace();
- }
- }
- public void chooseFunction(String message){
- String[] received = simpleprotocol.decodeMessage(message);
- String function = received[0];
- if(function.equals("sign-up")){
- userSignUp(message);
- }else if(function.equals("sign-in")){
- userSignIn(message);
- }else if(function.equals("get-message")){
- getMessage(message);
- }else if(function.equals("send-message")){
- sendMessage(message);
- }
- }
- public String userSignUp(String credentials){
- String[] received = simpleprotocol.decodeMessage(credentials);
- String username = received[1];
- String password = received[2];
- if(!(userDetails.containsKey(username))){
- if(username.length() >=5 && username.length() <=20){
- if(password.length() >= 8 && password.length() <=32){
- userDetails.put(username, password);
- }
- }
- }
- return simpleprotocol.createMessage("sign-up", "true","User successfully signed up");
- // try{
- // socket.close();
- // }catch (IOException e){
- // e.printStackTrace();
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement