-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java~
84 lines (62 loc) · 1.54 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
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
// client side program for connecting to server
import java.net.*;
import java.io.*;
import java.util.*;
public class Client implements Runnable {
private static Socket socket;
private static PrintStream out;
private static Output output;
private static ClientData data;
private static Object datalock;
public static void main(String[] args) {
try {
datalock=new Object();
data=new ClientData();
output=new GUI();
socket=new Socket("192.168.1.2",2000);
out=new PrintStream(socket.getOutputStream());
(new Thread(new Client())).start();
/*
Scanner sc=new Scanner(System.in);
while(sc.hasNextLine()) {
out.println(sc.nextLine());
out.flush();
}
*/
}
catch(IOException e) {
System.out.println("error: "+e);
System.exit(1);
}
} //main
public static void sendMessage(String s) {
out.println(s);
out.flush();
} //sendMessage
public void run() {
//handles text coming FROM socket
try {
Scanner in=new Scanner(socket.getInputStream());
while(in.hasNextLine()) {
String encoding=in.nextLine();
try {
ClientData temp=new ClientData(encoding);
synchronized(datalock) {
data=temp;
}
} catch(BadFormatException e) {
/* ** */System.out.println("bad: \""+encoding+"\"");
output.badUpdate();
}
}
}
catch(IOException e) {
System.err.println("error: "+e);
}
} //run
public static ClientData getData() {
synchronized(datalock) {
return data;
}
} //getData
} //class