-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathClient.java
More file actions
212 lines (197 loc) · 7.75 KB
/
Copy pathClient.java
File metadata and controls
212 lines (197 loc) · 7.75 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chattingapp;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.NoSuchElementException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
/**
*
* @author riddhi
*/
public class Client
{
private String username,password;
private Socket socket;
private InputStream in;
private OutputStream out;
private Scanner sc;
private PrintWriter pw;
public Scanner getScanner() {
return sc;
}
public void setScanner(Scanner sc) {
this.sc = sc;
}
public PrintWriter getPrintWriter() {
return pw;
}
public void setPrintWriter(PrintWriter pw) {
this.pw = pw;
}
public Client(String username,String password)
{
this.username = username;
this.password = password;
}
public void sendMessage(String str)
{
pw.println(str);
}
public String getUsername() {
return username;
}
public String toString()
{
String s = username;
if(socket!=null)
s += "(Online)";
else
s+= "(Offline)";
return s;
}
public Socket getSocket() {
return socket;
}
public InputStream getIn() {
return in;
}
public OutputStream getOut() {
return out;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void close()
{
try {
socket.close();
} catch (IOException ex) {
}
finally{
Client.this.socket = null;
Client.this.in = null;
Client.this.out = null;
Client.this.sc = null;
Client.this.pw = null;
ServerFrame.frame.lstChatters.repaint();
ServerFrame.frame.taChats.append(username+" left the chat room.\n");
ServerFrame.frame.broadcast("F:"+username);
}
}
public static Client searchByName(String name)
{
ArrayList<Client> clients = ServerFrame.frame.clients;
for(Client client:clients)
if(client.getUsername().equals(name))
return client;
return null;
}
public void handle(Socket socket)
{
try {
ServerFrame.frame.broadcast("O:"+username);
this.socket = socket;
in = socket.getInputStream();
out = socket.getOutputStream();
sc = new Scanner(in);
pw = new PrintWriter(out,true);
Thread t = new Thread(
new Runnable()
{
@Override
public void run()
{
while(true)
{
String request = null;
try{
request = sc.nextLine();
}
catch(NoSuchElementException ex)
{
close();
break;
}
if(request.charAt(0)=='T') //logout
{
close();
break;
}
else if(request.charAt(0)=='C')//public chatting
{
String chat = request.substring(2);
String message = "C:" + username + ":" + chat;
ServerFrame.frame.broadcast(message);
ServerFrame.frame.taChats.append(username +": "+chat+"\n");
}
else if(request.charAt(0)=='R') //client 1(sender) requests for private chat
{
String client2uname = request.substring(2);
Client client = searchByName(client2uname);
client.getPrintWriter().println("Q:"+username);
ServerFrame.frame.taChats.append(username +" requested "+client2uname +" for a private chat.\n");
}
else if(request.charAt(0)=='Q') //client 2(receiver) sends back response whether he accepts the private chat request by client 1.
{
String[] req = request.split(":");
String client1uname = req[1];
Client client1 = searchByName(client1uname);
int isAccept = Integer.parseInt(req[2]);
if(isAccept==0) //not accepted
ServerFrame.frame.taChats.append(username+" rejected private chat request of "+client1uname+".\n");
else //accepted
ServerFrame.frame.taChats.append(username+" accepted private chat request of "+client1uname+".\n");
client1.pw.println("R:"+username+":"+isAccept);
}
else if(request.charAt(0)=='P') //private chat
{
int p = request.indexOf(':',2);
String toClientStr = request.substring(2,p);
String message = request.substring(p+1);
Client toClient = searchByName(toClientStr);
toClient.getPrintWriter().println("P:"+username+":"+message);
}
else if(request.charAt(0)=='D') //private chat disconnect
{
String toClientStr = request.substring(2);
Client toClient = searchByName(toClientStr);
toClient.getPrintWriter().println("D:"+username);
ServerFrame.frame.taChats.append(username+" ended private chat with "+ toClientStr+".\n");
}
else if(request.charAt(0)=='A') // get all logged in users for user who have just logged in.
{
int no = 0;
String resp = "";
for(Client client:ServerFrame.frame.clients)
if(client.getSocket()!=null)
{
resp += ("\nA:"+client.getUsername());
no++;
}
pw.println("A:"+no+resp);
}
}
}
}
);
t.start();
} catch (IOException ex) {
close();
}
}
}