lolipop12

[OS] Java Networking Ispit

Jan 19th, 2020
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.86 KB | None | 0 0
  1. //https://imgur.com/a/2WX45MO?fbclid=IwAR2de4_7KK_AyW1BoOQADI1MT1gk0oodY9fOJDJnsLKmdQIj9ykL0a7pg1c
  2.  
  3. import java.io.*;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6. import java.util.concurrent.Semaphore;
  7.  
  8. public class TCPServer {
  9.     private ServerSocket server;
  10.     private File client_data;
  11.  
  12.     public TCPServer(File client_data) {
  13.         this.server = new ServerSocket(3398);
  14.         this.client_data = client_data;
  15.     }
  16.     public void takeRequests() throws IOException {
  17.         while(true){
  18.             Socket client=this.server.accept();
  19.             ServerWorkerThread swt=new ServerWorkerThread(client_data,client);
  20.             swt.start();
  21.  
  22.  
  23.         }
  24.     }
  25.     static class ServerWorkerThread extends  Thread{
  26.         private File client_data;
  27.         private Socket client;
  28.         private static Semaphore semaphore= new Semaphore(1);
  29.         private PrintWriter pw;
  30.  
  31.         public ServerWorkerThread( File client_data, Socket client) {
  32.             this.client_data = client_data;
  33.             this.client = client;
  34.         }
  35.  
  36.         @Override
  37.         public void run() {
  38. //            super.run();
  39.             try {
  40.                 DataInputStream dis=new DataInputStream(client.getInputStream());
  41.                 int count=dis.readInt();
  42.                 String row=client.getInetAddress().getHostAddress()+" "+client.getPort()+" "+ count;
  43.                 PrintWriter pw=new PrintWriter(new FileWriter(client_data,true);
  44.                 semaphore.acquire();
  45.                 pw.println(count);
  46.                 pw.flush();
  47.                 semaphore.release();
  48.             } catch (IOException | InterruptedException e) {
  49.                 e.printStackTrace();
  50.             }
  51.         }
  52.     }
  53. }
  54. import java.io.*;
  55. import java.net.InetAddress;
  56. import java.net.Socket;
  57.  
  58. public class TCPClien extends Thread {
  59.     private PrintWriter pw;
  60.     private Socket connection;
  61.     private File FolderOutput;
  62.     private File otdFile;
  63.  
  64.     public TCPClien(File folderOutput) throws IOException {
  65.         this.FolderOutput = folderOutput;
  66.         this.connection=new Socket(InetAddress.getLocalHost(),3398);
  67.         this.otdFile=new File(FolderOutput,"files.otd");
  68.         this.otdFile.createNewFile();
  69.         this.pw=new PrintWriter(new FileWriter(otdFile,true));
  70.     }
  71.    
  72.     public int findFiles(File fileToSearch) throws IOException {
  73.         File [] files=fileToSearch.listFiles();
  74.         int ChCount=0;
  75.         for (File f:files){
  76.             if(f.isFile()){
  77.                 BufferedReader bf=new BufferedReader((new FileReader(f)));
  78.  
  79.                 if((f.getName().endsWith(".txt") || f.getName().endsWith(".otd")) && (f.length()>(10*1024) && f.length()<(100*1024))) {
  80.                     String currentLine=bf.readLine();
  81.                     while(currentLine!=null){
  82.                     String [] words=currentLine.split(" ");
  83.                      for(String word : words){
  84.                     ChCount+=word.length();
  85.                     currentLine=bf.readLine();
  86.  
  87.                 }
  88.                     pw.println(currentLine);
  89.                     pw.flush();
  90.                 }
  91.                 }
  92.  
  93.  
  94.             }
  95.             else if(f.isDirectory()){
  96.                 ChCount+=findFiles(f);
  97.             }
  98.         }
  99.         return ChCount;
  100.     }
  101.     public void SendToServer() throws IOException {
  102.         int suma=findFiles(otdFile);
  103.         DataOutputStream dos=new DataOutputStream(connection.getOutputStream());
  104.         dos.write(suma);
  105.         dos.flush();
  106.     }
  107.  
  108.     @Override
  109.     public void run() {
  110.  
  111.         try {
  112.             findFiles(new File("neshto.txt");
  113.         } catch (IOException e) {
  114.             e.printStackTrace();
  115.         }
  116.         try {
  117.            SendToServer();
  118.         } catch (IOException e) {
  119.             e.printStackTrace();
  120.         }
  121.     }
  122.  
  123.     public static void main(String[] args) {
  124.  
  125.     }
  126. }
Add Comment
Please, Sign In to add comment