-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnection.java
More file actions
42 lines (35 loc) · 1.08 KB
/
Copy pathConnection.java
File metadata and controls
42 lines (35 loc) · 1.08 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
import java.io.Closeable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.SocketAddress;
public class Connection implements Closeable{
private final Socket socket;
private final ObjectOutputStream out;
private final ObjectInputStream in;
public Connection(Socket socket) throws IOException{
this.socket = socket;
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
}
public void send(Message message) throws IOException{
synchronized (out) {
out.writeObject(message);
}
}
public Message receive() throws IOException, ClassNotFoundException{
synchronized (in){
return (Message) in.readObject();
}
}
public SocketAddress getRemoteSocketAddress(){
return socket.getRemoteSocketAddress();
}
@Override
public void close() throws IOException{
socket.close();
out.close();
in.close();
}
}