-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient
More file actions
89 lines (77 loc) · 3.47 KB
/
Client
File metadata and controls
89 lines (77 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package httpClient;
import java.io.*;
import java.net.*;
import java.util.Scanner;
import java.awt.Desktop;
// Client class
public class Client
{
public static void main(String[] args) throws IOException
{
try
{
Scanner scn = new Scanner(System.in);
// getting localhost ip
InetAddress ip = InetAddress.getByName("localhost");
// establish the connection with server port
Socket s = new Socket(ip, 55067);
// obtaining input and out streams
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
System.out.println("Sending url ...");
System.out.println("https://www.google.com/");
dos.writeUTF("https://www.google.com/\n");
DataInputStream DataInput = new DataInputStream(s.getInputStream());
BufferedReader input;
//ObjectInputStream
//input = new BufferedReader(new InputStreamReader(s.getInputStream()));
String fileName = DataInput.readUTF();
String path = fileName.toString();
System.out.println("Receving the path of file ..." + path);
try {
// Use this for reading the data.
byte[] buffer = new byte[10000];
FileInputStream inputStream =
new FileInputStream(fileName);
File file = new File(fileName);
Desktop desktop = Desktop.getDesktop();
desktop.open(file);
BufferedReader br = new BufferedReader(new FileReader(fileName));
String st;
while ((st = br.readLine()) != null)
System.out.println(st);
// read fills buffer with data and returns
// the number of bytes read (which of course
// may be less than the buffer size, but
// it will never be more).
int total = 0;
int nRead = 0;
while((nRead = inputStream.read(buffer)) != -1) {
// Convert to String so we can display it.
// Of course you wouldn't want to do this with
// a 'real' binary file
System.out.println(new String(buffer));
total += nRead;
}
// Always close files.
inputStream.close();
System.out.println("Read " + total + " bytes");
}
catch(FileNotFoundException ex) {
System.out.println("Unable to open file '" + fileName + "'");
}
catch(IOException ex) {
System.out.println("Error reading file '" + fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
// }
// closing resources
scn.close();
dis.close();
dos.close();
}catch(Exception e){
e.printStackTrace();
}
}
}