Advertisement
Guest User

Untitled

a guest
Jan 9th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. package ie.gmit.sw.server;
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5. import java.util.Scanner;
  6.  
  7. public class LoginRequester {
  8.  
  9. Socket requestSocket;
  10. ObjectOutputStream out;
  11. ObjectInputStream in;
  12. String message;
  13. Scanner userInput;
  14.  
  15. public LoginRequester(){
  16. userInput = new Scanner(System.in);
  17. }
  18.  
  19. public void runLogin(){
  20.  
  21. int cont = 0;
  22.  
  23. try {
  24. requestSocket = new Socket ("127.0.0.1", 2010);
  25. System.out.println("Connected to localhost in port 2004");
  26. out = new ObjectOutputStream(requestSocket.getOutputStream());
  27. out.flush();
  28. in = new ObjectInputStream(requestSocket.getInputStream());
  29.  
  30. do{
  31. message = (String)in.readObject();
  32. System.out.println(message);
  33. message = userInput.next();
  34. sendMessage(message);
  35.  
  36. message = (String)in.readObject();
  37. System.out.println(message);
  38. message = userInput.next();
  39. sendMessage(message);
  40.  
  41. cont = 1;
  42.  
  43. }while(cont != 1);
  44. } catch (IOException e) {
  45. System.out.println("FAILED TO CONNECT");
  46. e.printStackTrace();
  47. } catch (ClassNotFoundException e) {
  48. System.out.println("Data recieved in unknown format");
  49. e.printStackTrace();
  50. }
  51. }
  52.  
  53. void sendMessage(String msg) {
  54. try{
  55. out.writeObject(msg);
  56. out.flush();
  57. System.out.println("Client > " + msg);
  58.  
  59. }catch(IOException e){
  60. e.printStackTrace();
  61. }
  62. }
  63.  
  64. public static void main(String[] args) {
  65. LoginRequester client = new LoginRequester();
  66. client.runLogin();
  67. }
  68.  
  69. }
  70.  
  71.  
  72. package ie.gmit.sw.server;
  73.  
  74. import java.io.*;
  75. import java.net.*;
  76. import java.util.Scanner;
  77.  
  78. public class LoginProvide {
  79.  
  80. ServerSocket providerSocket;
  81. Socket connection = null;
  82. ObjectOutputStream out;
  83. ObjectInputStream in;
  84. Scanner input;
  85. String message;
  86. String userInput;
  87. String name;
  88. String address;
  89. String ban;
  90. String user;
  91. String pass;
  92. int choice;
  93. int result;
  94.  
  95. LoginProvide(){
  96. input = new Scanner(System.in);
  97. }
  98.  
  99. void login(){
  100. int cont = 0;
  101. try {
  102. providerSocket = new ServerSocket(2010, 10);
  103.  
  104. System.out.println("Waiting for Connection");
  105. connection = providerSocket.accept();
  106.  
  107. System.out.println("Connection recieved from " + connection.getInetAddress().getHostAddress());
  108. out = new ObjectOutputStream(connection.getOutputStream());
  109. out.flush();
  110. in = new ObjectInputStream(connection.getInputStream());
  111.  
  112. do{
  113. sendMessage("Please Enter your Username ");
  114. try {
  115. message = (String)in.readObject();
  116.  
  117. user = new String (message);
  118.  
  119. sendMessage("Please Enter your Password ");
  120. message = (String)in.readObject();
  121. pass = new String (message);
  122.  
  123. cont = 1;
  124.  
  125. } catch (ClassNotFoundException e) {
  126. e.printStackTrace();
  127. }
  128. }while(cont != 1);
  129.  
  130. } catch (IOException e) {
  131. e.printStackTrace();
  132. }
  133.  
  134. finally{
  135. try {
  136. in.close();
  137. out.close();
  138. providerSocket.close();
  139. } catch (IOException e) {
  140. e.printStackTrace();
  141. }
  142. }
  143. }
  144.  
  145. private void sendMessage(String msg) {
  146.  
  147. try {
  148. out.writeObject(msg);
  149. out.flush();
  150. System.out.println("Server > " + msg);
  151. } catch (IOException e) {
  152. e.printStackTrace();
  153. }
  154. }
  155.  
  156. public static void main(String[] args) {
  157.  
  158. LoginProvide server = new LoginProvide();
  159.  
  160. while(true){
  161. server.login();
  162. }
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement