-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHelloClient.java
48 lines (43 loc) · 1.46 KB
/
HelloClient.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
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class HelloClient{
public static void main(String[] args) throws IOException{
Socket socket = null;
BufferedReader input = null;
DataOutputStream out = null;
DataInputStream in = null;
try {
socket = new Socket("127.0.0.1", 4321);
} catch(Exception i) {
System.out.println("Error in IP or port");
System.exit(1);
}
System.out.println("Connected");
try {
/* takes input from terminal */
input = new BufferedReader(new InputStreamReader(System.in));
/* sends output to the socket */
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(
new BufferedInputStream(socket.getInputStream()));
System.out.println(in.readUTF());
out.writeUTF(input.readLine());
String line="";
do{
line=in.readUTF();
System.out.println(line);
}
while( ! line.equals("Good Bye"));
} catch(EOFException e){
System.out.println("Server closed connection");
}catch(IOException i) {
System.out.println(i);
}
finally{
if (input!=null) input.close();
if (out!=null) out.close();
if (in!=null) in.close();
}
}
}