-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGETClient.java
More file actions
65 lines (55 loc) · 2.65 KB
/
GETClient.java
File metadata and controls
65 lines (55 loc) · 2.65 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
import java.io.ObjectOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.util.LinkedList;
import java.net.InetAddress;
import java.util.Scanner;
public class GETClient {
public static int lamportClock = 0;
public static int ID = 001;
public static String hostName = "";
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException, NullPointerException {
int port = 0;
Scanner input = new Scanner(System.in);
System.out.println("Enter the Client Name and Port Number: ");
hostName = input.nextLine();
hostName = hostName.replace("https://", "");
String[] nameComponents = hostName.split(":");
hostName = nameComponents[0];
port = Integer.parseInt(nameComponents[1]);
try (Socket socket = new Socket(hostName, port)) {
System.out.println("Connected at: " + socket.getInetAddress().getHostAddress() +
" (address) " + socket.getInetAddress().getHostName() +
" (host name)");
PrintWriter outBound = new PrintWriter(socket.getOutputStream(), true);
lamportClock++;
System.out.println("Timestamp at: " + lamportClock);
System.out.println("GET /atom.xml HTTP/1.1\r\nHost: localhost\r\nUser-Agent: " +
ID + "\r\nAccept: application/atom+xml");
outBound.println("GET /atom.xml HTTP/1.1");
outBound.println("Host: localhost");
outBound.println("User-Agent: " + ID);
outBound.println("Accept: application/atom+xml");
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
Packet toPacket = new Packet("GET", lamportClock);
lamportClock++;
oos.writeObject(toPacket);
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
XMLPacket fromPacket = (XMLPacket) ois.readObject();
LinkedList<String> receivedXML = fromPacket.xmlContent;
lamportClock = Math.max(lamportClock, fromPacket.timeStamp);
lamportClock++;
System.out.println("Timestamp at: " + lamportClock);
XMLFactory xmlFactory = new XMLFactoryImplementation();
for (int i = 0; i < receivedXML.size(); i++) {
xmlFactory.printXML(i, receivedXML.get(i));
}
}
catch( Exception e ) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}