-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.java
118 lines (77 loc) · 2.46 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
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class client {
protected static final long serialVersionUID = 1L;
protected ObjectOutputStream o;
protected ObjectInputStream i;
protected Socket cS;
protected String message;
protected String host;
protected int serverPort;
public static void main(String [] args){
int serverPort = 6000;
String host = "localhost";
//if new values are inserted use new values else use default
if (args.length < 2) {
System.out
.println("Usage: java MultiThreadChatClient <host> <portNumber>\n"
+ "Now using host=" + host + ", portNumber=" + serverPort);
} else {
host = args[0];
serverPort = Integer.valueOf(args[1]).intValue();
}
client c = new client();
c.run();
}
private void run(){
try {
connectToServer(host,6000);
setupStreams();
do{
String message= (String) i.readObject();
sendMessage(message);
}while(!(message).equals("quit"));
closeApp();
} catch (IOException|NullPointerException e) {
System.out.println(e);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void sendMessage(String message){
try{
o.writeObject("Client- " +message);
o.flush();
showMessage("\nClient- " + message);
}catch(IOException e ){
}
}
private void connectToServer(String host, int portN)throws IOException{
showMessage("Connecting....\n");
cS = new Socket(host,portN);
showMessage("Connected to .." + cS.getInetAddress().getHostName());
}
private void setupStreams() throws IOException{
o = new ObjectOutputStream(cS.getOutputStream());
o.flush();
i= new ObjectInputStream(cS.getInputStream());
showMessage("Connected,,");
}
private void closeApp() {
// TODO Auto-generated method stub
try{
o.close();
i.close();
cS.close();
}catch(IOException e){
e.printStackTrace();
}
}
private void showMessage(String string) {
// TODO Auto-generated method stub
}
}