Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javafx.scene.Scene;
- import java.net.*;
- import java.io.*;
- public class Client {
- public static void main(String[] args) {
- String ipAddress = "112.137.129.214";
- int port = 35116;
- Socket socket = null;
- BufferedWriter os = null;
- BufferedReader is = null;
- DataInputStream dataInputStream = null;
- DataOutputStream dataOutputStream = null;
- FileOutputStream fileOutputStream = null;
- try {
- socket = new Socket(ipAddress, port);
- os = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
- is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- dataInputStream = new DataInputStream(socket.getInputStream());
- dataOutputStream = new DataOutputStream(socket.getOutputStream());
- } catch (UnknownHostException e) {
- System.out.println("Server not found: " + e.getMessage());
- } catch (IOException e) {
- System.out.println("I/O error: " + e.getMessage());
- }
- try {
- os.write("#Login akdjflsadfkasdfj");
- os.newLine();
- os.flush();
- is.mark(250);
- // os.write("#msg abcxyz 'hey theson'");
- // os.newLine();
- // os.flush();
- os.write("#download large.pdf");
- os.newLine();
- os.flush();
- fileOutputStream = new FileOutputStream(new File("F:\\large.pdf"));
- int bytes = 0;
- long size = dataInputStream.readLong(); // read file size
- byte[] buffer = new byte[4*1024];
- while (size > 0 && (bytes = dataInputStream.read(buffer, 0, (int)Math.min(buffer.length, size))) != -1) {
- fileOutputStream.write(buffer,0,bytes);
- size -= bytes; // read upto file size
- }
- fileOutputStream.close();
- os.close();
- is.close();
- socket.close();
- } catch (UnknownHostException e) {
- System.out.println("Trying to connect to unknown host: " + e.getMessage());
- } catch (IOException e) {
- System.out.println("IOException: " + e.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement