-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
68 lines (59 loc) · 1.97 KB
/
Client.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
package clients;
import java.io.IOException;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.concurrent.LinkedBlockingQueue;
public class Client
{
public static final int DEFAULT_PORT = 4;
private ServerConnection serverConnection;
private LinkedBlockingQueue<String> serverMessageList;
private Socket socket;
private String IPAddress;
private int port;
private String username;
public Client(String IPAddress, int port, String username) {
this.IPAddress = IPAddress;
this.port = port;
this.username = username;
serverMessageList = new LinkedBlockingQueue<String>();
}
// Connects with Server. If connection is accepted, then It starts Listening.
public boolean connectToServer()
{
try {
socket = new Socket(IPAddress, port);
} catch (IOException e) {
System.out.println("ERROR: Couldn't establish connection with the server. Try again.");
e.printStackTrace();
return false;
}
serverConnection = new ServerConnection(socket, serverMessageList);
return true;
}
public void sendMessage(String clientMessage) {
if (clientMessage.equals("$"))
serverConnection.sendMessage(clientMessage);
else
{
String processedMessage = username + "%" +
clientMessage + "%" +
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
serverConnection.sendMessage(processedMessage);
}
}
public String getServerMessage()
{
if ( !serverMessageList.isEmpty() ) {
try {
return serverMessageList.take();
} catch (InterruptedException e) {
System.out.println("ERROR: Couldn't take a message from queue.");
e.printStackTrace();
}
}
return null;
}
}