-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathClient.java
More file actions
105 lines (91 loc) · 2.35 KB
/
Client.java
File metadata and controls
105 lines (91 loc) · 2.35 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
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
/*
* Client.java
*
* Created on 21 ãÇÑÓ, 2008, 09:23 Õ
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author Mohamed Talaat Saad
*/
public class Client {
/**
* Creates a new instance of Client
*/
private Socket clientSocket;
private String hostName;
private int serverPort;
private DataInputStream reader;
private DataOutputStream writer;
private Protocol protocol;
private static Client client;
private Client() throws IOException
{
protocol=new Protocol();
}
public void register(String Ip,int port,int posX,int posY) throws IOException
{
this.serverPort=port;
this.hostName=Ip;
clientSocket=new Socket(Ip,port);
writer=new DataOutputStream(clientSocket.getOutputStream());
writer.writeUTF(protocol.RegisterPacket(posX,posY));
}
public void sendToServer(String message)
{
if(message.equals("exit"))
System.exit(0);
else
{
try {
Socket s=new Socket(hostName,serverPort);
System.out.println(message);
writer=new DataOutputStream(s.getOutputStream());
writer.writeUTF(message);
} catch (IOException ex) {
}
}
}
public Socket getSocket()
{
return clientSocket;
}
public String getIP()
{
return hostName;
}
public static Client getGameClient()
{
if(client==null)
try {
client=new Client();
} catch (IOException ex) {
ex.printStackTrace();
}
return client;
}
public void closeAll()
{
try {
reader.close();
writer.close();
clientSocket.close();
} catch (IOException ex) {
}
}
}