Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package uts;
- /**
- *
- * @author A46CB
- */
- import java.io.DataInputStream;
- import java.io.IOException;
- import java.io.PrintStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- /**
- *
- * @author ayya
- */
- public class MultiThreadChatServer {
- // The server socket.
- private static ServerSocket serverSocket = null;
- // The client socket.
- private static Socket clientSocket = null;
- public static String[] group = new String[20];
- // This chat server can accept up to maxClientsCount clients' connections.
- private static final int maxClientsCount = 20;
- private static final clientThread[] threads = new clientThread[maxClientsCount];
- public static void main(String args[]) {
- // The default port number.
- int portNumber = 2224;
- if (args.length < 1) {
- System.out
- .println("Usage: java MultiThreadChatServer <portNumber>\n"
- + "Now using port number=" + portNumber);
- } else {
- portNumber = Integer.valueOf(args[0]).intValue();
- }
- try {
- serverSocket = new ServerSocket(portNumber);
- } catch (IOException e) {
- System.out.println(e);
- }
- /*
- * Create a client socket for each connection and pass it to a new client
- * thread.
- */
- while (true) {
- try {
- clientSocket = serverSocket.accept();
- int i = 0;
- for (i = 0; i < maxClientsCount; i++) {
- if (threads[i] == null) {
- (threads[i] = new clientThread(clientSocket, threads)).start();
- break;
- }
- }
- if (i == maxClientsCount) {
- PrintStream os = new PrintStream(clientSocket.getOutputStream());
- os.println("Server too busy. Try later.");
- os.close();
- clientSocket.close();
- }
- } catch (IOException e) {
- System.out.println(e);
- }
- }
- }
- }
- /*
- * The chat client thread. This client thread opens the input and the output
- * streams for a particular client, ask the client's name, informs all the
- * clients connected to the server about the fact that a new client has joined
- * the chat room, and as long as it receive data, echos that data back to all
- * other clients. When a client leaves the chat room this thread informs also
- * all the clients about that and terminates.
- */
- class clientThread extends Thread {
- private DataInputStream is = null;
- private PrintStream os = null;
- private Socket clientSocket = null;
- private final clientThread[] threads;
- private int maxClientsCount;
- public String Username, JoinDate,Password;
- public int kick=0;
- public boolean interupt=false;
- public boolean[] groups = new boolean[20];
- public clientThread(Socket clientSocket, clientThread[] threads) {
- this.clientSocket = clientSocket;
- this.threads = threads;
- maxClientsCount = threads.length;
- }
- public int cari(String GroupName) {
- int groupnumber = -1;
- for(int i=0;i<MultiThreadChatServer.group.length;i++) {
- if(MultiThreadChatServer.group[i] != null && GroupName.equals(MultiThreadChatServer.group[i])) {
- groupnumber = i;
- break;
- }
- }
- if(MultiThreadChatServer.group[19] != null) groupnumber = MultiThreadChatServer.group.length;
- return groupnumber;
- }
- public void run() {
- int maxClientsCount = this.maxClientsCount;
- clientThread[] threads = this.threads;
- try {
- /*
- * Create input and output streams for this client.
- */
- is = new DataInputStream(clientSocket.getInputStream());
- os = new PrintStream(clientSocket.getOutputStream());
- os.println("Enter your name.");
- String name = is.readLine().trim();
- int flagname=0, count=0;
- String namechange=name;
- while(flagname==0){
- flagname=1;
- for (int i = 0; i <maxClientsCount; i++) {
- if (threads[i] != null && namechange.equals(threads[i].Username)) {
- count++;
- namechange=name+"_"+count;
- flagname=0;
- break;
- }
- }
- }
- if(count>0)os.println("Your name is already used, your name changed to "+namechange);
- os.println("Enter your password.");
- String pass = is.readLine().trim();
- this.Username=namechange;
- this.Password=pass;
- this.JoinDate = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(Calendar.getInstance().getTime());
- os.println("Hello " + namechange
- + " to our chat room.\nJoin Date: "+JoinDate+ "\nTo leave enter /quit in a new line\n");
- os.println("Type /help for helps");
- for (int i = 0; i < maxClientsCount; i++) {
- if (threads[i] != null && threads[i] != this) {
- threads[i].os.println("*** A new user " + namechange
- + " entered the chat room !!! ***");
- }
- }
- while (!this.interupt) {
- String line = is.readLine();
- if (line.startsWith("/quit")) {
- break;
- }
- if(line.startsWith("/help")){
- os.println("@kill#name#password to kill a user" );
- os.println("@votekick#name to vote a user to be kicked" );
- os.println("@cek#name to check a user join date" );
- os.println("@join#gorupname to join a group");
- os.println("@group#groupname <chat> to chat in a group");
- os.println("@leave#groupname to leave a group chat");
- os.println("@name to personal chat a user");
- }
- else if (line.startsWith("@kill"))
- {
- String[] User = line.split("#",3);
- boolean flag=false;
- for (int i = 0; i <maxClientsCount; i++) {
- if (threads[i] != null && User[1].equals(threads[i].Username)) {
- if(User[2].equals(threads[i].Password)){
- os.println("You killed "+User[1]);
- threads[i].os.println("< KILL --> " + User[1] + " has been killed. you can send a goodbye message");
- threads[i].interupt=true;
- }
- flag=true;
- break;
- }
- }
- if(flag==false) os.println("<" + namechange + " --> " + User[1] + " not registered");
- }
- else if(line.startsWith("@votekick"))
- {
- String[] User = line.split("#",2);
- boolean flag=false;
- for (int i = 0; i <maxClientsCount; i++) {
- if (threads[i] != null && User[1].equals(threads[i].Username)) {
- os.println("You voted "+User[1]+"to be killed (if there is at least 3 user voted that name, that user will be kicked)");
- threads[i].kick+=1;
- if(threads[i].kick>=3) {
- threads[i].os.println("< VOTE KICK --> " + User[1] + " has been kicked. you can send a goodbye message");
- threads[i].interupt=true;
- }
- flag=true;
- break;
- }
- }
- for (int i = 0; i <maxClientsCount; i++) {
- if(threads[i]!=null && threads[i]!=this && User[1].equals(threads[i].Username) && flag==true)
- 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)");
- }
- if(flag==false) os.println("<" + namechange + " --> " + User[1] + " not registered");
- }
- else if (line.startsWith("@cek"))
- {
- String[] User = line.split("#",2);
- boolean flag=false;
- for (int i = 0; i <maxClientsCount; i++) {
- if (threads[i] != null && User[1].equals(threads[i].Username)) {
- os.println("<" + namechange + " --> " + User[1] + " entered the room chat at " + threads[i].JoinDate);
- flag=true;
- break;
- }
- }
- if(flag==false) os.println("<" + namechange + " --> " + User[1] + " not registered");
- }
- else if(line.startsWith("@join"))
- {
- String[] to = line.split("#",2);
- boolean flag = false;
- int groupnumber = cari(to[1]);
- if(groupnumber != -1 && groupnumber != MultiThreadChatServer.group.length) {
- flag = true;
- this.groups[groupnumber] = true;
- }
- if(flag==true) {
- os.println ("You entered the group" +to[1]);
- for (int i = 0; i < maxClientsCount; i++) {
- if (threads[i] != null && threads[i] != this && threads[i].groups[groupnumber] == true) {
- threads[i].os.println("*** A new user " + name + " entered the " + MultiThreadChatServer.group[groupnumber] + " chat !!! ***");
- }
- }
- }
- else {
- if(groupnumber != MultiThreadChatServer.group.length) {
- for(int i=0;i<MultiThreadChatServer.group.length;i++) {
- if(MultiThreadChatServer.group[i] == null) {
- this.groups[i] = true;
- MultiThreadChatServer.group[i] = to[1];
- os.println("You created a group " + to[1]);
- break;
- }
- }
- }
- else {
- os.println("Maximum group is exceeded");
- }
- }
- }
- else if(line.startsWith("@group"))
- {
- String[] GChat = line.split(" ",2);
- String[] GName = GChat[0].split("#",2);
- int groupnumber = cari(GName[1]);
- if(groupnumber == -1 || groupnumber == MultiThreadChatServer.group.length) {
- os.println("Group "+ GName[1]+" not available");
- }
- else {
- boolean ismember = false;
- for (int i = 0; i < maxClientsCount; i++) {
- if (threads[i] != null && threads[i].groups[groupnumber] == true && this.groups[groupnumber]==true ) {
- ismember = true;
- threads[i].os.println("<" + namechange + " @ " + MultiThreadChatServer.group[groupnumber] + " --> " + GChat[1]);
- }
- }
- if(ismember == false) {
- os.println("You are not member of " + GName[1]);
- }
- }
- }
- else if(line.startsWith("@leave"))
- {
- String[] GName = line.split("#",2);
- int groupnumber = cari(GName[1]);
- if(groupnumber == -1 || groupnumber == MultiThreadChatServer.group.length) {
- os.println("Group "+ GName[1]+" not available");
- }
- else if(groupnumber != -1 && groupnumber != MultiThreadChatServer.group.length) {
- os.println("*** You leave the group "+ GName[1] +" ***");
- this.groups[groupnumber] = false;
- for (int i = 0; i < maxClientsCount; i++) {
- if (threads[i] != null && threads[i].groups[groupnumber] == true ) {
- threads[i].os.println("*** The user " + namechange +" is leaving the group " +GName[1]+ " ***" );
- }
- }
- }
- }
- else if(line.startsWith("@"))
- {
- String[] PC = line.split(" ",2);
- String to = PC[0].substring(1);
- for (int i = 0; i < maxClientsCount; i++) {
- if (threads[i] != null && (to.equals(threads[i].Username) || threads[i] == this)) {
- threads[i].os.println("<" + namechange+ " to "+ to +"--> " + PC[1]);
- }
- }
- }
- else {
- for (int i = 0; i < maxClientsCount; i++) {
- if (threads[i] != null) {
- threads[i].os.println("<" + namechange + "--> " + line);
- }
- }
- }
- }
- for (int i = 0; i < maxClientsCount; i++) {
- if (threads[i] != null && threads[i] != this) {
- threads[i].os.println("*** The user " + namechange
- + " is leaving the chat room !!! ***");
- }
- }
- os.println("*** Bye " + namechange + " ***");
- /*
- * Clean up. Set the current thread variable to null so that a new client
- * could be accepted by the server.
- */
- for (int i = 0; i < maxClientsCount; i++) {
- if (threads[i] == this) {
- threads[i] = null;
- }
- }
- /*
- * Close the output stream, close the input stream, close the socket.
- */
- is.close();
- os.close();
- clientSocket.close();
- } catch (IOException e) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement