-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.java
59 lines (53 loc) · 1.29 KB
/
Client.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
import java.io.*;
import java.net.*;
import java.util.*;
public class Client {
private static Socket socket = null;
public Client(){}
public static void setUp(String hostName, int portNumber){
try{
socket = new Socket(hostName, portNumber);
} catch(Exception e){
System.out.println("Socket error: " + e.getMessage());
}
}
public static void write(String msg){
PrintWriter out = null;
try{
out = new PrintWriter(socket.getOutputStream());
System.out.println("to be sent:"+msg);
out.print(msg);
out.flush();
} catch (Exception e){
System.out.println("Write error: " + e.getMessage());
}
}
public static String read(){
BufferedReader receiveBuf = null;
String inStr = null;
try{
receiveBuf = new BufferedReader(new InputStreamReader(socket.getInputStream()));
inStr = receiveBuf.readLine();
}catch(Exception e){
System.out.println("Read error: " + e.getMessage());
}
return inStr;
}
/*
public static void main(String[] args){
// SET UP SOCKET CONNECTION
setUp("localhost", 5000);
// READ AND WRITE
Scanner sc = new Scanner(System.in);
while (true){
// write to socket
String msg = sc.nextLine();
write(msg);
//read from socket
String inStr = read();
System.out.println("Received:"+inStr);
}
}
*
*/
}