Advertisement
thesonpb

Untitled

Apr 5th, 2021
704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.26 KB | None | 0 0
  1. import javafx.scene.Scene;
  2.  
  3. import java.net.*;
  4. import java.io.*;
  5.  
  6. public class Client {
  7.     public static void main(String[] args) {
  8.         String ipAddress = "112.137.129.214";
  9.         int port = 35116;
  10.         Socket socket = null;
  11.         BufferedWriter os = null;
  12.         BufferedReader is = null;
  13.         DataInputStream dataInputStream = null;
  14.         DataOutputStream dataOutputStream = null;
  15.         FileOutputStream fileOutputStream = null;
  16.  
  17.  
  18.         try {
  19.             socket = new Socket(ipAddress, port);
  20.             os = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
  21.             is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  22.              dataInputStream = new DataInputStream(socket.getInputStream());
  23.              dataOutputStream = new DataOutputStream(socket.getOutputStream());
  24.         } catch (UnknownHostException e) {
  25.             System.out.println("Server not found: " + e.getMessage());
  26.         } catch (IOException e) {
  27.             System.out.println("I/O error: " + e.getMessage());
  28.         }
  29.  
  30.         try {
  31.             os.write("#Login akdjflsadfkasdfj");
  32.             os.newLine();
  33.             os.flush();
  34.             is.mark(250);
  35. //            os.write("#msg abcxyz 'hey theson'");
  36. //            os.newLine();
  37. //            os.flush();
  38.             os.write("#download large.pdf");
  39.             os.newLine();
  40.             os.flush();
  41.             fileOutputStream = new FileOutputStream(new File("F:\\large.pdf"));
  42.             int bytes = 0;
  43.             long size = dataInputStream.readLong();     // read file size
  44.             byte[] buffer = new byte[4*1024];
  45.             while (size > 0 && (bytes = dataInputStream.read(buffer, 0, (int)Math.min(buffer.length, size))) != -1) {
  46.                 fileOutputStream.write(buffer,0,bytes);
  47.                 size -= bytes;      // read upto file size
  48.             }
  49.             fileOutputStream.close();
  50.  
  51.  
  52.  
  53.             os.close();
  54.             is.close();
  55.             socket.close();
  56.         } catch (UnknownHostException e) {
  57.             System.out.println("Trying to connect to unknown host: " + e.getMessage());
  58.         } catch (IOException e) {
  59.             System.out.println("IOException: " + e.getMessage());
  60.         }
  61.  
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement