Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.25 KB | None | 0 0
  1.  
  2.  
  3. package javaapplication12;
  4.  
  5. import com.mysql.jdbc.*;
  6. import java.net.*;
  7. import java.io.*;
  8. import java.sql.SQLException;
  9. import java.util.*;
  10. import java.awt.*;
  11. import java.awt.event.*;
  12. import java.sql.DriverManager;
  13. import java.sql.ResultSet;
  14. import java.util.logging.Level;
  15. import java.util.logging.Logger;
  16. import sun.misc.CEFormatException;
  17.  
  18. public class ClientThread extends Thread {
  19.  
  20. public static String msg;
  21. Server source;
  22. Socket connection;
  23.  
  24. InputStream inStream;
  25. DataInputStream inDataStream;
  26. OutputStream outStream;
  27. DataOutputStream outDataStream;
  28.  
  29. String message;
  30. String userInput;
  31. PrintWriter printWriter = null;
  32. BufferedReader bufferedReader = null;
  33.  
  34.  
  35. ClientThread ( Socket socket, Server s, int id) {
  36.  
  37. super ();
  38. connection = socket;
  39. source = s;
  40. this.id=id;
  41. } // end constructor
  42.  
  43. int id;//=-1;
  44. String name;
  45.  
  46. private void printMessage(String message)
  47. {
  48. System.out.println(message);
  49. }
  50. public void run () {
  51.  
  52. String stringIn, stringOut;
  53. boolean again = true;
  54.  
  55. InetAddress inet = connection.getInetAddress ();
  56. String origin = inet.getHostName ();
  57. int originport = connection.getPort ();
  58.  
  59. System.out.println("Adding Client: "+origin+":"+originport);
  60. while(true)
  61. try {
  62.  
  63. outStream = connection.getOutputStream ();
  64. outDataStream = new DataOutputStream ( outStream );
  65. inStream = connection.getInputStream ();
  66. inDataStream = new DataInputStream ( inStream );
  67.  
  68. message = inDataStream.readUTF ();
  69. if (message.contains("name"))
  70. {
  71. name = message.substring(5);
  72. id=source.registerPlayer(name);
  73. System.out.println("Player "+name+" id: " +id );
  74. outDataStream.writeUTF ( "Player "+name+" id: "+id );
  75.  
  76.  
  77. if(source.game==null)
  78. { if((source.p[0]!=null)&&(source.p[1]!=null))
  79. source.startGame();
  80. }
  81. }
  82.  
  83. else if (message.contains("login"))
  84. {
  85. String lgn;
  86. String pass;
  87. //lgn = message.substring(6);
  88. int c=0;
  89. for(int k = 0; k < message.length(); k++)
  90. {if (message.charAt(k)==';')
  91. { c = k;
  92. break;
  93. }
  94. }
  95. lgn = message.substring(0,c);
  96. pass = message.substring(c+1);
  97.  
  98. lgn=lgn.substring(6);
  99. pass=pass.substring(9);
  100. PreparedStatement pstat = null;
  101. ResultSet rs = null;
  102.  
  103. String url = "jdbc:mysql://localhost:3306/users";
  104. try {
  105. Connection con = (Connection) DriverManager.getConnection(
  106. url,"root", "password");
  107. //Statement stmt;
  108. //stmt = (Statement) con.createStatement();
  109. pstat = (PreparedStatement) con.prepareStatement("select login,password from users where login='"+ lgn + "' and password = '"+pass+ "'");
  110. //pstat.setString(1,lgn);
  111. //pstat.setString(2,pass);
  112. rs = pstat.executeQuery();
  113. if(!rs.next() && rs.getRow() == 0) {
  114. outDataStream.writeUTF ("Wrong login and/or password.");
  115. source.t[id]=null;
  116. try {
  117. this.finalize();
  118. } catch (Throwable ex) {
  119. Logger.getLogger(ClientThread.class.getName()).log(Level.SEVERE, null, ex);
  120. }
  121. }
  122. else{
  123. id=source.registerPlayer(lgn);
  124. System.out.println("Player "+lgn+" id: " +id );
  125. outDataStream.writeUTF ( "Player "+lgn+" id: "+id );
  126. if(source.game==null)
  127. { if((source.p[0]!=null)&&(source.p[1]!=null))
  128. source.startGame();
  129. }
  130. }
  131. } catch (SQLException ex) {
  132. Logger.getLogger(ClientThread.class.getName()).log(Level.SEVERE, null, ex);
  133. }
  134.  
  135.  
  136.  
  137. }
  138.  
  139.  
  140. else
  141. if (message.contains("all"))
  142. {
  143. String msg;
  144. if (message.length()>3)
  145. msg = message.substring(4);
  146. else msg=" ";
  147. if(this.name!=null)
  148. if(this.id!=-1)
  149. msg+=" From: "+name+" id:"+id;
  150.  
  151. outDataStream.writeUTF (msg);
  152. source.sendAll(msg);
  153. }
  154.  
  155.  
  156.  
  157. else
  158. if (message.contains("board"))
  159. {
  160. String board;
  161. board = message.substring(6);
  162. source.game.board.setTable(board);
  163. source.game.pass=0;
  164. int id2;
  165. if(id==0)
  166. id2=1;
  167. else
  168. id2=0;
  169.  
  170. OutputStream outStream = source.t[id2].connection.getOutputStream ();
  171. DataOutputStream outDataStream = new DataOutputStream ( outStream );
  172. outDataStream.writeUTF (message);
  173. String msg="play";
  174. outDataStream.writeUTF (msg);
  175. outDataStream.writeUTF (msg);
  176. source.sendAll("gra "+ source.p[id2].name);
  177.  
  178. }
  179.  
  180. else if (message.contains("pass"))
  181. {
  182. source.game.pass++;
  183. if(source.game.pass==2)
  184. { source.sendAll("Koniec gry. Nikt nie wygrał");
  185. }
  186. else{
  187. int id2;
  188. if(id==0)
  189. id2=1;
  190. else
  191. id2=0;
  192.  
  193. OutputStream outStream = source.t[id2].connection.getOutputStream ();
  194. DataOutputStream outDataStream = new DataOutputStream ( outStream );
  195. outDataStream.writeUTF (message);
  196. String msg="play";
  197. outDataStream.writeUTF (msg);
  198. source.sendAll("gra "+ source.p[id2].name);
  199. }
  200. }
  201.  
  202.  
  203. else if (message.contains("win"))
  204. { String winner;
  205. winner = message.substring(4);
  206. source.sendAll("Koniec gry. Wygrywa "+ source.p[Integer.parseInt(winner)].name);
  207. }
  208.  
  209. else
  210. {
  211. System.out.println(" Message, below, received" );
  212. System.out.println( message );
  213. // outDataStream.writeUTF ( message+" server" );
  214. // System.out.println( " Message returned to client");
  215. source.sendAll(msg);
  216. System.out.println( " Message sent to all");
  217. }
  218.  
  219.  
  220. } // end try
  221.  
  222. catch ( EOFException except ) {
  223. System.out.println( " Connection closed by Client\n\n");
  224. try {
  225.  
  226. source.t[id]=null;
  227. source.p[id]=null;
  228. int id2;
  229. if(id==0)
  230. id2=1;
  231. else
  232. id2=0;
  233. if(source.t[id2]!=null)
  234. {OutputStream outStream = source.t[id2].connection.getOutputStream ();
  235. DataOutputStream outDataStream = new DataOutputStream ( outStream );
  236. outDataStream.writeUTF ("Koniec. Przeciwnik zakończył grę");
  237. }
  238. source.game = null;
  239. connection.close ();
  240. return;
  241. }
  242. catch ( IOException e ) {
  243. e.printStackTrace ();
  244. return;
  245. } // end IOException
  246.  
  247. } // end catch EOFException
  248. catch ( IOException e ) {
  249. System.out.println( " Connection closed abormally\n" );
  250. e.printStackTrace ();
  251. return;
  252. } // end catch IOException
  253. } // end run
  254.  
  255.  
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement