-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTCPClient.java
82 lines (72 loc) · 3.47 KB
/
TCPClient.java
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
import java.io.*;
import java.net.*;
public class TCPClient {
public static void main(String[] args) throws IOException {
// Variables for setting up connection and communication
Socket Socket = null; // socket to connect with ServerRouter
PrintWriter out = null; // for writing to ServerRouter
BufferedReader in = null; // for reading form ServerRouter
InetAddress addr = InetAddress.getLocalHost();
String host = addr.getHostAddress(); // Client machine's IP
String routerName = addr.getHostName(); // ServerRouter host name
int SockNum = 5555; // port number
// Tries to connect to the ServerRouter
try {
Socket = new Socket(routerName, SockNum);
out = new PrintWriter(Socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(Socket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about router: " + routerName);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: " + routerName);
System.exit(1);
}
// Variables for message passing
Reader reader = new FileReader("file.txt");
BufferedReader fromFile = new BufferedReader(reader); // reader for the string file
String fromServer; // messages received from ServerRouter
String fromUser; // messages sent to ServerRouter
//Server IP goes here
//Find this in console output of TCPServerRouter (ServerRouter connected with Client/Server: 127.0.0.1)
String address = "127.0.0.1"; // destination IP (Server)
long t0, t1, t;
// Communication process (initial sends/receives
out.println(address);// initial send (IP of the destination Server)
fromServer = in.readLine();//initial receive from router (verification of connection)
System.out.println("ServerRouter: " + fromServer);
out.println(host); // Client sends the IP of its machine as initial send
t0 = System.currentTimeMillis();
long tOriginal = t0;
int count = 0;
// Communication while loop
while ((fromServer = in.readLine()) != null) {
System.out.println("Server: " + fromServer);
t1 = System.currentTimeMillis();
if (fromServer.equals("Bye.")) // exit statement
break;
t = t1 - t0;
//If this is the first run, display the lookup time
if (count == 0) {
System.out.println("Lookup time is: " + t*10e-3);
} else {
//Otherwise output cycle time per line
System.out.println("Cycle time: " + t*10e-3);
}
count++;
fromUser = fromFile.readLine(); // reading strings from a file
if (fromUser != null) {
System.out.println("Client: " + fromUser);
out.println(fromUser); // sending the strings to the Server via ServerRouter
t0 = System.currentTimeMillis();
}
long t2 = System.currentTimeMillis();
//Total time so far, in nanoseconds
System.out.println("Total time so far: " + (t2 - tOriginal)*10e-3);
}
// closing connections
out.close();
in.close();
Socket.close();
}
}