Advertisement
Guest User

Untitled

a guest
Aug 30th, 2014
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import java.net.*;
  4. import java.util.*;
  5. import java.awt.*;
  6.  
  7. public class Server implements Runnable
  8. {
  9. public static ServerSocket echoServer = null;
  10. Thread t=new Thread(this);
  11.  
  12. Socket clientSocket = null;
  13.  
  14. String dirName="C:\\Users\\User\\Desktop\\Offline";
  15. String [] allowedExt = { ".java",".py" };
  16. int portNumber = 1234 ;
  17. String line;
  18.  
  19. ArrayList <ClientThread> clients = new ArrayList();
  20. int clientCount=0;
  21.  
  22. public void print(String s) //Defined macro-type function
  23. {
  24. System.out.println(s);
  25. }
  26.  
  27. Server()
  28. {
  29. try
  30. {
  31. echoServer = new ServerSocket(portNumber);
  32. System.out.println("The server has started at port " + portNumber);
  33. t.start();
  34. }
  35. catch(Exception e)
  36. {
  37. System.out.println(e.toString());
  38. }
  39. }
  40.  
  41. public void run()
  42. {
  43. while(true)
  44. {
  45. print("Amount of clients: "+ clientCount);
  46. try
  47. {
  48. addThread(echoServer.accept());
  49. } catch (IOException e) {
  50. // TODO Auto-generated catch block
  51. e.printStackTrace();
  52. }
  53. }
  54. }
  55.  
  56. public void addThread(Socket sock)
  57. {
  58. clientCount++;
  59. print("Client number "+clientCount + " just got connected!");
  60. clients.add(new Server.ClientThread(this,sock)); //creates new thread of new client
  61. }
  62.  
  63. class ClientThread extends Thread
  64. {
  65. //variables for the working thread
  66. Server serv;
  67. Socket socket = null;
  68. String portName = "localhost" ;
  69. int portNumber=1234;
  70. public final static int FILE_SIZE = 6022386; // file size temporary hard coded
  71.  
  72. BufferedReader br;
  73. PrintWriter bw;
  74.  
  75. ClientThread(Server s, Socket c) //constructor
  76. {
  77. serv=s; socket=c;
  78.  
  79. try
  80. {
  81. br=new BufferedReader( new InputStreamReader(socket.getInputStream()) );
  82. bw = new PrintWriter(socket.getOutputStream());
  83.  
  84. //now the main work
  85. start();
  86.  
  87. }
  88. catch (IOException e)
  89. {
  90. e.printStackTrace();
  91. }
  92.  
  93. }
  94.  
  95. public void close()
  96. {
  97. try
  98. {
  99. bw.close();
  100. socket.close();
  101. } catch (IOException e) {
  102. // TODO Auto-generated catch block
  103. e.printStackTrace();
  104. }
  105. }
  106.  
  107. public void receiveFile(String fileName, String dirName)
  108. {
  109. //print("received file <fake>");
  110. try
  111. {
  112. InputStream is = socket.getInputStream();
  113. FileOutputStream fos = new FileOutputStream(dirName+"\\"+fileName);
  114. BufferedOutputStream bos = new BufferedOutputStream(fos);
  115.  
  116. //byte[] buffer = new byte[socket.getReceiveBufferSize()];
  117. byte[] buffer = new byte[512];
  118.  
  119. int bytesReceived = 0;
  120.  
  121. while((bytesReceived = is.read(buffer))>0)
  122. {
  123. /* Write to the file */
  124. bos.write(buffer,0,bytesReceived);
  125. bos.flush();
  126. }
  127.  
  128. bos.flush();
  129. fos.flush();
  130. bos.close();
  131. fos.close();
  132. }
  133. catch(Exception e)
  134. {
  135. print(e.toString());
  136. }
  137.  
  138. }
  139.  
  140. public void run()
  141. {
  142. System.out.println("Connected to a client!");
  143. bw.write("Hello from FTP Server"+"\n");
  144. bw.flush();
  145.  
  146. String line;
  147. try
  148. {
  149. print("Getting input from client...");
  150. line=br.readLine();
  151.  
  152. while(true)
  153. {
  154. print("Client says: "+line);
  155.  
  156. File f1=new File(dirName);
  157. File fileList [] = f1.listFiles();
  158.  
  159. Arrays.sort(fileList);
  160.  
  161. int fileCount=0;
  162. boolean found=false;
  163. boolean extOK=false;
  164. String fileExt;
  165. String fileName=line; //1005047.java
  166.  
  167. //checking if file extension matches
  168. for(int i=0;i<allowedExt.length;i++)
  169. {
  170. print("checking "+fileName+ " with " + allowedExt[i]);
  171. if(fileName.endsWith(allowedExt[i]))
  172. {
  173. extOK=true;
  174. break;
  175. }
  176. }
  177.  
  178. //checking if file exists
  179. for(int i=0;i<fileList.length;i++)
  180. {
  181. if(fileList[i].canRead())
  182. {
  183. fileCount++;
  184. String fName=fileList[i].getName();
  185.  
  186. int pos=fName.indexOf(".");
  187.  
  188. fileExt=fName.substring(pos+1);
  189. print("File name: "+fName +", Ext: "+fileExt + ", File Size:" +fileList[i].length() + "bytes");
  190. if(fName.equals(line)) found = true;
  191. }
  192. }
  193.  
  194. print(fileCount + " files found.");
  195. if(found && extOK)
  196. {
  197. print("SEARCHED FILE PRESENT!");
  198. bw.write("present"+"\n");
  199. bw.flush();
  200.  
  201. line=br.readLine();//response if client wants to send file
  202. if(line.equals("y"))
  203. {
  204. receiveFile(fileName,dirName);
  205. //break;
  206. }
  207. }
  208. else
  209. {
  210. print("SEARCH NOT FOUND");
  211. bw.write("no"+"\n");
  212. bw.flush();
  213.  
  214. line=br.readLine();//response if client wants to send file
  215. if(line.equals("y"))
  216. {
  217. receiveFile(fileName,dirName);
  218. //break;
  219. }
  220. }
  221.  
  222. line=br.readLine();
  223. if(line.equals("q"))
  224. {
  225. print("This user is quitting...");
  226. break;
  227. }
  228. }
  229. close();
  230.  
  231.  
  232. } catch (Exception e) {
  233. // TODO Auto-generated catch block
  234. e.toString();
  235. }
  236. }
  237. }
  238.  
  239.  
  240. public static void main(String[] args)
  241. {
  242. Server s=new Server();
  243. }
  244.  
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement