-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocketClient.java
More file actions
129 lines (102 loc) · 3.74 KB
/
socketClient.java
File metadata and controls
129 lines (102 loc) · 3.74 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
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
122
123
124
125
126
127
128
129
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class socketClient {
private String hostname;
private int port;
private String userName;
public socketClient(String hostname, int port){
this.hostname = hostname;
this.port = port;
}
public class ReadThread extends Thread {
private BufferedReader reader;
private Socket socket;
private socketClient client;
public ReadThread(Socket socket, socketClient client){
this.socket = socket;
this.client = client;
try{
InputStream input = socket.getInputStream();
reader = new BufferedReader(new InputStreamReader(input));
}catch(IOException ex){
System.out.println("Error getting input stream: " + ex.getMessage());
ex.printStackTrace();
}
}
public void run(){
while(true){
try{
String response = reader.readLine();
System.out.println("\n" + response);
if(client.getUserName()!=null){
System.out.print("[" + client.getUserName() + "]: ");
}
} catch(IOException ex){
System.out.println("Error reading from server" + ex.getMessage());
ex.printStackTrace();
break;
}
}
}
}
public class WriteThread extends Thread {
private PrintWriter writer;
private Socket socket;
private socketClient client;
public WriteThread(Socket socket, socketClient client) {
this.socket = socket;
this.client = client;
try {
OutputStream output = socket.getOutputStream();
writer = new PrintWriter(output, true);
} catch (IOException ex) {
System.out.println("Error getting output stream: " + ex.getMessage());
ex.printStackTrace();
}
}
public void run() {
Scanner s = new Scanner(System.in);
String userName = s.nextLine();
client.setUserName(userName);
writer.println(userName);
String text;
do {
text = s.nextLine();
writer.println(text);
} while (!text.equals("bye"));
try {
socket.close();
} catch (IOException ex) {
System.out.println("Error writing to server: " + ex.getMessage());
}
}
}
public void execute(){
try{
Socket socket = new Socket(hostname, port);
System.out.println("Connected !");
new ReadThread(socket, this).start();
new WriteThread(socket, this).start();
}catch(UnknownHostException ex){
System.out.println("Server not found" + ex.getMessage());
}catch(IOException ex){
System.out.println("I/O Error: " + ex.getMessage());
}
}
void setUserName(String userName){
this.userName = userName;
}
String getUserName(){
return this.userName;
}
public static void main(String[] args){
//When communicate within one computer
//String hostname = "localhost" ;
//When the server is running on anther computer, hostname is server side computer's IP address
String hostname = "100.64.9.115";
int port = 8000;
socketClient client = new socketClient(hostname, port);
client.execute();
}
}