Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.io.*;
- import java.util.*;
- public class Server {
- final static int server_port = 9000;
- static ArrayList<ClientThread> activeClients = new ArrayList<>();
- static int numOfClients = 0;
- public static void main(String[] args) throws Exception{
- ServerSocket serverSocket = new ServerSocket(server_port);
- Socket s = null;
- int dali_treba_da_se_vnese_pass = -1;
- while(true) {
- s = serverSocket.accept(); // primi go socketot ispraten od clientot
- System.out.println("New user is trying to connect...");
- DataInputStream input = new DataInputStream(s.getInputStream());
- DataOutputStream output = new DataOutputStream(s.getOutputStream());
- String username = input.readUTF();
- StringTokenizer stringTokenizer = new StringTokenizer(username, ":");
- String s1 = stringTokenizer.nextToken();
- String s2 = stringTokenizer.nextToken();
- boolean ok = false;
- if(s1.equals("name") && s2.length() > 0) {
- ok = true;
- }
- if(!ok) {
- output.writeUTF("Login failed!");
- output.flush();
- continue;
- }
- String password = input.readUTF();
- System.out.println(password);
- StringTokenizer st = new StringTokenizer(password, ":");
- String p1 = st.nextToken();
- p1 = st.nextToken();
- boolean upper_case = false;
- boolean number = false;
- boolean specialChar = false;
- for(int i = 0; i < p1.length(); i++) {
- char c = p1.charAt(i);
- if(c >= 'A' && c <= 'Z') {
- upper_case = true;
- }
- if(c >= '1' && c <= '9') {
- number = true;
- }
- if(c >= '!' && c <= '/') {
- specialChar = true;
- }
- }
- if(upper_case && number && specialChar && p1.length() >= 8) {
- }
- else {
- output.writeUTF("Login failed!");
- output.flush();
- continue;
- }
- System.out.println("User: " + s2 + " is connected!");
- ClientThread cl = new ClientThread(s, s2, input, output);
- activeClients.add(cl);
- cl.start();
- numOfClients++;
- }
- }
- }
- class ClientThread extends Thread {
- private String username;
- private DataInputStream input;
- private DataOutputStream output;
- Socket socket;
- boolean logged_in = false;
- public ClientThread(Socket s, String usname, DataInputStream dis, DataOutputStream dos) {
- socket = s;
- username = usname;
- input = dis;
- output = dos;
- logged_in = true;
- }
- @Override
- public void run() {
- int pass = -1;
- while(true) {
- try {
- if(socket.isConnected()) {
- String recieved = input.readUTF();
- System.out.println(recieved);
- StringTokenizer stringTokenizer = new StringTokenizer(recieved, ":");
- String to_whom = stringTokenizer.nextToken();
- String message = stringTokenizer.nextToken();
- boolean ok = false;
- for(ClientThread client : Server.activeClients) {
- if(client.username.equals(to_whom) && client.logged_in == true) {
- client.output.writeUTF("User " + username + " says: " + message);
- client.output.flush();
- ok = true;
- break;
- }
- }
- if(!ok) {
- output.writeUTF("User not found or logged in!");
- output.flush();
- }
- }
- else {
- break;
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- try{
- input.close();
- output.close();
- }
- catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- }
- // name:fdsjfksdf
- // password:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement