Advertisement
Josif_tepe

Untitled

Apr 29th, 2021
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. import java.net.*;
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. public class Client {
  6.     final static int server_port = 9000;
  7.     final static String ip = "localhost";
  8.  
  9.     static int pass = -1;
  10.  
  11.     public static void main(String[] args) throws IOException {
  12.         Scanner sc = new Scanner(System.in);
  13.         Socket socket = new Socket(ip, server_port);
  14.  
  15.         DataInputStream input = new DataInputStream(socket.getInputStream()); // toa sto go pecati serverot
  16.         DataOutputStream output = new DataOutputStream(socket.getOutputStream()); // toa sto nie go pecatime na serverot
  17.  
  18.         Thread prati_poraka = new Thread(new Runnable() {
  19.             @Override
  20.             public void run() {
  21.                 while (true) {
  22.                     String s = sc.nextLine();
  23.                     try {
  24.                         output.writeUTF(s);
  25.                         output.flush();
  26.                     }
  27.                     catch (IOException ex) {
  28.                         ex.printStackTrace();
  29.                     }
  30.                     if(pass == -1) {
  31.                         pass  = 1;
  32.                         s = sc.nextLine();
  33.                         try {
  34.                             output.writeUTF(s);
  35.                             output.flush();
  36.                         }
  37.                         catch (IOException ex) {
  38.                             ex.printStackTrace();
  39.                         }
  40.                     }
  41.  
  42.                 }
  43.             }
  44.         });
  45.  
  46.         Thread primi_poraka = new Thread(new Runnable() {
  47.             @Override
  48.             public void run() {
  49.                 while(true) {
  50.                     try {
  51.                         String s = input.readUTF();
  52.                         if (s.equals("Login failed!")) {
  53.                             System.out.println("Login failed... Try again...");
  54.                             System.exit(0);
  55.                         }
  56.                         System.out.println(s);
  57.                     } catch (IOException ex) {
  58.                         ex.printStackTrace();
  59.                     }
  60.                 }
  61.             }
  62.         });
  63.  
  64.         prati_poraka.start();
  65.         primi_poraka.start();
  66.  
  67.     }
  68.  
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement