-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNetwork.java
181 lines (162 loc) · 4.04 KB
/
Network.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import java.io.*;
import java.net.*;
/**
*
*/
/**
* @author Simon
* Klasse um netzwerk verbindungsaufbau und datenuebertragung zu handlen
*/
public class Network {
public static String hostname = "127.0.0.1";
private boolean host = false;
private ServerSocket server;
private Socket client;
private DataOutputStream out;
private DataInputStream in;
private ObjectOutputStream oos;
private ObjectInputStream ois;
private ObjectInputStream ois2;
private ObjectOutputStream oos2;
private boolean connected = false;
public Network(boolean hosting) {
host = hosting;
if(host) {
try {
server = new ServerSocket(12346);
server.setSoTimeout(100);
} catch ( InterruptedIOException e1 ) {
e1.printStackTrace();
} catch (SocketException e2) {
e2.printStackTrace();
} catch (IOException e3) {
e3.printStackTrace();
}
} else {
try {
client = new Socket(hostname,12346);
out = new DataOutputStream(client.getOutputStream());
ois2 = new ObjectInputStream(client.getInputStream());
ois= new ObjectInputStream(client.getInputStream());
connected = true;
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void destroy() {
if(host) {
try {
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(client != null) {
try {
client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else {
try {
client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public boolean isHost() {
return host;
}
public void setHost(boolean host) {
this.host = host;
}
public boolean isConnected() {
return connected;
}
public void setConnected(boolean connected) {
this.connected = connected;
}
public boolean pollConnect() {
if(host) {
if(connected) return true;
try{
client = server.accept();
connected = true;
oos2 = new ObjectOutputStream(client.getOutputStream());
in = new DataInputStream(client.getInputStream());
oos = new ObjectOutputStream(client.getOutputStream());
} catch (SocketTimeoutException e) {
connected = false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connected;
} else {
return true;
}
}
public void send(int num) {
try {
out.write(num);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void sendmap (Feld[][] spielfeld, int[] coords){
try {
oos2.reset();
oos2.writeObject(coords);
oos2.flush();
oos.reset();
oos.writeObject(spielfeld);
oos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int recv() {
int out = -1;
try {
if (this.isHost()){
while(in.available()>0) {
out = in.read();
switch(out){
case 1: Game.getPlayer(1).setRightMovement(true);break;
case 2: Game.getPlayer(1).setLeftMovement(true);break;
case 3: Game.getPlayer(1).setUpMovement(true);break;
case 4: Game.getPlayer(1).setDownMovement(true);break;
case 5: Game.getPlayer(1).setPlantingBomb(true);break;
case 6: Game.getPlayer(1).setUsingSpecials(true);break;
}
}
}
if (!(this.isHost())){
int[] coordsInput=(int[]) ois2.readObject();
Game.spielfeld= (Feld[][]) ois.readObject();
if (0==coordsInput[0] && 0==coordsInput[1] && 0==coordsInput[2] && 0==coordsInput[3]){
System.exit(1);
}
Game.getPlayer(0).setx( coordsInput[0]);
Game.getPlayer(0).sety( coordsInput[1]);
Game.getPlayer(1).setx( coordsInput[2]);
Game.getPlayer(1).sety( coordsInput[3]);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e){}
return out;
}
}