-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTCPServerSocketImpl.java
30 lines (26 loc) · 1.02 KB
/
TCPServerSocketImpl.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
import java.net.DatagramPacket;
public class TCPServerSocketImpl extends TCPServerSocket {
private EnhancedDatagramSocket UDPSocket;
public TCPServerSocketImpl(int port) throws Exception {
super(port);
this.UDPSocket = new EnhancedDatagramSocket(port);
}
@Override
public TCPSocket accept() throws Exception {
DatagramPacket UDPPacket = null;
TCPPacket req = null;
while (req == null || !(req.getSYN() && !req.getACK())) {
if (req != null)
System.err.println("Invalid TCP Package");
byte[] data = new byte[this.UDPSocket.getPayloadLimitInBytes()];
UDPPacket = new DatagramPacket(data, data.length);
this.UDPSocket.receive(UDPPacket);
req = new TCPPacket(data);
}
return new TCPSocketImpl(UDPPacket.getAddress().getHostAddress(), UDPPacket.getPort(), req);
}
@Override
public void close() throws Exception {
this.UDPSocket.close();
}
}