Advertisement
Guest User

Untitled

a guest
Nov 13th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.54 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 = 2234;
  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. os.println("Enter your password.");
  134. String pass = is.readLine().trim();
  135. while(flagname==0){
  136. flagname=1;
  137. for (int i = 0; i <maxClientsCount; i++) {
  138. if (threads[i] != null && namechange.equals(threads[i].Username)) {
  139. count++;
  140. namechange=name+"_"+count;
  141. flagname=0;
  142. break;
  143. }
  144. }
  145.  
  146. }
  147. if(count>0)os.println("Your name is already used, your name changed to "+namechange);
  148.  
  149.  
  150. this.Username=namechange;
  151. this.Password=pass;
  152. this.JoinDate = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(Calendar.getInstance().getTime());
  153. os.println("Hello " + namechange
  154. + " to our chat room.\nJoin Date: "+JoinDate+ "\nTo leave enter /quit in a new line\n");
  155. os.println("Type /help for helps");
  156. for (int i = 0; i < maxClientsCount; i++) {
  157. if (threads[i] != null && threads[i] != this) {
  158. threads[i].os.println("*** A new user " + namechange
  159. + " entered the chat room !!! ***");
  160. }
  161. }
  162. while (!this.interupt) {
  163. String line = is.readLine();
  164.  
  165. if (line.startsWith("/quit")) {
  166. break;
  167. }
  168. if(line.startsWith("/help")){
  169. os.println("@list to see user list" );
  170. os.println("@kill#name#password to kill a user" );
  171. os.println("@votekick#name to vote a user to be kicked" );
  172. os.println("@cek#name to check a user join date" );
  173. os.println("@join#gorupname to join a group");
  174. os.println("@group#groupname <chat> to chat in a group");
  175. os.println("@leave#groupname to leave a group chat");
  176. os.println("@name to personal chat a user");
  177. }
  178. else if(line.startsWith("@list")){
  179. int cnt=0;
  180. for (int i = 0; i <maxClientsCount; i++) {
  181. if (threads[i] != null)
  182. {
  183. cnt++;
  184. os.println(cnt+". "+threads[i].Username);
  185. }
  186. }
  187. }
  188. else if (line.startsWith("@kill"))
  189. {
  190. String[] User = line.split("#",3);
  191. boolean flag=false;
  192. for (int i = 0; i <maxClientsCount; i++) {
  193. if (threads[i] != null && User[1].equals(threads[i].Username)) {
  194. if(User[2].equals(threads[i].Password)){
  195. os.println("You killed "+User[1]);
  196. threads[i].os.println("< KILL --> " + User[1] + " has been killed. you can send a goodbye message");
  197. threads[i].interupt=true;
  198. }
  199.  
  200. flag=true;
  201. break;
  202. }
  203. }
  204. if(flag==false) os.println("<" + namechange + " --> " + User[1] + " not registered");
  205. }
  206. else if(line.startsWith("@votekick"))
  207. {
  208. String[] User = line.split("#",2);
  209. boolean flag=false;
  210. int kicked=-1;
  211. for (int i = 0; i <maxClientsCount; i++) {
  212.  
  213. if (threads[i] != null && User[1].equals(threads[i].Username)) {
  214. os.println("You voted "+User[1]+"to be killed (if there is at least 3 user voted that name, that user will be kicked)");
  215. threads[i].kick+=1;
  216. kicked=i;
  217. flag=true;
  218. break;
  219. }
  220. }
  221.  
  222. for (int i = 0; i <maxClientsCount; i++) {
  223. if(threads[i]!=null && threads[i]!=this && flag==true )
  224. 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)");
  225.  
  226. }
  227. if(threads[kicked].kick>=3 && flag==true) {
  228. threads[kicked].os.println("< VOTE KICK > " + User[1] + " has been kicked. you can send a goodbye message");
  229. threads[kicked].interupt=true;
  230. }
  231. if(flag==false) os.println("<" + namechange + " --> " + User[1] + " not registered");
  232. }
  233.  
  234. else if (line.startsWith("@cek"))
  235. {
  236. String[] User = line.split("#",2);
  237. boolean flag=false;
  238. for (int i = 0; i <maxClientsCount; i++) {
  239. if (threads[i] != null && User[1].equals(threads[i].Username)) {
  240. os.println("<" + namechange + " --> " + User[1] + " entered the room chat at " + threads[i].JoinDate);
  241. flag=true;
  242. break;
  243. }
  244. }
  245. if(flag==false) os.println("<" + namechange + " --> " + User[1] + " not registered");
  246. }
  247. else if(line.startsWith("@join"))
  248. {
  249. String[] to = line.split("#",2);
  250. boolean flag = false;
  251. int groupnumber = cari(to[1]);
  252. if(groupnumber != -1 && groupnumber != MultiThreadChatServer.group.length) {
  253. flag = true;
  254. this.groups[groupnumber] = true;
  255. }
  256.  
  257. if(flag==true) {
  258. os.println ("You entered the group" +to[1]);
  259. for (int i = 0; i < maxClientsCount; i++) {
  260. if (threads[i] != null && threads[i] != this && threads[i].groups[groupnumber] == true) {
  261. threads[i].os.println("*** A new user " + name + " entered the " + MultiThreadChatServer.group[groupnumber] + " chat !!! ***");
  262. }
  263. }
  264. }
  265. else {
  266.  
  267. if(groupnumber != MultiThreadChatServer.group.length) {
  268. for(int i=0;i<MultiThreadChatServer.group.length;i++) {
  269. if(MultiThreadChatServer.group[i] == null) {
  270. this.groups[i] = true;
  271. MultiThreadChatServer.group[i] = to[1];
  272. os.println("You created a group " + to[1]);
  273. break;
  274. }
  275. }
  276. }
  277.  
  278. else {
  279. os.println("Maximum group is exceeded");
  280. }
  281. }
  282. }
  283. else if(line.startsWith("@invite")){
  284. String[] GName = line.split("#",3);
  285. int groupnumber = cari(GName[1]);
  286. boolean ismember = false;
  287. int invismember = -1;
  288. if(groupnumber == -1 || groupnumber == MultiThreadChatServer.group.length)
  289. {
  290. os.println("Group "+ GName[1]+" not available");
  291. }
  292. else{
  293. for (int i = 0; i < maxClientsCount; i++) {
  294. if (threads[i] != null && this.groups[groupnumber]==true ) {
  295. ismember = true;
  296. }
  297. if (threads[i] != null && GName[2].equals(threads[i].Username)&&threads[i].groups[groupnumber]==false ) {
  298. invismember = i;
  299. }
  300. }
  301. if(ismember == false) {
  302. os.println("You are not member of " + GName[1]);
  303. }
  304. else if(invismember ==-1){
  305. os.println(GName[2]+" already a member of " + GName[1]);
  306. }
  307. else {
  308. threads[invismember].os.println ("You invited to the group" +GName[1]);
  309. threads[invismember].groups[groupnumber]=true;
  310. for (int i = 0; i < maxClientsCount; i++) {
  311. if (threads[i] != null && threads[i].groups[groupnumber] == true) {
  312. threads[i].os.println("*** A new user " + GName[2] + " entered the " + MultiThreadChatServer.group[groupnumber] + " chat !!! ***");
  313. }
  314. }
  315. }
  316. }
  317.  
  318. }
  319. else if(line.startsWith("@group"))
  320. {
  321. String[] GChat = line.split(" ",2);
  322. String[] GName = GChat[0].split("#",2);
  323. int groupnumber = cari(GName[1]);
  324. if(groupnumber == -1 || groupnumber == MultiThreadChatServer.group.length) {
  325. os.println("Group "+ GName[1]+" not available");
  326. }
  327. else {
  328. boolean ismember = false;
  329.  
  330. for (int i = 0; i < maxClientsCount; i++) {
  331. if (threads[i] != null && threads[i].groups[groupnumber] == true && this.groups[groupnumber]==true ) {
  332. ismember = true;
  333. threads[i].os.println("<" + namechange + " @ " + MultiThreadChatServer.group[groupnumber] + " --> " + GChat[1]);
  334. }
  335. }
  336. if(ismember == false) {
  337. os.println("You are not member of " + GName[1]);
  338. }
  339. }
  340. }
  341. else if(line.startsWith("@leave"))
  342. {
  343. String[] GName = line.split("#",2);
  344. int groupnumber = cari(GName[1]);
  345. if(groupnumber == -1 || groupnumber == MultiThreadChatServer.group.length) {
  346. os.println("Group "+ GName[1]+" not available");
  347. }
  348. else if(groupnumber != -1 && groupnumber != MultiThreadChatServer.group.length) {
  349. os.println("*** You leave the group "+ GName[1] +" ***");
  350. this.groups[groupnumber] = false;
  351. for (int i = 0; i < maxClientsCount; i++) {
  352. if (threads[i] != null && threads[i].groups[groupnumber] == true ) {
  353. threads[i].os.println("*** The user " + namechange +" is leaving the group " +GName[1]+ " ***" );
  354. }
  355. }
  356. }
  357. }
  358. else if(line.startsWith("@groupkick")){
  359. //String[] GName = line.split("#");
  360. String[] Member =line.split("#");
  361. int groupnumber = cari(Member[1]);
  362. if(groupnumber == -1 || groupnumber == MultiThreadChatServer.group.length) {
  363. os.println("Group "+ Member[1]+" not available");
  364. }
  365. else if(groupnumber != -1 && groupnumber != MultiThreadChatServer.group.length) {
  366. boolean ismember=false;
  367. int kicismember=-1;
  368. for (int i = 0; i < maxClientsCount; i++) {
  369. if (threads[i] != null && this.groups[groupnumber]==true ) {
  370. ismember = true;
  371. }
  372. if (threads[i] != null && Member[2].equals(threads[i].Username)&&threads[i].groups[groupnumber]==true ) {
  373. kicismember = i;
  374. }
  375. }
  376. if(ismember == false) {
  377. os.println("You are not member of " + Member[1]);
  378. }
  379. else if(kicismember ==-1){
  380. os.println(Member[2]+" are not a member of " + Member[1]);
  381. }
  382. else {
  383. threads[kicismember].os.println("*** You leave the group "+ Member[1] +" ***");
  384. threads[kicismember].groups[groupnumber] = false;
  385. for (int i = 0; i < maxClientsCount; i++) {
  386. if (threads[i] != null && threads[i].groups[groupnumber] == true ) {
  387. threads[i].os.println("*** The user " + Member[2] +" kicked from the group " +Member[1]+ " ***" );
  388. }
  389. }
  390. }
  391. }
  392. }
  393. else if(line.startsWith("@"))
  394. {
  395. String[] PC = line.split(" ",2);
  396. String to = PC[0].substring(1);
  397. for (int i = 0; i < maxClientsCount; i++) {
  398. if (threads[i] != null && (to.equals(threads[i].Username) || threads[i] == this)) {
  399. threads[i].os.println("<" + namechange+ " to "+ to +"--> " + PC[1]);
  400. }
  401. }
  402. }
  403. else {
  404. for (int i = 0; i < maxClientsCount; i++) {
  405. if (threads[i] != null) {
  406. threads[i].os.println("<" + namechange + "--> " + line);
  407. }
  408. }
  409. }
  410. }
  411. for (int i = 0; i < maxClientsCount; i++) {
  412. if (threads[i] != null && threads[i] != this) {
  413. threads[i].os.println("*** The user " + namechange
  414. + " is leaving the chat room !!! ***");
  415. }
  416. }
  417. os.println("*** Bye " + namechange + " ***");
  418.  
  419. /*
  420. * Clean up. Set the current thread variable to null so that a new client
  421. * could be accepted by the server.
  422. */
  423. for (int i = 0; i < maxClientsCount; i++) {
  424. if (threads[i] == this) {
  425. threads[i] = null;
  426. }
  427. }
  428.  
  429. /*
  430. * Close the output stream, close the input stream, close the socket.
  431. */
  432. is.close();
  433. os.close();
  434. clientSocket.close();
  435.  
  436. } catch (IOException e) {
  437. }
  438. }
  439. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement