Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- import java.util.*;
- import java.awt.*;
- public class Server implements Runnable
- {
- public static ServerSocket echoServer = null;
- Thread t=new Thread(this);
- Socket clientSocket = null;
- String dirName="C:\\Users\\User\\Desktop\\Offline";
- String [] allowedExt = { ".java",".py" };
- int portNumber = 1234 ;
- String line;
- ArrayList <ClientThread> clients = new ArrayList();
- int clientCount=0;
- public void print(String s) //Defined macro-type function
- {
- System.out.println(s);
- }
- Server()
- {
- try
- {
- echoServer = new ServerSocket(portNumber);
- System.out.println("The server has started at port " + portNumber);
- t.start();
- }
- catch(Exception e)
- {
- System.out.println(e.toString());
- }
- }
- public void run()
- {
- while(true)
- {
- print("Amount of clients: "+ clientCount);
- try
- {
- addThread(echoServer.accept());
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public void addThread(Socket sock)
- {
- clientCount++;
- print("Client number "+clientCount + " just got connected!");
- clients.add(new Server.ClientThread(this,sock)); //creates new thread of new client
- }
- class ClientThread extends Thread
- {
- //variables for the working thread
- Server serv;
- Socket socket = null;
- String portName = "localhost" ;
- int portNumber=1234;
- public final static int FILE_SIZE = 6022386; // file size temporary hard coded
- BufferedReader br;
- PrintWriter bw;
- ClientThread(Server s, Socket c) //constructor
- {
- serv=s; socket=c;
- try
- {
- br=new BufferedReader( new InputStreamReader(socket.getInputStream()) );
- bw = new PrintWriter(socket.getOutputStream());
- //now the main work
- start();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- public void close()
- {
- try
- {
- bw.close();
- socket.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void receiveFile(String fileName, String dirName)
- {
- //print("received file <fake>");
- try
- {
- InputStream is = socket.getInputStream();
- FileOutputStream fos = new FileOutputStream(dirName+"\\"+fileName);
- BufferedOutputStream bos = new BufferedOutputStream(fos);
- //byte[] buffer = new byte[socket.getReceiveBufferSize()];
- byte[] buffer = new byte[512];
- int bytesReceived = 0;
- while((bytesReceived = is.read(buffer))>0)
- {
- /* Write to the file */
- bos.write(buffer,0,bytesReceived);
- bos.flush();
- }
- bos.flush();
- fos.flush();
- bos.close();
- fos.close();
- }
- catch(Exception e)
- {
- print(e.toString());
- }
- }
- public void run()
- {
- System.out.println("Connected to a client!");
- bw.write("Hello from FTP Server"+"\n");
- bw.flush();
- String line;
- try
- {
- print("Getting input from client...");
- line=br.readLine();
- while(true)
- {
- print("Client says: "+line);
- File f1=new File(dirName);
- File fileList [] = f1.listFiles();
- Arrays.sort(fileList);
- int fileCount=0;
- boolean found=false;
- boolean extOK=false;
- String fileExt;
- String fileName=line; //1005047.java
- //checking if file extension matches
- for(int i=0;i<allowedExt.length;i++)
- {
- print("checking "+fileName+ " with " + allowedExt[i]);
- if(fileName.endsWith(allowedExt[i]))
- {
- extOK=true;
- break;
- }
- }
- //checking if file exists
- for(int i=0;i<fileList.length;i++)
- {
- if(fileList[i].canRead())
- {
- fileCount++;
- String fName=fileList[i].getName();
- int pos=fName.indexOf(".");
- fileExt=fName.substring(pos+1);
- print("File name: "+fName +", Ext: "+fileExt + ", File Size:" +fileList[i].length() + "bytes");
- if(fName.equals(line)) found = true;
- }
- }
- print(fileCount + " files found.");
- if(found && extOK)
- {
- print("SEARCHED FILE PRESENT!");
- bw.write("present"+"\n");
- bw.flush();
- line=br.readLine();//response if client wants to send file
- if(line.equals("y"))
- {
- receiveFile(fileName,dirName);
- //break;
- }
- }
- else
- {
- print("SEARCH NOT FOUND");
- bw.write("no"+"\n");
- bw.flush();
- line=br.readLine();//response if client wants to send file
- if(line.equals("y"))
- {
- receiveFile(fileName,dirName);
- //break;
- }
- }
- line=br.readLine();
- if(line.equals("q"))
- {
- print("This user is quitting...");
- break;
- }
- }
- close();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.toString();
- }
- }
- }
- public static void main(String[] args)
- {
- Server s=new Server();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement