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.nio.file.*;
- import static java.nio.charset.StandardCharsets.*;
- public class Client implements Runnable
- {
- DataOutputStream output2 = null;
- DataInputStream input = null;
- // ClientConnect listenForFilePort = null;
- public boolean isConnected = true;
- int ConnectOnce = 0;
- public String[] clientArgs;
- public static Socket socket;
- public Socket fileSocket;
- public boolean keepRunning = true;
- public String option = "";
- public int differentPort;
- public int ConnectOnce1 = 0;
- public int normalPort = 0;
- public Client(String[] args) throws IOException
- {
- this.clientArgs = args;
- if(ConnectOnce == 0)
- {
- try
- {
- differentPort = Integer.valueOf(clientArgs[1]);
- String host = "localhost";
- InetAddress address = InetAddress.getByName(host);
- String port_number1= clientArgs[3];
- normalPort = Integer.valueOf(port_number1);
- socket = new Socket(address, normalPort);
- ConnectOnce = 4;
- }
- catch (UnknownHostException e)
- {
- e.printStackTrace();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- finally
- {
- }
- }
- }
- public void clientRun2(String[] args)
- {
- clientArgs = args;
- clientArgs = Arrays.copyOf(args, args.length);
- clientSend.start();
- /*
- * listenForFilePort = new ClientConnect(socket, differentPort);
- * listenForFilePort.clientConnection(differentPort, fileSocket);
- */
- }
- Thread clientSend = new Thread()
- {
- public void run()
- {
- OutputOptions();
- while(isConnected)
- {
- try
- {
- ClientRecieve clientThread = new ClientRecieve(socket);
- clientThread.start();
- DataInputStream userKeyboardInput = new DataInputStream(System.in);
- String line2 = userKeyboardInput.readLine();
- if(line2.equals("m") || line2.equals("M"))
- {
- ClientSend();
- }
- else if(line2.equals("f") || line2.equals("F"))
- {
- FileRequest();
- }
- else if(line2.equals("x") || line2.equals("X"))
- {
- isConnected = false;
- System.exit(0);
- break;
- }
- else if(!line2.equals("x") && !line2.equals("X") && !line2.equals("m")
- && !line2.equals("M") && !line2.equals("f") && !line2.equals("F"))
- {
- System.out.println("(invalid choice)");
- }
- }
- catch ( Exception e )
- {
- System.out.println( e.getMessage() );
- }
- }
- }
- };
- public void ClientSend()
- {
- try
- {
- DataInputStream userKeyboardInput = new DataInputStream(System.in);
- DataOutputStream output = new DataOutputStream(socket.getOutputStream());
- System.out.println("Enter your message:");
- String newMessage = userKeyboardInput.readLine();
- if(newMessage!=null && !newMessage.isEmpty())
- {
- output.writeUTF(newMessage);
- }
- output.flush();
- StandardInput();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- finally
- {
- }
- }
- public void FileRequest() throws FileNotFoundException
- {
- try
- {
- ///
- output2 = new DataOutputStream(socket.getOutputStream());
- input = new DataInputStream(socket.getInputStream());
- System.out.println("Which file do you want?");
- Scanner scanner = new Scanner(System.in);
- String thisFileName = scanner.nextLine();
- System.out.println("99877");
- if(thisFileName!=null && !thisFileName.isEmpty())
- {
- System.out.println("666332");
- System.out.println("69683721");
- output2.writeUTF(thisFileName + "`" + "\n");
- System.out.println("1234123456666");
- output2.flush();
- System.out.println("flushed");
- long file_size= input.readLong();
- System.out.println("The file size is: " + file_size);
- if ( file_size == 0 )
- {
- System.out.println("File size is 0");
- }
- FileOutputStream file_out= new FileOutputStream( thisFileName );
- System.out.println("4445555");
- int number_read;
- byte[] buffer= new byte[1500];
- System.out.println("23234");
- while( (number_read= input.read( buffer)) != -1 )
- {
- file_out.write( buffer, 0, number_read );
- System.out.println("66333");
- }
- System.out.println("45531");
- System.out.println("File " + thisFileName + " downloaded (" + number_read + " bytes read)");
- System.out.println("123411");
- StandardInput();
- }
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- public int recieveSizeAndPath(Socket socket)
- {
- int size = 0;
- try
- {
- BufferedReader readfromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- String fromServer = readfromServer.readLine();
- if(fromServer!=null && !fromServer.isEmpty())
- {
- if(fromServer.contains("size"))
- {
- String string = fromServer;
- String[] parts = string.split(":");
- String part1 = parts[0];
- String part2 = parts[1];
- System.out.println("Size of file is: " + part2);
- size = Integer.valueOf(part2);
- return size;
- }
- }
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- finally
- {
- }
- return size;
- }
- public static void receiveFile(int portNo, String fileLocation, Socket socket, int size) throws IOException
- {
- int bytesRead=0;
- int current = 0;
- FileOutputStream fileOutputStream = null;
- BufferedOutputStream bufferedOutputStream = null;
- try
- {
- byte [] byteArray = new byte [size];
- System.out.println("26");
- InputStream inputStream = socket.getInputStream();
- System.out.println("3453");
- fileOutputStream = new FileOutputStream(fileLocation);
- System.out.println("43252");
- bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
- System.out.println("849392");
- bytesRead = inputStream.read(byteArray,0,byteArray.length);
- current = bytesRead;
- do
- {
- bytesRead =inputStream.read(byteArray, current, (byteArray.length-current));
- if(bytesRead >= 0) current += bytesRead;
- }
- while(bytesRead > -1);
- bufferedOutputStream.write(byteArray, 0 , current);
- bufferedOutputStream.flush();
- System.out.println("File " + fileLocation + " downloaded ( size: " + current + " bytes read)");
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- finally
- {
- }
- }
- Thread ClientRead = new Thread();
- void OutputOptions()
- {
- System.out.println("Enter an option ('m', 'f', 'x'): ");
- System.out.println("(M)essage (send)");
- System.out.println("(F)ile (request) ");
- System.out.println("e(X)it ");
- }
- @Override
- public void run()
- {
- }
- public void StandardInput()
- {
- OutputOptions();
- while(true)
- {
- String option = "";
- String newmessage = "";
- try
- {
- DataInputStream userKeyboardInput = new DataInputStream(System.in);
- String line2 = userKeyboardInput.readLine();
- if(line2.equals("m") || line2.equals("M"))
- {
- ClientSend();
- }
- else if(line2.equals("f") || line2.equals("F"))
- {
- FileRequest();
- }
- else if(line2.equals("x") || line2.equals("X"))
- {
- isConnected = false;
- System.exit(0);
- break;
- }
- else if(!line2.equals("x") && !line2.equals("X") && !line2.equals("m")
- && !line2.equals("M") && !line2.equals("f") && !line2.equals("F"))
- {
- System.out.println("(invalid choice)");
- }
- }
- catch ( Exception e )
- {
- e.printStackTrace();
- }
- finally
- {
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement