Advertisement
Guest User

server

a guest
Feb 14th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.77 KB | None | 0 0
  1. package tcpserver;
  2.  
  3. import java.awt.Color;
  4. import java.awt.event.*;
  5. import java.net.*;
  6. import java.io.*;
  7. import java.sql.*;
  8. import java.util.ArrayList;
  9. import javax.swing.*;
  10.  
  11. public class TCPServer implements Runnable {
  12.  
  13. private static final String UserName = "root";
  14. private static final String Password = "";
  15. private static final String ConnString = "jdbc:mysql://localhost:3306/football_db?zeroDateTimeBehavior=convertToNull";
  16.  
  17. private ChatServerThread clients[] = new ChatServerThread[50];
  18. private ServerSocket server = null;
  19. private Thread thread = null;
  20. private int clientCount = 0;
  21.  
  22. Connection conn = null;
  23.  
  24. String[][] userId = new String[20][20];
  25. int userIdCount = -1;
  26.  
  27. public TCPServer(int port) {
  28. try {
  29. System.out.println("Binding to port " + port + ", please wait ...");
  30. server = new ServerSocket(port);
  31. System.out.println("Server started: " + server);
  32.  
  33. start();
  34. } catch (IOException ioe) {
  35. System.out.println("Can not bind to port " + port + ": " + ioe.getMessage());
  36. }
  37.  
  38. }
  39.  
  40. @Override
  41. public void run() {
  42. while (thread != null) {
  43. try {
  44. System.out.println("Waiting for a client ...");
  45. addThread(server.accept());
  46. } catch (IOException ioe) {
  47. System.out.println("Server accept error: " + ioe);
  48. stop();
  49. }
  50. }
  51. }
  52.  
  53. public void start() {
  54. if (thread == null) {
  55. thread = new Thread(this);
  56. thread.start();
  57. }
  58. }
  59.  
  60. public void stop() {
  61. if (thread != null) {
  62. thread.stop();
  63. thread = null;
  64. }
  65. }
  66.  
  67. private int findClient(int ID) {
  68. for (int i = 0; i < clientCount; i++) {
  69. if (clients[i].getID() == ID) {
  70. return i;
  71. }
  72. }
  73. return -1;
  74. }
  75.  
  76. public synchronized void handle(int ID, String input) throws ClassNotFoundException {
  77. //System.out.println("Client " + ID + " sent: " + input);
  78.  
  79. if (input.endsWith("user")) {
  80. String[] msg = input.split(" ");
  81. String checkName = null;
  82. String checkPass = null;
  83. String[] temp;
  84. temp = checkLogin(msg[0]).split(" ");
  85. checkName = temp[0];
  86. checkPass = temp[1];
  87.  
  88. if (msg[0].equals("") || !msg[0].equals(checkName) || !msg[1].equals(checkPass)) {
  89. System.out.println("Login failed");
  90. clients[findClient(ID)].send("login failed");
  91. } else {
  92. System.out.println("Login successful");
  93. //ai khne
  94. clients[findClient(ID)].send("login successful");
  95. onlineUser(msg[0]);
  96. userIdCount++;
  97. userId[userIdCount][0] = Integer.toString(ID);
  98. userId[userIdCount][1] = msg[0];
  99. System.out.println("userId[" + userIdCount + "][0]: " + userId[userIdCount][0] + " userId[" + userIdCount + "][1]: " + userId[userIdCount][1]);
  100. }
  101. } else if (input.equals("register")) {
  102. clients[findClient(ID)].send("register button pressed.");
  103. } else if (input.endsWith("registered")) {
  104. String[] msg = input.split(" ");
  105. String fname = msg[1];
  106. String user = msg[2];
  107. String pass = msg[3];
  108. registerEntry(fname, user, pass);
  109. clients[findClient(ID)].send("msg[4]: " + msg[4]);
  110. } else if (input.startsWith("team")) {
  111. String[] temp = input.split(" ");
  112. System.out.println("Alpha team chosen by " + ID);
  113.  
  114. switch (temp[1]) {
  115. case "alpha": {
  116. System.out.println(">> Team alpha selected");
  117. //clients[findClient(ID)].send("check");
  118. String tempUserName;
  119. tempUserName = getNameById(ID);
  120. System.out.println(getNameById(ID));
  121. updateTeam(tempUserName, "alpha");
  122. break;
  123. }
  124. case "beta": {
  125. System.out.println(">> Team beta selected");
  126. //clients[findClient(ID)].send("beta selected");
  127. String tempUserName;
  128. tempUserName = getNameById(ID);
  129. System.out.println(getNameById(ID));
  130. updateTeam(tempUserName, "beta");
  131. break;
  132. }
  133. case "charlie": {
  134. System.out.println(">> Team charlie selected");
  135. //clients[findClient(ID)].send("charlie selected");
  136. String tempUserName;
  137. tempUserName = getNameById(ID);
  138. System.out.println(getNameById(ID));
  139. updateTeam(tempUserName, "charlie");
  140. break;
  141. }
  142. case "delta": {
  143. System.out.println(">> Team delta selected");
  144. //clients[findClient(ID)].send("delta selected");
  145. String tempUserName;
  146. tempUserName = getNameById(ID);
  147. System.out.println(getNameById(ID));
  148. updateTeam(tempUserName, "delta");
  149. break;
  150. }
  151. case "echo": {
  152. System.out.println(">> Team echo selected");
  153. //clients[findClient(ID)].send("echo selected");
  154. String tempUserName;
  155. tempUserName = getNameById(ID);
  156. System.out.println(getNameById(ID));
  157. updateTeam(tempUserName, "echo");
  158. break;
  159. }
  160. default: {
  161. System.out.println("Default switch selected.");
  162. break;
  163. }
  164. }
  165. clients[findClient(ID)].send("team selected");
  166. } else if(input.equals("check")){
  167. System.out.println("Check hche");
  168. }
  169. else if (input.equals("offline")) {
  170. offlineEveryone();
  171. } else if (input.equals(".bye")) {
  172. clients[findClient(ID)].send(".bye");
  173. remove(ID);
  174.  
  175. } else {
  176. /*for (int i = 0; i < clientCount; i++) {
  177. clients[i].send(ID + " sent: " + input);
  178. }*/
  179. System.out.println("else activated.");
  180. }
  181.  
  182. }
  183.  
  184. public synchronized void remove(int ID) {
  185. int pos = findClient(ID);
  186. if (pos >= 0) {
  187. ChatServerThread toTerminate = clients[pos];
  188. System.out.println("Removing client thread " + ID + " at " + pos);
  189. if (pos < clientCount - 1) {
  190. for (int i = pos + 1; i < clientCount; i++) {
  191. clients[i - 1] = clients[i];
  192. }
  193. }
  194. clientCount--;
  195. try {
  196. toTerminate.close();
  197. } catch (IOException ioe) {
  198. System.out.println("Error closing thread: " + ioe);
  199. }
  200. toTerminate.stop();
  201. }
  202. }
  203.  
  204. private void addThread(Socket socket) {
  205. if (clientCount < clients.length) {
  206. System.out.println("Client accepted: " + socket);
  207. clients[clientCount] = new ChatServerThread(this, socket);
  208. try {
  209. clients[clientCount].open();
  210. clients[clientCount].start();
  211. clientCount++;
  212. } catch (IOException ioe) {
  213. System.out.println("Error opening thread: " + ioe);
  214. }
  215. } else {
  216. System.out.println("Client refused: maximum " + clients.length + " reached.");
  217. }
  218. }
  219.  
  220. public static void main(String args[]) {
  221. TCPServer server = null;
  222. server = new TCPServer(2000);
  223.  
  224. }
  225.  
  226. public void registerEntry(String fname, String user, String pass) throws ClassNotFoundException {
  227.  
  228. //System.out.println("First Name: " + fname + " User Name:" + user + " Password: " + pass + " Team Name: " + team);
  229. System.out.println(">>Register hoise from server :D ");
  230. try {
  231. Class.forName("com.mysql.jdbc.Driver");
  232. conn = (Connection) DriverManager.getConnection(ConnString, UserName, Password);
  233. System.out.println("Entry hoche");
  234. Statement st = (Statement) conn.createStatement();
  235. String insert = "INSERT INTO `users` (`first_name`, `user_name`, `password`) VALUES ('" + fname + "', '" + user + "', '" + pass + "');";
  236. st.executeUpdate(insert);
  237. } catch (SQLException e) {
  238. System.err.println(e);
  239. }
  240.  
  241. }
  242.  
  243. public String checkLogin(String user) throws ClassNotFoundException {
  244. String u = null;
  245. String p = null;
  246. String ret = null;
  247. try {
  248. Class.forName("com.mysql.jdbc.Driver");
  249. conn = (Connection) DriverManager.getConnection(ConnString, UserName, Password);
  250. System.out.println(">>Connection Established.");
  251. Statement st = (Statement) conn.createStatement();
  252. String selQuery = "SELECT `user_name`, `password` FROM `users` WHERE user_name = '" + user + "'";
  253. ResultSet checkLog = st.executeQuery(selQuery);
  254.  
  255. while (checkLog.next()) {
  256. u = checkLog.getString("user_name");
  257. p = checkLog.getString("password");
  258. System.out.println(">>>>Login er jonno client pathaise: Name: " + u + " Password: " + p);
  259. }
  260. ret = (u + " " + p);
  261. System.out.println(">>ret : " + ret);
  262.  
  263. } catch (SQLException e) {
  264. System.err.println(e);
  265. }
  266. return ret;
  267. }
  268.  
  269. public void onlineUser(String user) throws ClassNotFoundException {
  270. //**method for making the user online in DB**
  271. System.out.println(user + " just came online.");
  272. try {
  273. Class.forName("com.mysql.jdbc.Driver");
  274. conn = (Connection) DriverManager.getConnection(ConnString, UserName, Password);
  275. System.out.println(">>Connection Established.");
  276. Statement st = (Statement) conn.createStatement();
  277. String upQuery = "UPDATE `users` SET `logged_in`=1 WHERE `user_name` = '" + user + "';";
  278. int checkLog = st.executeUpdate(upQuery);
  279. } catch (SQLException e) {
  280. System.err.println(e);
  281. }
  282. }
  283.  
  284. public void updateTeam(String user, String newTeam) throws ClassNotFoundException {
  285. //**method for updating team name of that username**
  286. System.out.println("user: " + user + " newTeam: " + newTeam);
  287. try {
  288. Class.forName("com.mysql.jdbc.Driver");
  289. conn = (Connection) DriverManager.getConnection(ConnString, UserName, Password);
  290. System.out.println(">>Connection Established.");
  291. Statement st = (Statement) conn.createStatement();
  292. String upQuery = "UPDATE `users` SET `team_name`='" + newTeam + "' WHERE `user_name` = '" + user + "';";
  293. int checkLog = st.executeUpdate(upQuery);
  294. } catch (SQLException e) {
  295. System.err.println(e);
  296. }
  297. }
  298.  
  299. public void totalOnline() {
  300.  
  301. }
  302.  
  303. public void offlineEveryone() throws ClassNotFoundException {
  304. //**Method for offlineing everyone in DB**
  305. System.out.println("Offlining everyone. >>>>BYE");
  306. try {
  307. Class.forName("com.mysql.jdbc.Driver");
  308. conn = (Connection) DriverManager.getConnection(ConnString, UserName, Password);
  309. System.out.println(">>Connection Established.");
  310. Statement st = (Statement) conn.createStatement();
  311. String upQuery = "UPDATE `users` SET `logged_in`=0 WHERE `logged_in` = 1;";
  312. int checkLog = st.executeUpdate(upQuery);
  313. } catch (SQLException e) {
  314. System.err.println(e);
  315. }
  316. }
  317.  
  318. public String getNameById(int ID) {
  319. String ret = null;
  320. for (int i = 0; i <= userIdCount; i++) {
  321. System.out.println("userId[i][0]: " + userId[i][0]);
  322. if (userId[i][0].equals(Integer.toString(ID))) {
  323. ret = userId[i][1];
  324. } else {
  325. ret = null;
  326. }
  327. }
  328. return ret;
  329. }
  330.  
  331. public int getIdByName(String name) {
  332. int ret = -1;
  333. for (int i = 0; i <= userIdCount; i++) {
  334. if (userId[i][1].equals(name)) {
  335. ret = Integer.parseInt(userId[i][0]);
  336. } else {
  337. ret = -1;
  338. }
  339. }
  340. return ret;
  341. }
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement