Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //https://imgur.com/a/2WX45MO?fbclid=IwAR2de4_7KK_AyW1BoOQADI1MT1gk0oodY9fOJDJnsLKmdQIj9ykL0a7pg1c
- import java.io.*;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.concurrent.Semaphore;
- public class TCPServer {
- private ServerSocket server;
- private File client_data;
- public TCPServer(File client_data) {
- this.server = new ServerSocket(3398);
- this.client_data = client_data;
- }
- public void takeRequests() throws IOException {
- while(true){
- Socket client=this.server.accept();
- ServerWorkerThread swt=new ServerWorkerThread(client_data,client);
- swt.start();
- }
- }
- static class ServerWorkerThread extends Thread{
- private File client_data;
- private Socket client;
- private static Semaphore semaphore= new Semaphore(1);
- private PrintWriter pw;
- public ServerWorkerThread( File client_data, Socket client) {
- this.client_data = client_data;
- this.client = client;
- }
- @Override
- public void run() {
- // super.run();
- try {
- DataInputStream dis=new DataInputStream(client.getInputStream());
- int count=dis.readInt();
- String row=client.getInetAddress().getHostAddress()+" "+client.getPort()+" "+ count;
- PrintWriter pw=new PrintWriter(new FileWriter(client_data,true);
- semaphore.acquire();
- pw.println(count);
- pw.flush();
- semaphore.release();
- } catch (IOException | InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- import java.io.*;
- import java.net.InetAddress;
- import java.net.Socket;
- public class TCPClien extends Thread {
- private PrintWriter pw;
- private Socket connection;
- private File FolderOutput;
- private File otdFile;
- public TCPClien(File folderOutput) throws IOException {
- this.FolderOutput = folderOutput;
- this.connection=new Socket(InetAddress.getLocalHost(),3398);
- this.otdFile=new File(FolderOutput,"files.otd");
- this.otdFile.createNewFile();
- this.pw=new PrintWriter(new FileWriter(otdFile,true));
- }
- public int findFiles(File fileToSearch) throws IOException {
- File [] files=fileToSearch.listFiles();
- int ChCount=0;
- for (File f:files){
- if(f.isFile()){
- BufferedReader bf=new BufferedReader((new FileReader(f)));
- if((f.getName().endsWith(".txt") || f.getName().endsWith(".otd")) && (f.length()>(10*1024) && f.length()<(100*1024))) {
- String currentLine=bf.readLine();
- while(currentLine!=null){
- String [] words=currentLine.split(" ");
- for(String word : words){
- ChCount+=word.length();
- currentLine=bf.readLine();
- }
- pw.println(currentLine);
- pw.flush();
- }
- }
- }
- else if(f.isDirectory()){
- ChCount+=findFiles(f);
- }
- }
- return ChCount;
- }
- public void SendToServer() throws IOException {
- int suma=findFiles(otdFile);
- DataOutputStream dos=new DataOutputStream(connection.getOutputStream());
- dos.write(suma);
- dos.flush();
- }
- @Override
- public void run() {
- try {
- findFiles(new File("neshto.txt");
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- SendToServer();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- }
- }
Add Comment
Please, Sign In to add comment