-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.java
121 lines (111 loc) · 3.16 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
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
117
118
119
120
121
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
/**
* A very basic Client that connects to a Server.
* It takes input messages from the user and sends them to the server.
* Not too fancy and might differ from your Lab exercises or implementations.
*/
public class Client {
private int port;
private String host;
private static BufferedReader bufferedReader;
private PrintWriter printWriter;
private Socket socket;
public static void main(String[] args){
Client client = new Client("127.0.0.1", 8005);
client.start();
while (true) {
System.out.println("Please enter text:");
Scanner s = new Scanner(System.in);
String m = s.nextLine();
client.writeMessage(m);
client.readMessage();
try
{
// wait 5s to display the message, to see if it works
Thread.sleep(5000);
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
}
}
/**
* Stores the IP address & port nr the client is connecting to.
* @param host The String of the host's IP address.
* @param port The port nr to listen to on connection.
*/
public Client(String host, int port) {
this.host = host;
this.port = port;
}
/**
* Starts the client and sets up streams to and from it
*/
private void start() {
connectSocket();
setupStreams();
}
/**
* Creates a socket to communicate through
*/
private void connectSocket() {
try
{
socket = new Socket(host, port);
}
catch (UnknownHostException uhe)
{
uhe.printStackTrace();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
// From here on, almost 1:1 copy-paste from Server!
/**
* Create IO-Streams
*/
private void setupStreams() {
try (InputStreamReader input = new InputStreamReader(socket.getInputStream());
OutputStreamWriter output = new OutputStreamWriter(socket.getOutputStream());)
{
bufferedReader = new BufferedReader(input);
printWriter = new PrintWriter(output);
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
/**
* Read incoming messages and print them out
*/
private void readMessage() {
try
{
String s = bufferedReader.readLine();
System.out.println("Client got the following: " + s);
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
/**
* Method to write messages to the server
* @param message The message string to write to the connection.
*/
private void writeMessage(String message) {
printWriter.print(message + "\r\n");
printWriter.flush();
}
}