/
ClientCommunicate.java
163 lines (136 loc) · 4.08 KB
/
ClientCommunicate.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
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
/**
*ClientCommunicate.java
* Listens for BlueCat server
*
* @author Arielle Davenport
*
* Last modified: 4-15-15
*/
import java.net.*;
import java.io.*;
public class ClientCommunicate {
//global variables
int port_number;
InetAddress loopback;
Socket client_socket;
PrintWriter output;
BufferedReader input;
//constructor with exception
public ClientCommunicate(int port) throws IOException{
port_number = port;
loopback = InetAddress.getLoopbackAddress();
//try to connect to server
client_socket = new Socket(loopback,port_number);
//grab the output and input streams
output = new PrintWriter(client_socket.getOutputStream(), true);
input = new BufferedReader(new InputStreamReader(client_socket.getInputStream()));
}
void communicate(String message) throws IOException{
//send message
System.out.println("Sending message to the server...");
output.println(message);
//receive a message
String response = input.readLine();
if(response.isEmpty())
System.out.println("(server did not reply with a message)");
else{
System.out.println("Server response:");
System.out.println(response);
}
}
void filecommunicate(String filename) throws IOException{
// open the file for reading
BufferedReader file= new BufferedReader( new FileReader( filename ) );
// read the contents and output it to stdout
String line= null;
//send message
System.out.println("Sending message to the server...");
while( (line= file.readLine()) != null ){
String onebyone = line;
output.println(onebyone);
}
file.close();
//receive a message
String response = input.readLine();
if(response.isEmpty())
System.out.println("(server did not reply with a message)");
else{
System.out.println("Server response:");
System.out.println(response);
}
}
void fileline(String filename) throws IOException{
// open the file for reading
BufferedReader file= new BufferedReader( new FileReader( filename ) );
// read the contents and output it to stdout
String line= null;
//send message
System.out.println("Sending message to the server...");
while( (line= file.readLine()) != null ){
String onebyone = line;
output.println(onebyone);
}
file.close();
//receive a message
String response = input.readLine();
if(response.isEmpty())
System.out.println("(server did not reply with a message)");
else{
System.out.println("Server response:");
System.out.println(response);
}
}
void writefile(String message, String filename2) throws IOException{
//send message
System.out.println("Sending message to the server...");
output.println(message);
// open the file for reading
PrintWriter file2= new PrintWriter( new BufferedWriter( new FileWriter(filename2) ) );
String line2 = input.readLine();
//receive a message
//String response = input.readLine();
if(line2.isEmpty())
System.out.println("(server did not reply with a message)");
else{
System.out.println("Writing to file...:");
//String line= null;
file2.println( line2 );
file2.close();
}
System.out.println("Done!");
}
void writeoneline(String filename1, String filename2) throws IOException{
// open the file for reading
BufferedReader file= new BufferedReader( new FileReader( filename1 ) );
String line= null;
//send message
System.out.println("Sending message to the server...");
while( (line= file.readLine()) != null ){
String onebyone = line;
output.println(onebyone);
}
file.close();
// open the file for reading
PrintWriter file2= new PrintWriter( new BufferedWriter( new FileWriter(filename2) ) );
String line2 = input.readLine();
//receive a message
//String response = input.readLine();
if(line2.isEmpty())
System.out.println("(server did not reply with a message)");
else{
System.out.println("Writing to file...:");
//String line= null;
file2.println( line2 );
file2.close();
}
System.out.println("Done!");
}
void close(){
try
{
client_socket.close();
}catch(Exception e){
//do nothing
}
}
}