-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.java
More file actions
345 lines (244 loc) · 13.1 KB
/
Copy pathclient.java
File metadata and controls
345 lines (244 loc) · 13.1 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import java.io.*;
import java.net.*;
public class client
{
static Scanner in = new Scanner(System.in);
static DataOutputStream dos;
static DataInputStream dis;
static Socket s;
static boss server;
static String username;
//-------For File sharing------------
static FileInputStream fis = null;
static BufferedInputStream bis = null;
static OutputStream os = null;
static InputStream is = null;
static FileOutputStream fos = null;
static BufferedOutputStream bos = null;
static String FILE_TO_RECEIVE;
static int bytesRead;
static int current = 0;
//-------------------------------
static JTextPane chatMessages = new JTextPane();
static JScrollPane JPchatMessages = new JScrollPane(chatMessages);
static String msgHistory = new String("");
public static void main(final String args[]) throws IOException
{
JFrame frame1 = new JFrame("Login to LAN Chat & File Sharing");
JFrame frame2 = new JFrame("LAN Chat & File Sharing");
JFrame frame3 = new JFrame("Choose a file to send");
//----------------------------------------------------------------
//---------------------- FRAME 2----------------------------------
//----------------------------------------------------------------
frame2.setSize(500,700);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setLayout(new GridBagLayout());
JLabel helloUser = new JLabel("Hello and Welcome !");
frame2.add(helloUser, new GridBagConstraints(0,0,1,1,3,1,GridBagConstraints.CENTER, GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0));
JButton logOutButton = new JButton("Logout");
frame2.add(logOutButton, new GridBagConstraints(2,0,1,1,.25,1,GridBagConstraints.CENTER, GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0));
chatMessages.setEditable(false);
frame2.add(JPchatMessages, new GridBagConstraints(0,1,3,1,1.0,100.0,GridBagConstraints.CENTER, GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0));
JTextField message = new JTextField(20);
frame2.add(message, new GridBagConstraints(0,2,1,1,.5,1,GridBagConstraints.CENTER, GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0));
JButton send = new JButton("send");
frame2.add(send, new GridBagConstraints(1,2,1,1,.25,.25,GridBagConstraints.CENTER, GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0));
send.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent sendButtonClick)
{
String msg = message.getText();
message.setText(null);
try
{
dos.writeUTF(username + " : " + msg);
}
catch(IOException e){}
}
});
JButton sendFile = new JButton("Send File");
frame2.add(sendFile, new GridBagConstraints(2,2,1,1,.25,.25,GridBagConstraints.CENTER, GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0));
sendFile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent sendFileButtonClick)
{
frame3.setVisible(true);
}
});
//----------------------------------------------------------------
//---------------------- FRAME 2 ends ----------------------------
//----------------------------------------------------------------
//----------------------------------------------------------------
//------------------------ FRAME 1 -------------------------------
//----------------------------------------------------------------
frame1.setLayout(new GridBagLayout());
frame1.setSize(500,700);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//----For bg image-----------
JLabel background=new JLabel(new ImageIcon("logo.png"),JLabel.CENTER);
frame1.add(background, new GridBagConstraints(0,0,1,1,2.0,1.0,GridBagConstraints.CENTER, GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0));
//---------------------------
//------Label-----------------
JLabel enter = new JLabel("Enter your name");
frame1.add(enter, new GridBagConstraints(0,2,1,1,1.0,1.0,GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(0,0,0,0),0,0));
//----------------------------
//------Username Textfield-------
JTextField usernameTextArea = new JTextField(10);
frame1.add(usernameTextArea, new GridBagConstraints(0,3,1,1,1.0,1.0,GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(0,0,0,0),0,0));
//----------------------------
//-------Login Button-------------
JButton login = new JButton("Join chat");
frame1.add(login, new GridBagConstraints(0,4,1,1,1.0,1.0,GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(0,0,0,0),0,0));
login.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent buttonClick)
{
username = usernameTextArea.getText();
helloUser.setText("Hello "+username+". Welcome !");
frame1.setVisible(false);
try
{
s = new Socket("localhost", 7777);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
server = new boss(dis);
Thread t = new Thread(server);
t.start();
frame2.setVisible(true);
}
catch(IOException e)
{
System.out.println("Server unavailable to connect. Press Ctrl+C to exit..");
}
}
});
//-------------------------------
frame1.setVisible(true);
//----------------------------------------------------------------
//---------------------- FRAME 1 ends ----------------------------
//----------------------------------------------------------------
//----------------------------------------------------------------
//---------------------- FRAME 3 ---------------------------------
//----------------------------------------------------------------
frame3.setSize(500,700);
frame3.setLayout(new GridBagLayout());
JFileChooser fileSelector = new JFileChooser();
frame3.add(fileSelector, new GridBagConstraints(1,0,1,1,.25,.25,GridBagConstraints.CENTER, GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0));
fileSelector.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent fileChooserEvent)
{
String command = fileChooserEvent.getActionCommand();
if(command.equals(JFileChooser.APPROVE_SELECTION))
{
File fileToBeSent = fileSelector.getSelectedFile();
frame3.setVisible(false);
try{
byte [] fileByteArray = new byte [(int)fileToBeSent.length()];
fis = new FileInputStream(fileToBeSent);
bis = new BufferedInputStream(fis);
bis.read(fileByteArray,0,fileByteArray.length);
os = s.getOutputStream();
dos.flush();
os.flush();
dos.flush();
dos.writeUTF("46511231dsfdsfsd#@$#$#@^$%#@*$#^");
os.write(fileByteArray,0,fileByteArray.length);
os.flush();
System.out.println("Done.");
updateMessageArea("File Successfully Sent.");
if (os != null)
os.close();
}
catch(Exception e){}
}
}
});
//----------------------------------------------------------------
//----------------------------------------------------------------
//----------------------------------------------------------------
}
public static void updateMessageArea(String msg)
{
msgHistory = msgHistory + "\n";
msgHistory = msgHistory + msg;
chatMessages.setText(msgHistory);
}
public static void reconnect()
{
try
{
s.close();
s = new Socket("192.168.0.101", 7777);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
server = new boss(dis);
Thread newConnection = new Thread(server);
newConnection.start();
}
catch(Exception e)
{
System.out.println("Exception caught in reconnect().");
}
}
public static void receiveFile()
{
String workingDir = System.getProperty("user.dir");
String FILE_TO_RECEIVED = workingDir+ "/imageReceived.jpg";
int FILE_SIZE = 70000;
int bytesRead;
int current = 0;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
byte [] mybytearray = new byte [FILE_SIZE];
InputStream is = s.getInputStream();
fos = new FileOutputStream(FILE_TO_RECEIVED);
bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
bos.write(mybytearray, 0 , current);
bos.close();
System.out.println("File " + FILE_TO_RECEIVED+ " downloaded (" + current + " bytes read)");
}
catch(Exception e)
{
System.out.println("Exception caught in receiveFile().");
}
}
}
class boss extends Thread
{
DataInputStream disServer;
String secretCode = new String("46511231dsfdsfsd#@$#$#@^$%#@*$#^");
public boss(DataInputStream z)
{
disServer = z;
}
public void run()
{
while(true)
{
try
{
String str = disServer.readUTF();
if(str.equals(secretCode))
{
client.receiveFile();
}
else
client.updateMessageArea(str);
}
catch(IOException e)
{
System.out.println("Exception in run method. Reconnecting..");
client.reconnect();
break;
}
}
}
}