Advertisement
Josif_tepe

Untitled

Apr 29th, 2021
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.50 KB | None | 0 0
  1. import java.net.*;
  2. import java.io.*;
  3. import java.util.*;
  4. public class Server {
  5.     final static int server_port = 9000;
  6.    static ArrayList<ClientThread> activeClients = new ArrayList<>();
  7.    static int numOfClients = 0;
  8.     public static void main(String[] args) throws Exception{
  9.         ServerSocket serverSocket = new ServerSocket(server_port);
  10.         Socket s = null;
  11.         int dali_treba_da_se_vnese_pass = -1;
  12.         while(true) {
  13.             s = serverSocket.accept(); // primi go socketot ispraten od clientot
  14.             System.out.println("New user is trying to connect...");
  15.  
  16.             DataInputStream input = new DataInputStream(s.getInputStream());
  17.             DataOutputStream output = new DataOutputStream(s.getOutputStream());
  18.  
  19.             String username = input.readUTF();
  20.             StringTokenizer stringTokenizer = new StringTokenizer(username, ":");
  21.             String s1 = stringTokenizer.nextToken();
  22.             String s2 = stringTokenizer.nextToken();
  23.              boolean ok = false;
  24.             if(s1.equals("name") && s2.length() > 0) {
  25.                 ok = true;
  26.             }
  27.             if(!ok) {
  28.                 output.writeUTF("Login failed!");
  29.                 output.flush();
  30.                 continue;
  31.             }
  32.                 String password = input.readUTF();
  33.             System.out.println(password);
  34.                 StringTokenizer st = new StringTokenizer(password, ":");
  35.                 String p1 = st.nextToken();
  36.                 p1 = st.nextToken();
  37.                 boolean upper_case = false;
  38.                 boolean number = false;
  39.                 boolean specialChar = false;
  40.                 for(int i = 0; i < p1.length(); i++) {
  41.                     char c = p1.charAt(i);
  42.                     if(c >= 'A' && c <= 'Z') {
  43.                       upper_case = true;
  44.                     }
  45.                     if(c >= '1' && c <= '9') {
  46.                         number = true;
  47.                     }
  48.                     if(c >= '!' && c <= '/') {
  49.                         specialChar = true;
  50.                     }
  51.                 }
  52.                 if(upper_case && number && specialChar && p1.length() >= 8) {
  53.  
  54.                 }
  55.                 else {
  56.                     output.writeUTF("Login failed!");
  57.                     output.flush();
  58.                     continue;
  59.                 }
  60.  
  61.             System.out.println("User: " + s2 + " is connected!");
  62.             ClientThread cl = new ClientThread(s, s2, input, output);
  63.             activeClients.add(cl);
  64.             cl.start();
  65.             numOfClients++;
  66.         }
  67.     }
  68.  
  69. }
  70.  
  71. class ClientThread extends Thread {
  72.     private String username;
  73.      private DataInputStream input;
  74.     private DataOutputStream output;
  75.     Socket socket;
  76.     boolean logged_in = false;
  77.  
  78.     public ClientThread(Socket s, String usname, DataInputStream dis, DataOutputStream dos) {
  79.         socket = s;
  80.         username = usname;
  81.         input = dis;
  82.         output = dos;
  83.         logged_in = true;
  84.     }
  85.     @Override
  86.     public void run() {
  87.         int pass = -1;
  88.         while(true) {
  89.             try {
  90.                 if(socket.isConnected()) {
  91.                     String recieved = input.readUTF();
  92.                     System.out.println(recieved);
  93.                     StringTokenizer stringTokenizer = new StringTokenizer(recieved, ":");
  94.                     String to_whom = stringTokenizer.nextToken();
  95.                     String message = stringTokenizer.nextToken();
  96.                     boolean ok = false;
  97.                     for(ClientThread client : Server.activeClients) {
  98.                         if(client.username.equals(to_whom) && client.logged_in == true) {
  99.                             client.output.writeUTF("User " + username +  " says: " + message);
  100.                             client.output.flush();
  101.                             ok = true;
  102.                             break;
  103.                         }
  104.                     }
  105.                     if(!ok) {
  106.                         output.writeUTF("User not found or logged in!");
  107.                         output.flush();
  108.                     }
  109.                 }
  110.                 else {
  111.                     break;
  112.                 }
  113.             }
  114.             catch (Exception e) {
  115.                 e.printStackTrace();
  116.             }
  117.         }
  118.         try{
  119.             input.close();
  120.             output.close();
  121.         }
  122.         catch (IOException ex) {
  123.             ex.printStackTrace();
  124.         }
  125.     }
  126. }
  127.  
  128. // name:fdsjfksdf
  129. // password:
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement