Advertisement
Guest User

Untitled

a guest
Nov 13th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.00 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package uts;
  7.  
  8. /**
  9. *
  10. * @author A46CB
  11. */
  12. import java.io.DataInputStream;
  13. import java.io.IOException;
  14. import java.io.PrintStream;
  15. import java.net.ServerSocket;
  16. import java.net.Socket;
  17.  
  18. import java.text.SimpleDateFormat;
  19. import java.util.Calendar;
  20.  
  21. /**
  22. *
  23. * @author ayya
  24. */
  25. public class MultiThreadChatServer {
  26.  
  27. // The server socket.
  28. private static ServerSocket serverSocket = null;
  29. // The client socket.
  30. private static Socket clientSocket = null;
  31.  
  32. public static String[] group = new String[20];
  33. // This chat server can accept up to maxClientsCount clients' connections.
  34. private static final int maxClientsCount = 20;
  35. private static final clientThread[] threads = new clientThread[maxClientsCount];
  36.  
  37. public static void main(String args[]) {
  38.  
  39. // The default port number.
  40. int portNumber = 2224;
  41. if (args.length < 1) {
  42. System.out
  43. .println("Usage: java MultiThreadChatServer <portNumber>\n"
  44. + "Now using port number=" + portNumber);
  45. } else {
  46. portNumber = Integer.valueOf(args[0]).intValue();
  47. }
  48.  
  49. try {
  50. serverSocket = new ServerSocket(portNumber);
  51. } catch (IOException e) {
  52. System.out.println(e);
  53. }
  54.  
  55. /*
  56. * Create a client socket for each connection and pass it to a new client
  57. * thread.
  58. */
  59. while (true) {
  60. try {
  61. clientSocket = serverSocket.accept();
  62. int i = 0;
  63. for (i = 0; i < maxClientsCount; i++) {
  64. if (threads[i] == null) {
  65. (threads[i] = new clientThread(clientSocket, threads)).start();
  66. break;
  67. }
  68. }
  69. if (i == maxClientsCount) {
  70. PrintStream os = new PrintStream(clientSocket.getOutputStream());
  71. os.println("Server too busy. Try later.");
  72. os.close();
  73. clientSocket.close();
  74. }
  75. } catch (IOException e) {
  76. System.out.println(e);
  77. }
  78. }
  79. }
  80. }
  81. /*
  82. * The chat client thread. This client thread opens the input and the output
  83. * streams for a particular client, ask the client's name, informs all the
  84. * clients connected to the server about the fact that a new client has joined
  85. * the chat room, and as long as it receive data, echos that data back to all
  86. * other clients. When a client leaves the chat room this thread informs also
  87. * all the clients about that and terminates.
  88. */
  89. class clientThread extends Thread {
  90.  
  91. private DataInputStream is = null;
  92. private PrintStream os = null;
  93. private Socket clientSocket = null;
  94. private final clientThread[] threads;
  95. private int maxClientsCount;
  96. public String Username, JoinDate,Password;
  97. public int kick=0;
  98. public boolean interupt=false;
  99. public boolean[] groups = new boolean[20];
  100.  
  101. public clientThread(Socket clientSocket, clientThread[] threads) {
  102. this.clientSocket = clientSocket;
  103. this.threads = threads;
  104. maxClientsCount = threads.length;
  105. }
  106.  
  107. public int cari(String GroupName) {
  108. int groupnumber = -1;
  109. for(int i=0;i<MultiThreadChatServer.group.length;i++) {
  110. if(MultiThreadChatServer.group[i] != null && GroupName.equals(MultiThreadChatServer.group[i])) {
  111. groupnumber = i;
  112. break;
  113. }
  114. }
  115. if(MultiThreadChatServer.group[19] != null) groupnumber = MultiThreadChatServer.group.length;
  116. return groupnumber;
  117. }
  118.  
  119. public void run() {
  120. int maxClientsCount = this.maxClientsCount;
  121. clientThread[] threads = this.threads;
  122.  
  123. try {
  124. /*
  125. * Create input and output streams for this client.
  126. */
  127. is = new DataInputStream(clientSocket.getInputStream());
  128. os = new PrintStream(clientSocket.getOutputStream());
  129. os.println("Enter your name.");
  130. String name = is.readLine().trim();
  131. int flagname=0, count=0;
  132. String namechange=name;
  133. while(flagname==0){
  134. flagname=1;
  135. for (int i = 0; i <maxClientsCount; i++) {
  136. if (threads[i] != null && namechange.equals(threads[i].Username)) {
  137. count++;
  138. namechange=name+"_"+count;
  139. flagname=0;
  140. break;
  141. }
  142. }
  143.  
  144. }
  145. if(count>0)os.println("Your name is already used, your name changed to "+namechange);
  146.  
  147. os.println("Enter your password.");
  148. String pass = is.readLine().trim();
  149. this.Username=namechange;
  150. this.Password=pass;
  151. this.JoinDate = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(Calendar.getInstance().getTime());
  152. os.println("Hello " + namechange
  153. + " to our chat room.\nJoin Date: "+JoinDate+ "\nTo leave enter /quit in a new line\n");
  154. os.println("Type /help for helps");
  155. for (int i = 0; i < maxClientsCount; i++) {
  156. if (threads[i] != null && threads[i] != this) {
  157. threads[i].os.println("*** A new user " + namechange
  158. + " entered the chat room !!! ***");
  159. }
  160. }
  161. while (!this.interupt) {
  162. String line = is.readLine();
  163.  
  164. if (line.startsWith("/quit")) {
  165. break;
  166. }
  167. if(line.startsWith("/help")){
  168. os.println("@kill#name#password to kill a user" );
  169. os.println("@votekick#name to vote a user to be kicked" );
  170. os.println("@cek#name to check a user join date" );
  171. os.println("@join#gorupname to join a group");
  172. os.println("@group#groupname <chat> to chat in a group");
  173. os.println("@leave#groupname to leave a group chat");
  174. os.println("@name to personal chat a user");
  175. }
  176. else if (line.startsWith("@kill"))
  177. {
  178. String[] User = line.split("#",3);
  179. boolean flag=false;
  180. for (int i = 0; i <maxClientsCount; i++) {
  181. if (threads[i] != null && User[1].equals(threads[i].Username)) {
  182. if(User[2].equals(threads[i].Password)){
  183. os.println("You killed "+User[1]);
  184. threads[i].os.println("< KILL --> " + User[1] + " has been killed. you can send a goodbye message");
  185. threads[i].interupt=true;
  186. }
  187.  
  188. flag=true;
  189. break;
  190. }
  191. }
  192. if(flag==false) os.println("<" + namechange + " --> " + User[1] + " not registered");
  193. }
  194. else if(line.startsWith("@votekick"))
  195. {
  196. String[] User = line.split("#",2);
  197. boolean flag=false;
  198. for (int i = 0; i <maxClientsCount; i++) {
  199.  
  200. if (threads[i] != null && User[1].equals(threads[i].Username)) {
  201. os.println("You voted "+User[1]+"to be killed (if there is at least 3 user voted that name, that user will be kicked)");
  202. threads[i].kick+=1;
  203. if(threads[i].kick>=3) {
  204. threads[i].os.println("< VOTE KICK --> " + User[1] + " has been kicked. you can send a goodbye message");
  205. threads[i].interupt=true;
  206. }
  207. flag=true;
  208. break;
  209. }
  210. }
  211.  
  212. for (int i = 0; i <maxClientsCount; i++) {
  213. if(threads[i]!=null && threads[i]!=this && User[1].equals(threads[i].Username) && flag==true)
  214. threads[i].os.println(User[1]+" has been voted to be kicked to be killed (if there is at least 3 user voted that name, that user will be kicked)");
  215.  
  216. }
  217. if(flag==false) os.println("<" + namechange + " --> " + User[1] + " not registered");
  218. }
  219.  
  220. else if (line.startsWith("@cek"))
  221. {
  222. String[] User = line.split("#",2);
  223. boolean flag=false;
  224. for (int i = 0; i <maxClientsCount; i++) {
  225. if (threads[i] != null && User[1].equals(threads[i].Username)) {
  226. os.println("<" + namechange + " --> " + User[1] + " entered the room chat at " + threads[i].JoinDate);
  227. flag=true;
  228. break;
  229. }
  230. }
  231. if(flag==false) os.println("<" + namechange + " --> " + User[1] + " not registered");
  232. }
  233. else if(line.startsWith("@join"))
  234. {
  235. String[] to = line.split("#",2);
  236. boolean flag = false;
  237. int groupnumber = cari(to[1]);
  238. if(groupnumber != -1 && groupnumber != MultiThreadChatServer.group.length) {
  239. flag = true;
  240. this.groups[groupnumber] = true;
  241. }
  242.  
  243. if(flag==true) {
  244. os.println ("You entered the group" +to[1]);
  245. for (int i = 0; i < maxClientsCount; i++) {
  246. if (threads[i] != null && threads[i] != this && threads[i].groups[groupnumber] == true) {
  247. threads[i].os.println("*** A new user " + name + " entered the " + MultiThreadChatServer.group[groupnumber] + " chat !!! ***");
  248. }
  249. }
  250. }
  251. else {
  252.  
  253. if(groupnumber != MultiThreadChatServer.group.length) {
  254. for(int i=0;i<MultiThreadChatServer.group.length;i++) {
  255. if(MultiThreadChatServer.group[i] == null) {
  256. this.groups[i] = true;
  257. MultiThreadChatServer.group[i] = to[1];
  258. os.println("You created a group " + to[1]);
  259. break;
  260. }
  261. }
  262. }
  263.  
  264. else {
  265. os.println("Maximum group is exceeded");
  266. }
  267. }
  268. }
  269. else if(line.startsWith("@group"))
  270. {
  271. String[] GChat = line.split(" ",2);
  272. String[] GName = GChat[0].split("#",2);
  273. int groupnumber = cari(GName[1]);
  274. if(groupnumber == -1 || groupnumber == MultiThreadChatServer.group.length) {
  275. os.println("Group "+ GName[1]+" not available");
  276. }
  277. else {
  278. boolean ismember = false;
  279.  
  280. for (int i = 0; i < maxClientsCount; i++) {
  281. if (threads[i] != null && threads[i].groups[groupnumber] == true && this.groups[groupnumber]==true ) {
  282. ismember = true;
  283. threads[i].os.println("<" + namechange + " @ " + MultiThreadChatServer.group[groupnumber] + " --> " + GChat[1]);
  284. }
  285. }
  286. if(ismember == false) {
  287. os.println("You are not member of " + GName[1]);
  288. }
  289. }
  290. }
  291. else if(line.startsWith("@leave"))
  292. {
  293. String[] GName = line.split("#",2);
  294. int groupnumber = cari(GName[1]);
  295. if(groupnumber == -1 || groupnumber == MultiThreadChatServer.group.length) {
  296. os.println("Group "+ GName[1]+" not available");
  297. }
  298. else if(groupnumber != -1 && groupnumber != MultiThreadChatServer.group.length) {
  299. os.println("*** You leave the group "+ GName[1] +" ***");
  300. this.groups[groupnumber] = false;
  301. for (int i = 0; i < maxClientsCount; i++) {
  302. if (threads[i] != null && threads[i].groups[groupnumber] == true ) {
  303. threads[i].os.println("*** The user " + namechange +" is leaving the group " +GName[1]+ " ***" );
  304. }
  305. }
  306. }
  307. }
  308.  
  309. else if(line.startsWith("@"))
  310. {
  311. String[] PC = line.split(" ",2);
  312. String to = PC[0].substring(1);
  313. for (int i = 0; i < maxClientsCount; i++) {
  314. if (threads[i] != null && (to.equals(threads[i].Username) || threads[i] == this)) {
  315. threads[i].os.println("<" + namechange+ " to "+ to +"--> " + PC[1]);
  316. }
  317. }
  318. }
  319. else {
  320. for (int i = 0; i < maxClientsCount; i++) {
  321. if (threads[i] != null) {
  322. threads[i].os.println("<" + namechange + "--> " + line);
  323. }
  324. }
  325. }
  326. }
  327. for (int i = 0; i < maxClientsCount; i++) {
  328. if (threads[i] != null && threads[i] != this) {
  329. threads[i].os.println("*** The user " + namechange
  330. + " is leaving the chat room !!! ***");
  331. }
  332. }
  333. os.println("*** Bye " + namechange + " ***");
  334.  
  335. /*
  336. * Clean up. Set the current thread variable to null so that a new client
  337. * could be accepted by the server.
  338. */
  339. for (int i = 0; i < maxClientsCount; i++) {
  340. if (threads[i] == this) {
  341. threads[i] = null;
  342. }
  343. }
  344.  
  345. /*
  346. * Close the output stream, close the input stream, close the socket.
  347. */
  348. is.close();
  349. os.close();
  350. clientSocket.close();
  351.  
  352. } catch (IOException e) {
  353. }
  354. }
  355. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement