-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoolClient.java
More file actions
76 lines (70 loc) · 2.21 KB
/
CoolClient.java
File metadata and controls
76 lines (70 loc) · 2.21 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
66
67
68
69
70
71
72
73
74
75
76
/**
* Created by sirlittle on 1/4/15.
*/
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class CoolClient{
PrintWriter writer;
BufferedReader reader;
public void go(){
try{
Socket s = new Socket("192.168.1.20", 4240);
Scanner input = new Scanner(System.in);
writer = new PrintWriter(s.getOutputStream());
InputStreamReader streamReader = new InputStreamReader(s.getInputStream());
reader = new BufferedReader(streamReader);
InetAddress IP=InetAddress.getLocalHost();
System.out.println("IP of my system is := "+ IP.getHostAddress());
String sendToServer;
ReadStuff threadstuff = new ReadStuff();
Thread th = new Thread(threadstuff);
th.start();
while(true) {
sendToServer = input.nextLine();
writer.println(sendToServer);
writer.flush();
if (sendToServer.equals("/quit")) {
break;
}
}
threadstuff.kill();
System.out.println("Connection Terminated");
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args){
CoolClient client = new CoolClient();
client.go();
}
private class ReadStuff implements Runnable{
boolean killed = false;
public void run(){
String message;
try{
while(!killed){
message = reader.readLine();
if (message != null && message.equals("terminate")){
break;
}
else if(message != null){
System.out.println(message);
}
}
writer.close();
reader.close();
}
catch(SocketException se){
System.out.println("Quit while Socket was tryna read, but its k.");
}
catch(Exception e){
e.printStackTrace();
}
}
public void kill(){
killed = true;
}
}
}