-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySP.java
More file actions
80 lines (64 loc) · 1.55 KB
/
MySP.java
File metadata and controls
80 lines (64 loc) · 1.55 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
// Nico Feld 1169233
package Topologie;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.Socket;
public class MySP {
static abstract class MySP_Message implements Serializable {
private static final long serialVersionUID = 1L;
}
static class MySP_Msg_Message extends MySP_Message{
int id;
String msg;
public MySP_Msg_Message(int i, String s) {
id = i;
msg = s;
}
}
static class MySP_Msg_Disconnect extends MySP_Message {
private static final long serialVersionUID = 1L;
}
static class MySP_Msg_GetIds extends MySP_Message{
}
static class MySP_Msg_Name extends MySP_Message{
String s;
public MySP_Msg_Name(String name) {
s = name;
}
}
ObjectOutputStream objOut;
ObjectInputStream objIn;
Socket socket;
String serverIP;
int serverPort;
MySP(Socket s) {
serverIP = s.getInetAddress().toString();
serverPort = s.getPort();
socket = s;
try {
objOut = new ObjectOutputStream(socket.getOutputStream());
objIn = new ObjectInputStream(socket.getInputStream());
} catch (IOException e) {
System.out.println("Could not open IO streams for server");
System.out.println(e.getMessage());
System.exit(1);
}
}
MySP_Message receive() {
try {
return (MySP_Message) objIn.readObject();
} catch (ClassNotFoundException | IOException e) {
return null;
}
}
void send(MySP_Message km) {
try {
objOut.writeObject(km);
objOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}