Advertisement
Guest User

Untitled

a guest
Jun 9th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. try
  2. {
  3. ss = new ServerSocket(Integer.valueOf(port));
  4. Thread t1 = new Thread()
  5. {
  6. @Override
  7. public void run()
  8. {
  9. BufferedReader in = null;
  10. PrintWriter out = null;
  11. Connection con = null;
  12. PreparedStatement stmt = null;
  13. Socket s = null;
  14.  
  15. while(true)
  16. {
  17.  
  18. try
  19. {
  20. System.out.println("poczatek");
  21. if(!ss.isBound())
  22. {
  23. s = ss.accept();
  24. }
  25. else
  26. {
  27. Thread t2 = new Thread()
  28. {
  29. Socket innerS = null;
  30. @Override
  31. public void run()
  32. {
  33. try
  34. {
  35. innerS = ss.accept();
  36. }
  37. catch(Exception e)
  38. {
  39. e.printStackTrace();
  40. }
  41. }
  42. };
  43. t2.start();
  44. }
  45.  
  46.  
  47.  
  48. System.out.println("polaczylem sie z klientem");
  49. in = new BufferedReader(new InputStreamReader(s.getInputStream())); //wejsciowy
  50. out = new PrintWriter(s.getOutputStream(), true); //wyjsciowy
  51. String line = in.readLine();
  52.  
  53.  
  54. if(line.matches("\\d.*")) // sygnal bazodanowy
  55. {
  56. System.out.println("Odebralem działanie: "+line);
  57. if(line.startsWith("1"))
  58. {
  59. line = line.substring(1,line.length());
  60. String login = line.split(" ")[0];
  61. String haslo = line.split(" ")[1];
  62. System.out.println("login: "+ login +" haslo: " + haslo);
  63.  
  64. try
  65. {
  66. System.out.println("lalala");
  67. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/komunikator","root","local");
  68. stmt = con.prepareStatement("INSERT INTO user(login,password) VALUES (?,?)");
  69. stmt.setString(1, login);
  70. stmt.setString(2, haslo);
  71. stmt.executeUpdate();
  72. out.println("1created");
  73.  
  74. }
  75. catch (SQLException ex)
  76. {
  77. if (ex.getSQLState().startsWith("23"))
  78. {
  79. out.println("1error1");
  80. }
  81. else
  82. {
  83. out.println("1error2");
  84. }
  85. ex.printStackTrace();
  86. }
  87. finally
  88. { // zwalnianie zasobow
  89. if (stmt != null)
  90. {
  91. try
  92. {
  93. stmt.close();
  94. }
  95. catch (SQLException sqlEx) { }
  96. stmt = null;
  97. }
  98. if(con != null)
  99. {
  100. try
  101. {
  102. con.close();
  103. }
  104. catch (SQLException sqlEx) { }
  105. con = null;
  106. }
  107. }
  108.  
  109. }
  110. else if(line.startsWith("2"))
  111. {
  112.  
  113. }
  114.  
  115.  
  116. }
  117. else
  118. { out.println("Serwer nie obsluguje "+line);
  119. System.out.println("Dostalem: "+line +" i to nie jest obsługiwane dzialanie");
  120. }
  121. }
  122. catch(Exception e)
  123. {
  124. System.out.println("Problem z połączeniem TCP");
  125. e.printStackTrace();
  126. System.exit(1);
  127. }
  128.  
  129.  
  130. }
  131. }
  132. };
  133. t1.start();
  134. }
  135. catch(Exception e)
  136. {
  137. System.out.println("Problem z socketem serwera");
  138. e.printStackTrace();
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement