-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientText.java
116 lines (104 loc) · 4.22 KB
/
ClientText.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Assignment3;
/**
* @author Chairman
*/
import java.io.IOException;
import java.util.*;
import java.net.*;
import java.nio.ByteBuffer;
public class ClientText extends Client {
double newBid;
String buyerAddress = "0.0.0.0:0";
private class MyTask implements Runnable {
public void run() {
try {
DatagramSocket clientSocket = new DatagramSocket(port + 1);
for(;;) {
byte[] bidPacket = new byte[8];
byte[] addressPacket = new byte[64];
byte[] timePacket = new byte[4];
DatagramPacket receivePacket = new DatagramPacket(bidPacket, bidPacket.length);
for (int i = 0; i < 1; i++) {
try {
clientSocket.receive(receivePacket);
} catch (SocketTimeoutException e) {
i--;
}
}
newBid = ByteBuffer.wrap(bidPacket).getDouble();
//Uncomment this to add receiving of buyer address and time left in auction
//CLIENT TEXT SHOULDN'T DO ANYTHING WITH TIME OTHER THAN RECEIVE TO CLEAR THE PACKET
/*
receivePacket = new DatagramPacket(addressPacket, addressPacket.length);
clientSocket.receive(receivePacket);
buyerAddress = new String(addressPacket);
buyerAddress = buyerAddress.trim();
receivePacket = new DatagramPacket(timePacket, timePacket.length);
socket.receive(receivePacket);
*/
if (newBid != bid) {
bid = newBid;
System.out.println("\n***New Max Bid***\n");
System.out.print("\rMax bid: $" + bid + "\tBuyer address: " + buyerAddress);
System.out.print("\tBid: ");
}
}
} catch (IOException e) {}
}
}
public void main() {
Scanner kb = new Scanner(System.in);
for (; ; ) {
try {
if (socket == null) {
findGoodPort();
socket = new DatagramSocket(port);
address = InetAddress.getByName("localhost");
contactServer();
}
System.out.print("\rMax bid: $" + bid + "\tBuyer address: " + buyerAddress);
System.out.print("\tBid: $");
bid(kb.nextLine());
} catch (IOException e) {}
}
}
public void contactServer() {
bid("-1");
}
public void bid(String input) {
try {
Thread t = new Thread(new MyTask());
t.start();
double bidAmount;
String[] parts = input.split("\\.");
if (parts.length > 1) {
if (parts[1].length() > 2) {
System.out.println("\n***Amount can only have up to two decimal places.***\n");
System.out.print("Max bid: $" + bid + "\tBuyer address: " + buyerAddress);
System.out.print("\tBid: $");
return;
}
}
try {
bidAmount = Double.parseDouble(input);
} catch (NumberFormatException e) {
System.out.println("\n***Invalid amount.***\n");
System.out.print("Max bid: $" + bid + "\tBuyer address: " + buyerAddress);
System.out.print("\tBid: $");
return;
}
//long startTime = System.nanoTime();
DatagramPacket sendPacket = new DatagramPacket(ByteBuffer.allocate(8).putDouble(bidAmount).array(),
8, address, SERVER_PORT);
socket.send(sendPacket);
if(!input.equals("-1")) {
System.out.println("\n***Client sent $" + input + " as bid***\n");
}
} catch (IOException e) {}
}
}