-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
186 lines (141 loc) · 4.14 KB
/
Client.java
File metadata and controls
186 lines (141 loc) · 4.14 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
package com.cn;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
public class Client {
Socket requestSocket; //socket connect to the server
ObjectOutputStream out; //stream write to the socket
ObjectInputStream in; //stream read from the socket
String message; //message send to the server
BufferedReader br;
String clientName;
int portNo;
public Client(String clientName) {
this.clientName = clientName;
}
void run()
{
try{
//create a socket to connect to the server
requestSocket = new Socket("localhost", 8000);
System.out.println("Connected to localhost in port 8000");
//initialize inputStream and outputStream
out = new ObjectOutputStream(requestSocket.getOutputStream());
out.flush();
in = new ObjectInputStream(requestSocket.getInputStream());
//get Input from standard input
br = new BufferedReader(new InputStreamReader(System.in));
final Thread sendThread = new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
while(true)
{
sendThreadMethod();
}
}
});
sendThread.start();
final Thread receiveThread = new Thread(new Runnable(){
@Override
public void run() {
while(true){
receiveThreadMethod();
}
// TODO Auto-generated method stub
}
});
receiveThread.start();
TimeUnit.MINUTES.sleep(20);
}
catch (ConnectException e) {
System.err.println("Connection refused. You need to initiate a server first.");
}
catch(UnknownHostException unknownHost){
System.err.println("You are trying to connect to an unknown host!");
}
catch(IOException ioException){
ioException.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
//Close connections
try{
in.close();
out.close();
requestSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
//send a message to the output stream
private void sendThreadMethod(){
try{
message = br.readLine();
//Send the sentence to the server
out.writeObject(message);
out.flush();
}
catch(Exception e){
}
}
private void receiveThreadMethod(){
try{
Object o = in.readObject();
if(o instanceof String)
{
String msg = (String)o;
int clientNo = Character.getNumericValue(msg.charAt(msg.length()-1));
msg = msg.substring(0, msg.length()-1);
System.out.println("Message received from client "+clientNo+": "+msg);
}
else {
System.out.println("Receiving file....");
FileHelper fileHelper = (FileHelper)o;
Path p = Paths.get(fileHelper.getFileName());
String name = p.getFileName().toString();
String filePath = "com\\cn\\client"+fileHelper.getReceiverNo()+"\\"+name;
File file = new File(filePath);
if(!file.getParentFile().exists())
file.getParentFile().mkdirs();
if ( ! file.exists() )
file.createNewFile();
Files.write(file.toPath(), fileHelper.getContent());
System.out.println("File received from client"+fileHelper.getSenderNo()+" - "+name);
}
}
catch(Exception e){
}
}
void sendMessage(String msg)
{
try{
//stream write the message
out.writeObject(msg);
out.flush();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
//main method
public static void main(String args[])
{
Client client = new Client(args[0]);
client.run();
}
}