Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- import java.util.*;
- import javax.imageio.IIOException;
- public class Server
- {
- private String[] serverArgs;
- public Socket socket;
- public Socket fileSocket;
- public boolean keepRunning = true;
- public int ConnectOnce = 0;
- public String option = "";
- public boolean isConnected = false;
- public Server(String[] args) throws IOException
- {
- this.serverArgs = args;
- if(ConnectOnce == 0)
- {
- int port_number1 = Integer.valueOf(serverArgs[1]);
- ServerSocket serverSocket = new ServerSocket(port_number1);
- socket = serverSocket.accept();
- ConnectOnce = 4;
- isConnected = true;
- }
- }
- public void serverRun2(String[] args) throws IOException
- {
- serverArgs = args;
- serverArgs = Arrays.copyOf(args, args.length);
- serverSend.start();
- }
- Thread serverSend = new Thread()
- {
- public void run()
- {
- OutputOptions();
- while(isConnected)
- {
- try
- {
- ServerRecieve serverThread = new ServerRecieve(socket);
- serverThread.start();
- DataInputStream userKeyboardInput = new DataInputStream(System.in);
- String line2 = userKeyboardInput.readLine();
- if(line2.equals("m") || line2.equals("M"))
- {
- ServerSend();
- }
- 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 ServerSend()
- {
- 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
- {
- System.out.println("Which file do you want?");
- DataInputStream userKeyboardInput = new DataInputStream(System.in);
- DataOutputStream output = new DataOutputStream(socket.getOutputStream());
- String thisFileName = userKeyboardInput.readLine();
- if(thisFileName!=null && !thisFileName.isEmpty())
- {
- DataInputStream input = new DataInputStream(socket.getInputStream());
- output.writeUTF(thisFileName + "`" + "\n");
- long file_size= input.readLong();
- System.out.println("File size is: " + file_size);
- if ( file_size == 0 )
- {
- System.out.println("File size is 0");
- }
- FileOutputStream file_out= new FileOutputStream( thisFileName );
- int number_read;
- byte[] buffer= new byte[1500];
- while( (number_read= input.read( buffer)) != -1 )
- {
- file_out.write( buffer, 0, number_read );
- }
- System.out.println("File " + thisFileName + " downloaded (" + number_read + " bytes read)");
- file_out.flush();
- output.flush();
- StandardInput();
- }
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- 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 ");
- }
- public void StandardInput()
- {
- OutputOptions();
- while(true)
- {
- try
- {
- DataInputStream userKeyboardInput = new DataInputStream(System.in);
- String line2 = userKeyboardInput.readLine();
- if(line2.equals("m") || line2.equals("M"))
- {
- ServerSend();
- }
- 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() );
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment