Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package server;
- import java.io.BufferedInputStream;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.PrintStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.ArrayList;
- import java.util.Scanner;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author SHUBHAM
- */
- public class Server {
- int port;
- ServerSocket server=null;
- Socket client=null;
- ExecutorService pool = null;
- int clientcount=0;
- static int a,b,c,d,e;
- static ArrayList<Socket> clientsock=new ArrayList<Socket>();
- public static void broadcast(String message)
- {
- PrintStream oo = null;
- //Socket socket=null;
- for(Socket socket:clientsock){
- try {
- oo = new PrintStream(socket.getOutputStream(),true);
- oo.println(message);
- } catch (IOException ex) {
- Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- oo.close();
- }
- public static void main(String[] args) throws IOException {
- Server serverobj=new Server(5000);
- serverobj.startServer();
- }
- Server(int port){
- this.port=port;
- pool = Executors.newFixedThreadPool(5);
- }
- public void startServer() throws IOException {
- server=new ServerSocket(5000);
- System.out.println("Server Booted");
- System.out.println("Any client can stop the server by sending -1");
- while(true)
- {
- client=server.accept();
- clientcount++;
- clientsock.add(client);
- ServerThread runnable= new ServerThread(client,clientcount,this);
- pool.execute(runnable);
- }}
- private static class ServerThread implements Runnable {
- Server server=null;
- Socket client=null;
- BufferedReader cin;
- PrintStream cout;
- Scanner sc=new Scanner(System.in);
- int id;
- String s;
- ServerThread(Socket client, int count ,Server server ) throws IOException {
- this.client=client;
- this.server=server;
- this.id=count;
- System.out.println("Connection "+id+"established with client "+client);
- cin=new BufferedReader(new InputStreamReader(client.getInputStream()));
- cout=new PrintStream(client.getOutputStream());
- }
- @Override
- public void run() {
- int x=1;
- int f=0;
- try{
- while(true){
- s=cin.readLine();
- //BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
- File login=new File("login.txt");
- Scanner ss=new Scanner(login);
- while(ss.hasNext()&& f==0){
- String name=ss.next();
- if(name.equals(s)){
- f=10;
- System.out.println("logged in");
- if(id==1) a=1;
- else if(id==2) b=1;
- else if(id==3) c=1;
- else if(id==4) d=1;
- else if(id==5) e=1;
- }
- }
- if(f==10) cout.println("login successfull");
- // if(f==0) cout.println("login failed");
- else if(s.equals("logout")&&f==10)
- {
- System.out.println("client"+id+" logged out");
- //cout.println("logout");
- if(id==1) a=0;
- else if(id==2) b=0;
- else if(id==3) c=0;
- else if(id==4) d=0;
- else if(id==5) e=0;
- }
- else if(s.equals("show")&& f==10)
- {
- if(a==1) System.out.println("suba logged in");
- else System.out.println("suba not logged in");
- if(b==1) System.out.println("raisa logged in");
- else System.out.println("raisa not logged in");
- if(c==1) System.out.println("muna logged in");
- else System.out.println("muna not logged in");
- if(d==1) System.out.println("orni logged in");
- else System.out.println("orni not logged in");
- if(e==1) System.out.println("sadia logged in");
- else System.out.println("sadia not logged in");
- }
- System. out.print("Client("+id+") :"+s+"\n");
- System.out.print("Server : ");
- //s=stdin.readLine();
- // s=sc.nextLine();
- if (s.equalsIgnoreCase("bye"))
- {
- cout.println("BYE");
- x=0;
- System.out.println("Connection ended by server");
- break;
- }
- //cout.println(s);
- else if(s.contains("#b")) {
- String q=s.substring(2);
- broadcast(q);
- }
- else if(s.equals("DL"))
- {
- try
- {
- File file = new File("login.txt");
- FileInputStream fis = new FileInputStream(file);
- BufferedInputStream bis = new BufferedInputStream(fis);
- OutputStream os = client.getOutputStream();
- byte[] contents;
- long fileLength = file.length();
- cout.println(String.valueOf(fileLength)); //These two lines are used
- cout.flush(); //to send the file size in bytes.
- long current = 0;
- while(current!=fileLength){
- int size = 10000;
- if(fileLength - current >= size)
- current += size;
- else{
- size = (int)(fileLength - current);
- current = fileLength;
- }
- contents = new byte[size];
- bis.read(contents, 0, size);
- os.write(contents);
- //System.out.println("Sending file ... "+(current*100)/fileLength+"% complete!");
- }
- os.flush();
- System.out.println("File sent successfully!");
- }
- catch(Exception e)
- {
- System.err.println("Could not transfer file.");
- }
- cout.println("Downloaded.");
- cout.flush();
- }
- else
- {
- // System.out.println("[" + id + "] says: " + str);
- cout.println("Got it. You sent");
- cout.flush();
- }
- }
- }
- catch(IOException ex){
- System.out.println("Error : "+ex);
- }
- try {
- cin.close();
- } catch (IOException ex) {
- Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
- }
- try {
- client.close();
- } catch (IOException ex) {
- Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
- }
- cout.close();
- if(x==0) {
- System.out.println( "Server cleaning up." );
- System.exit(0);
- }
- }
- }
- }
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package client;
- import java.io.BufferedOutputStream;
- import java.io.BufferedReader;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.PrintStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.Scanner;
- /**
- *
- * @author SHUBHAM
- */
- public class Client {
- public static void main(String args[]) throws Exception
- {
- Socket sk=new Socket("127.0.0.1",5000);
- BufferedReader sin=new BufferedReader(new InputStreamReader(sk.getInputStream()));
- PrintStream sout=new PrintStream(sk.getOutputStream());
- BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
- String s;
- int f2=0;
- Scanner ss=new Scanner(System.in);
- String who=null;
- while ( true )
- {
- if(f2==10) System.out.println("enter your msg type");
- if(f2==0) System.out.println("please login");
- System.out.print("Client : ");
- s=stdin.readLine();
- if(s.equals("#l"))
- {
- System.out.println("enter your name\n");
- String n=ss.nextLine();
- System.out.println("password\n");
- String p=ss.nextLine();
- System.out.println("TYPE\n");
- String t=ss.nextLine();
- lmessage login=new lmessage(n,p,t);
- sout.println(login.un());
- p=sin.readLine();
- System.out.println(p);
- if(p.equals("login successfull")) f2=10;
- who=login.tt();
- // sin.reset();
- }
- if(s.contains("#s")&&f2==10)
- {
- //System.out.println("COMMAND");
- String cc=s.substring(2);
- smessage com=new smessage(cc);
- sout.println(com.sm());
- //String d=sin.readLine();
- if(cc.equals("logout")) f2=0;
- }
- if(s.contains("#b"))
- {
- if(f2==0) System.out.println("you are not logged in");
- else if(who.equals("admin"))
- {
- System.out.println("TYPE YOUR TXT");
- String d=ss.nextLine();
- bmessage bb=new bmessage(d);
- sout.println(bb.mm());
- //sin.notifyAll();
- }
- else System.out.println("you are not admin");
- }
- if ( s.equalsIgnoreCase("BYE") )
- {
- System.out.println("Connection ended by client");
- break;
- }
- if(s.equals("DL"))
- {
- try
- {
- String strRecv = sin.readLine(); //These two lines are used to determine
- int filesize=Integer.parseInt(strRecv); //the size of the receiving file
- byte[] contents = new byte[10000];
- FileOutputStream fos = new FileOutputStream("tt.txt");
- BufferedOutputStream bos = new BufferedOutputStream(fos);
- InputStream is = sk.getInputStream();
- int bytesRead = 0;
- int total=0; //how many bytes read
- while(total!=filesize) //loop is continued until received byte=totalfilesize
- {
- bytesRead=is.read(contents);
- total+=bytesRead;
- bos.write(contents, 0, bytesRead);
- }
- bos.flush();
- }
- catch(Exception e)
- {
- System.err.println("Could not transfer file.");
- }
- }
- //String ws=sin.readLine();
- // System.out.print("Server : "+ws+"\n");
- }
- sk.close();
- sin.close();
- sout.close();
- stdin.close();
- }
- }
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package client;
- /**
- *
- * @author AKM
- */
- public class lmessage {
- private String username;
- private String password;
- private String type;
- public lmessage(String u,String p,String t)
- {
- username=u;
- password=p;
- type=t;
- }
- public String un()
- {
- StringBuilder mm=new StringBuilder();
- mm.append(username);
- mm.append(password);
- mm.append(type);
- return mm.toString();
- }
- public String tt()
- {
- return type;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement