Advertisement
Guest User

Untitled

a guest
Feb 1st, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.41 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package server;
  7.  
  8. import java.io.BufferedInputStream;
  9. import java.io.BufferedReader;
  10. import java.io.File;
  11. import java.io.FileInputStream;
  12. import java.io.FileNotFoundException;
  13. import java.io.IOException;
  14. import java.io.InputStreamReader;
  15. import java.io.OutputStream;
  16. import java.io.PrintStream;
  17. import java.net.ServerSocket;
  18. import java.net.Socket;
  19. import java.util.ArrayList;
  20. import java.util.Scanner;
  21. import java.util.concurrent.ExecutorService;
  22. import java.util.concurrent.Executors;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;
  25.  
  26. /**
  27. *
  28. * @author SHUBHAM
  29. */
  30. public class Server {
  31.  
  32. int port;
  33. ServerSocket server=null;
  34. Socket client=null;
  35. ExecutorService pool = null;
  36. int clientcount=0;
  37. static int a,b,c,d,e;
  38. static ArrayList<Socket> clientsock=new ArrayList<Socket>();
  39. public static void broadcast(String message)
  40. {
  41. PrintStream oo = null;
  42. //Socket socket=null;
  43. for(Socket socket:clientsock){
  44.  
  45.  
  46. try {
  47. oo = new PrintStream(socket.getOutputStream(),true);
  48. oo.println(message);
  49. } catch (IOException ex) {
  50. Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
  51. }
  52. }
  53. oo.close();
  54. }
  55.  
  56.  
  57. public static void main(String[] args) throws IOException {
  58. Server serverobj=new Server(5000);
  59. serverobj.startServer();
  60. }
  61.  
  62. Server(int port){
  63. this.port=port;
  64. pool = Executors.newFixedThreadPool(5);
  65. }
  66.  
  67. public void startServer() throws IOException {
  68.  
  69. server=new ServerSocket(5000);
  70. System.out.println("Server Booted");
  71. System.out.println("Any client can stop the server by sending -1");
  72.  
  73. while(true)
  74. {
  75. client=server.accept();
  76. clientcount++;
  77. clientsock.add(client);
  78. ServerThread runnable= new ServerThread(client,clientcount,this);
  79. pool.execute(runnable);
  80. }}
  81.  
  82.  
  83.  
  84. private static class ServerThread implements Runnable {
  85.  
  86. Server server=null;
  87. Socket client=null;
  88. BufferedReader cin;
  89. PrintStream cout;
  90. Scanner sc=new Scanner(System.in);
  91. int id;
  92. String s;
  93.  
  94. ServerThread(Socket client, int count ,Server server ) throws IOException {
  95.  
  96. this.client=client;
  97. this.server=server;
  98. this.id=count;
  99. System.out.println("Connection "+id+"established with client "+client);
  100.  
  101. cin=new BufferedReader(new InputStreamReader(client.getInputStream()));
  102. cout=new PrintStream(client.getOutputStream());
  103.  
  104. }
  105.  
  106.  
  107. @Override
  108. public void run() {
  109. int x=1;
  110. int f=0;
  111.  
  112.  
  113. try{
  114. while(true){
  115. s=cin.readLine();
  116.  
  117. //BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
  118. File login=new File("login.txt");
  119. Scanner ss=new Scanner(login);
  120. while(ss.hasNext()&& f==0){
  121. String name=ss.next();
  122. if(name.equals(s)){
  123. f=10;
  124. System.out.println("logged in");
  125. if(id==1) a=1;
  126. else if(id==2) b=1;
  127. else if(id==3) c=1;
  128. else if(id==4) d=1;
  129. else if(id==5) e=1;
  130.  
  131. }
  132. }
  133. if(f==10) cout.println("login successfull");
  134. // if(f==0) cout.println("login failed");
  135. else if(s.equals("logout")&&f==10)
  136. {
  137. System.out.println("client"+id+" logged out");
  138. //cout.println("logout");
  139. if(id==1) a=0;
  140. else if(id==2) b=0;
  141. else if(id==3) c=0;
  142. else if(id==4) d=0;
  143. else if(id==5) e=0;
  144. }
  145. else if(s.equals("show")&& f==10)
  146. {
  147. if(a==1) System.out.println("suba logged in");
  148. else System.out.println("suba not logged in");
  149. if(b==1) System.out.println("raisa logged in");
  150. else System.out.println("raisa not logged in");
  151. if(c==1) System.out.println("muna logged in");
  152. else System.out.println("muna not logged in");
  153. if(d==1) System.out.println("orni logged in");
  154. else System.out.println("orni not logged in");
  155. if(e==1) System.out.println("sadia logged in");
  156. else System.out.println("sadia not logged in");
  157.  
  158.  
  159. }
  160. System. out.print("Client("+id+") :"+s+"\n");
  161. System.out.print("Server : ");
  162. //s=stdin.readLine();
  163. // s=sc.nextLine();
  164. if (s.equalsIgnoreCase("bye"))
  165. {
  166. cout.println("BYE");
  167. x=0;
  168. System.out.println("Connection ended by server");
  169. break;
  170. }
  171. //cout.println(s);
  172. else if(s.contains("#b")) {
  173. String q=s.substring(2);
  174. broadcast(q);
  175. }
  176. else if(s.equals("DL"))
  177. {
  178. try
  179. {
  180. File file = new File("login.txt");
  181. FileInputStream fis = new FileInputStream(file);
  182. BufferedInputStream bis = new BufferedInputStream(fis);
  183. OutputStream os = client.getOutputStream();
  184. byte[] contents;
  185. long fileLength = file.length();
  186. cout.println(String.valueOf(fileLength)); //These two lines are used
  187. cout.flush(); //to send the file size in bytes.
  188.  
  189. long current = 0;
  190.  
  191. while(current!=fileLength){
  192. int size = 10000;
  193. if(fileLength - current >= size)
  194. current += size;
  195. else{
  196. size = (int)(fileLength - current);
  197. current = fileLength;
  198. }
  199. contents = new byte[size];
  200. bis.read(contents, 0, size);
  201. os.write(contents);
  202. //System.out.println("Sending file ... "+(current*100)/fileLength+"% complete!");
  203. }
  204. os.flush();
  205. System.out.println("File sent successfully!");
  206. }
  207. catch(Exception e)
  208. {
  209. System.err.println("Could not transfer file.");
  210. }
  211. cout.println("Downloaded.");
  212. cout.flush();
  213.  
  214. }
  215. else
  216. {
  217. // System.out.println("[" + id + "] says: " + str);
  218. cout.println("Got it. You sent");
  219. cout.flush();
  220. }
  221. }
  222. }
  223.  
  224. catch(IOException ex){
  225. System.out.println("Error : "+ex);
  226. }
  227.  
  228.  
  229. try {
  230. cin.close();
  231. } catch (IOException ex) {
  232. Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
  233. }
  234. try {
  235. client.close();
  236. } catch (IOException ex) {
  237. Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
  238. }
  239. cout.close();
  240. if(x==0) {
  241. System.out.println( "Server cleaning up." );
  242. System.exit(0);
  243. }
  244. }
  245. }
  246. }
  247. /*
  248. * To change this license header, choose License Headers in Project Properties.
  249. * To change this template file, choose Tools | Templates
  250. * and open the template in the editor.
  251. */
  252. package client;
  253.  
  254. import java.io.BufferedOutputStream;
  255. import java.io.BufferedReader;
  256. import java.io.FileOutputStream;
  257. import java.io.InputStream;
  258. import java.io.InputStreamReader;
  259. import java.io.PrintStream;
  260. import java.net.ServerSocket;
  261. import java.net.Socket;
  262. import java.util.Scanner;
  263. /**
  264. *
  265. * @author SHUBHAM
  266. */
  267. public class Client {
  268.  
  269. public static void main(String args[]) throws Exception
  270. {
  271. Socket sk=new Socket("127.0.0.1",5000);
  272. BufferedReader sin=new BufferedReader(new InputStreamReader(sk.getInputStream()));
  273. PrintStream sout=new PrintStream(sk.getOutputStream());
  274. BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
  275. String s;
  276. int f2=0;
  277. Scanner ss=new Scanner(System.in);
  278. String who=null;
  279. while ( true )
  280. {
  281. if(f2==10) System.out.println("enter your msg type");
  282. if(f2==0) System.out.println("please login");
  283. System.out.print("Client : ");
  284. s=stdin.readLine();
  285. if(s.equals("#l"))
  286. {
  287.  
  288. System.out.println("enter your name\n");
  289. String n=ss.nextLine();
  290.  
  291. System.out.println("password\n");
  292. String p=ss.nextLine();
  293. System.out.println("TYPE\n");
  294. String t=ss.nextLine();
  295. lmessage login=new lmessage(n,p,t);
  296. sout.println(login.un());
  297. p=sin.readLine();
  298. System.out.println(p);
  299. if(p.equals("login successfull")) f2=10;
  300. who=login.tt();
  301. // sin.reset();
  302. }
  303. if(s.contains("#s")&&f2==10)
  304. {
  305. //System.out.println("COMMAND");
  306. String cc=s.substring(2);
  307. smessage com=new smessage(cc);
  308. sout.println(com.sm());
  309. //String d=sin.readLine();
  310. if(cc.equals("logout")) f2=0;
  311. }
  312. if(s.contains("#b"))
  313. {
  314. if(f2==0) System.out.println("you are not logged in");
  315. else if(who.equals("admin"))
  316. {
  317. System.out.println("TYPE YOUR TXT");
  318. String d=ss.nextLine();
  319. bmessage bb=new bmessage(d);
  320. sout.println(bb.mm());
  321. //sin.notifyAll();
  322. }
  323. else System.out.println("you are not admin");
  324. }
  325.  
  326.  
  327. if ( s.equalsIgnoreCase("BYE") )
  328. {
  329. System.out.println("Connection ended by client");
  330. break;
  331. }
  332. if(s.equals("DL"))
  333. {
  334.  
  335. try
  336. {
  337. String strRecv = sin.readLine(); //These two lines are used to determine
  338. int filesize=Integer.parseInt(strRecv); //the size of the receiving file
  339. byte[] contents = new byte[10000];
  340.  
  341. FileOutputStream fos = new FileOutputStream("tt.txt");
  342. BufferedOutputStream bos = new BufferedOutputStream(fos);
  343. InputStream is = sk.getInputStream();
  344.  
  345. int bytesRead = 0;
  346. int total=0; //how many bytes read
  347.  
  348. while(total!=filesize) //loop is continued until received byte=totalfilesize
  349. {
  350. bytesRead=is.read(contents);
  351. total+=bytesRead;
  352. bos.write(contents, 0, bytesRead);
  353. }
  354. bos.flush();
  355. }
  356. catch(Exception e)
  357. {
  358. System.err.println("Could not transfer file.");
  359. }
  360.  
  361. }
  362.  
  363.  
  364. //String ws=sin.readLine();
  365. // System.out.print("Server : "+ws+"\n");
  366.  
  367. }
  368. sk.close();
  369. sin.close();
  370. sout.close();
  371. stdin.close();
  372. }
  373.  
  374. }
  375. /*
  376. * To change this license header, choose License Headers in Project Properties.
  377. * To change this template file, choose Tools | Templates
  378. * and open the template in the editor.
  379. */
  380. package client;
  381.  
  382. /**
  383. *
  384. * @author AKM
  385. */
  386. public class lmessage {
  387. private String username;
  388. private String password;
  389. private String type;
  390. public lmessage(String u,String p,String t)
  391. {
  392. username=u;
  393. password=p;
  394. type=t;
  395. }
  396. public String un()
  397. {
  398. StringBuilder mm=new StringBuilder();
  399. mm.append(username);
  400. mm.append(password);
  401. mm.append(type);
  402. return mm.toString();
  403. }
  404. public String tt()
  405. {
  406. return type;
  407. }
  408.  
  409.  
  410. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement