-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.java
More file actions
168 lines (164 loc) · 4.51 KB
/
client.java
File metadata and controls
168 lines (164 loc) · 4.51 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
import java.net.*;
import java.io.*;
import java.util.*;
import java.lang.*;
public class client
{
public class threadm implements Runnable
{
Socket s;
InputStreamReader input;
BufferedReader reader;
public threadm(Socket soc)
{
s=soc;
try
{
input = new InputStreamReader(s.getInputStream());
reader = new BufferedReader(input);
}
catch(Exception ex) { System.out.println("Server has Closed"); }
}
public void run()
{
String message;
String filename;
try
{
while((message=reader.readLine())!=null)
{
if(message.substring(0, 4).equals("file"))
{
System.out.println(message.substring(5));
filename = findfile(message);
try
{
InputStream inpustream = s.getInputStream();
FileOutputStream file = new FileOutputStream(filename);
BufferedOutputStream bufferoutput = new BufferedOutputStream(file);
byte[] bytes = new byte[16];
int count;
while((count=inpustream.read(bytes))>0)
{
bufferoutput.write(bytes,0,count);
bufferoutput.flush();
if(count<16) break;
}
}
catch(Exception ex) {System.out.println("Problem in last step");}
}
else if(!message.equals("message")) System.out.println(message);
}
}
catch(Exception ex) {ex.printStackTrace();}
}
public String findfile(String st)
{
String[] sts = st.split("\\s+");
return sts[2];
}
}
public void go()
{
int check=0,port;
Socket socket=null;
String message="",string="",name;
InputStreamReader input=null;
BufferedReader reader=null;
PrintWriter writer=null;
Scanner scan = new Scanner(System.in);
while(check==0 && scan.hasNextLine()) { message = scan.nextLine(); check=1;}
port = portnumber(message);
name=clientname(message);
try
{
socket = new Socket("127.0.0.1",port);
writer = new PrintWriter(socket.getOutputStream());
writer.println(name);
writer.flush();
input = new InputStreamReader(socket.getInputStream());
reader = new BufferedReader(input);
System.out.println(reader.readLine());
Thread t = new Thread(new threadm(socket));
t.start();
}
catch(Exception ex) {System.out.println("clientside mistake"); }
while((message=scan.nextLine())!=null)
{
if(containswordmessage(message))
{
message = modifymessage(message);
writer.println(message);
writer.flush();
}
else if(containswordfile(message))
{
string = modifymessage(message);
writer.println(string);
writer.flush();
File myFile = new File(findfilename(message));
byte[] bytes = new byte[16];
try
{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile));
OutputStream os = socket.getOutputStream();
int count;
while((count=bis.read(bytes))>0)
{
os.write(bytes, 0, count);
os.flush();
if(count<16) break;
}
}
catch(Exception ex) { System.out.println("problem in file writing"); }
}
else System.out.println("you entered a Invalid string");
}
}
public String findfilename2(String message)
{
String sts[]=message.split("\\s+");
return sts[1];
}
public String findfilename(String message)
{
String sts[]=message.split("\\s+");
return sts[2];
}
public String modifymessage(String st)
{
String ans="";
String[] sts=st.split("\\s+");
if(sts.length<2) System.out.println("Invalid String, Try Again");
ans = ans + sts[1];
ans = ans +" "+sts[0];
for(int i=2;i<sts.length;i++) ans = ans+" "+sts[i];
return ans;
}
public String clientname(String st)
{
String[] sts=st.split("\\s+");
return sts[0];
}
public int portnumber(String st)
{
String[] ans = st.split("\\s+");
return Integer.parseInt(ans[1]);
}
public boolean containswordmessage(String st)
{
String[] sts=st.split("\\s+");
for(String s:sts) if(s.equals("message")) return true;
return false;
}
public boolean containswordfile(String st)
{
String[] sts=st.split("\\s+");
for(String s:sts) if(s.equals("file")) return true;
return false;
}
public static void main(String args[])
{
new client().go();
}
}