-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTCPDecoder.java
31 lines (29 loc) · 1.25 KB
/
TCPDecoder.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
import java.nio.ByteBuffer;
import java.util.*;
public class TCPDecoder {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
byte[] data = new byte[str.length() / 2];
for (int i = 0; i < str.length(); i += 2) {
data[i / 2] = (byte) ((Character.digit(str.charAt(i), 16) << 4)
+ (byte) Character.digit(str.charAt(i + 1), 16));
}
TCPPacket packet = new TCPPacket(data);
System.out.println("sequenceNumber:\t\t" + packet.getSequenceNumber());
System.out.println("acknowledgementNumber:\t" + packet.getAcknowledgementNumber());
System.out.println("ACK:\t\t\t" + packet.getACK());
System.out.println("SYN:\t\t\t" + packet.getSYN());
System.out.println("dataLength:\t\t\t" + packet.getDataLength());
System.out.println("data (as Hex):\t\t" + Integer.toHexString(toInt(packet.getData())));
System.out.println("data (as String):\t" + new String(packet.getData()));
}
private static int toInt(byte[] bytes) {
int ret = 0;
for (byte b : bytes) {
ret <<= 8;
ret |= (int) b & 0xFF;
}
return ret;
}
}