Advertisement
Guest User

Untitled

a guest
Aug 9th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. package net;
  2.  
  3. import handlers.PersonHelper;
  4.  
  5. import java.io.BufferedReader;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.io.ObjectInputStream;
  10. import java.io.ObjectOutputStream;
  11. import java.io.Serializable;
  12. import java.net.ConnectException;
  13. import java.net.InetAddress;
  14. import java.net.ServerSocket;
  15. import java.net.Socket;
  16. import java.net.UnknownHostException;
  17. import java.sql.SQLException;
  18. import java.util.HashMap;
  19.  
  20. import javax.naming.InvalidNameException;
  21.  
  22. import main.ServerApplication;
  23. import model.Person;
  24.  
  25. public class ServerConnection extends Thread{
  26. public static HashMap<Person, Socket> activePerson = new HashMap<Person, Socket>();
  27. private ObjectOutputStream oos;
  28. private ObjectInputStream outputStream;
  29. public int port = 4444;
  30. private ServerSocket server = null;
  31. private Socket socket = null;
  32. private LoginHandler loginHandler = null;
  33.  
  34. public ServerConnection(int port){
  35. System.out.println(getIPv4Address());
  36. this.port = port;
  37.  
  38. }
  39.  
  40. public HashMap<Person, Socket> getList(){
  41. return activePerson;
  42.  
  43. }
  44.  
  45.  
  46. public void run(){
  47.  
  48. try {
  49. server = new ServerSocket(port);
  50. } catch (IOException e1) {
  51. e1.printStackTrace();
  52. }
  53. while(true)
  54. {
  55. try
  56. {
  57. socket = server.accept();
  58. if (socket == null) continue;
  59. System.out.println("Someone tried to connect");
  60.  
  61.  
  62. String username = (String)this.receive();
  63. String password = (String)this.receive();
  64.  
  65. if (!loginHandler.isValidLogin(username, password)) {
  66. sendObject(null);
  67. System.out.println("Wrong password. Disconnecting...");
  68. socket.close();
  69. } else {
  70. sendObject(PersonHelper.getPersonFromName(username, ServerApplication.db.getConnection()));
  71. System.out.println(username + " logged in with password " + password);
  72.  
  73. }
  74.  
  75.  
  76. }
  77. catch (IOException e)
  78. {
  79. try {
  80. socket.close();
  81. } catch (IOException e1) {
  82. // TODO Auto-generated catch block
  83. e1.printStackTrace();
  84. }
  85. System.out.println("----- Something went wrong, login denied");
  86.  
  87. } catch (InvalidNameException e) {
  88. } catch (SQLException e) {
  89. }
  90.  
  91. //activePerson.put((Person)receive(), socket);
  92.  
  93. ReceiveWorker cliThread = new ReceiveWorker(socket);
  94. cliThread.start();
  95. System.out.println("New thread started");
  96.  
  97. //Frå klienten sender ein Personen som er tilkopla først.
  98. }
  99. }
  100.  
  101. private String getIPv4Address() {
  102. try {
  103. return InetAddress.getLocalHost().getHostAddress();
  104. }
  105. catch (UnknownHostException e) {
  106.  
  107. return "127.0.0.1";
  108. }
  109. }
  110.  
  111. public int randomPort()
  112. {
  113. int port = (int)(Math.random() *2000) + 5000;
  114. int i = 1;
  115. while(i == 0){
  116. port = (int)(Math.random() *2000) + 5000;
  117. }
  118. return port;
  119. }
  120.  
  121. public void close(){
  122. try {
  123. oos.close();
  124. outputStream.close();
  125. } catch (IOException e) {
  126. //CRASH, ferdig.
  127. }
  128. }
  129.  
  130. public boolean send(Serializable obj){
  131. try {
  132. oos = new ObjectOutputStream(socket.getOutputStream());
  133. oos.writeObject(obj);
  134. } catch (Exception e) {
  135. return false;
  136. }
  137. return true;
  138. }
  139.  
  140.  
  141. public static void main(String[] args) {
  142. new ServerConnection(444);
  143. }
  144. public Serializable receive(){
  145. Serializable obj= null;
  146. ObjectInputStream inputstream;
  147. try {
  148. outputStream = new ObjectInputStream(socket.getInputStream());
  149. obj = (Serializable)outputStream.readObject();
  150. } catch (Exception e) {
  151. //e.printStackTrace();
  152. //return receive();
  153. }
  154. return obj;
  155. }
  156. public void setLoginHandler(LoginHandler l){
  157. this.loginHandler = l;
  158. }
  159.  
  160. public void sendObject(Object o){
  161. try {
  162. oos = new ObjectOutputStream(socket.getOutputStream() );
  163. oos.writeObject(o);
  164.  
  165. } catch (Exception e) {
  166. System.out.println("Couldnt send objec" + o.toString());
  167.  
  168. }
  169. }
  170.  
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement