-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
62 lines (54 loc) · 1.67 KB
/
Client.java
File metadata and controls
62 lines (54 loc) · 1.67 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
import java.io.*;
import java.net.*;
import java.util.*;
/**
* The Client class serves as the client portion of the
* Asteroids game. It connects to the Server in order to
* play with other players
*
* @author Herman Lin
* @author Devin Zhu
*/
public class Client {
Socket socket;
// DataInputStream sin;
// DataOutputStream sout;
Scanner sin;
PrintStream sout;
Client() {}
Client(Socket newSocket) {
try {
socket = newSocket;
// sin = new DataInputStream(newSocket.getInputStream());
// sout = new DataOutputStream(newSocket.getOutputStream());
sin = new Scanner(socket.getInputStream());
sout = new PrintStream(socket.getOutputStream());
} catch (IOException e) {}
}
public boolean connectTo(String address) {
try {
socket = new Socket(address, Server.DEFAULT_PORT);
// sin = new DataInputStream(socket.getInputStream());
// sout = new DataOutputStream(socket.getOutputStream());
sin = new Scanner(socket.getInputStream());
sout = new PrintStream(socket.getOutputStream());
return true;
} catch (Exception e) { return false; }
}
//read data from server
public String readFromServer() {
try {
// String data = sin.readUTF();
String data = sin.nextLine();
return data;
} catch (Exception e) {}
return "";
}
// write the ship object to the server
public void writeToServer(String data) {
try {
// sout.writeUTF(data);
sout.println(data);
} catch (Exception e) {}
}
}