一句一句的翻译一下这段代码public class Client int port = 8888; String ip = localhost; DataInputStream di = null; DataOutputStream dou = null; Socket client = null; socket 通常也称作套接字用于描述IP地址和端口是一个通信链的
public class Client { int port = 8888; String ip = "localhost"; DataInputStream di = null; DataOutputStream dou = null; Socket client = null;
// This is a constructor to initialize the socket
public Client() throws UnknownHostException, IOException {
client = new Socket(ip, port);
run();
}
// Transfer the seq value and add 1 to form the ack value
public String seqTransferAck(String s) {
String result = null;
int i = Integer.parseInt(s);
i = i + 1;
result = String.valueOf(i);
return result;
}
// Send messages to the server
public void run() throws IOException {
di = new DataInputStream(client.getInputStream());
dou = new DataOutputStream(client.getOutputStream());
// First handshake, data sent from client to server. seq=random number SYN=1 ACK=0
seq = new Random().nextInt(1000)+"";
ack = "0";
SYN = "1";
ACK = "0";
// Write the data to be sent to the server into the data output stream
dou.writeUTF(SYN);
dou.writeUTF(ACK);
dou.writeUTF(seq);
}
public String[] secendhand() throws Exception{
di = new DataInputStream(client.getInputStream());
dou = new DataOutputStream(client.getOutputStream());
// Second handshake, read data from the server
String str[] = new String[4];
serverSYN = di.readUTF();// SYN
serverACK = di.readUTF();// ACK
serverseq = di.readUTF();// seq
serverack = di.readUTF();//
str[0] = serverSYN;
str[1] = serverACK;
str[2] = serverseq;
str[3] = serverack;
return str;
}
public void secendPrint()throws Exception{
System.out.println("Second handshake");
System.out.println("ACK received from the server=" + serverSYN);
System.out.println("SYN received from the server=" + serverACK);
System.out.println("seq received from the server=" + serverseq);
System.out.println("ack received from the server=" + serverack);
// Third handshake, data sent from client to server. Set seq=serverack SYN=0 ACK=1 ack=seqTransferAck(serverseq)
ACK = "1";
SYN = "0";
ack = seqTransferAck(serverseq);
dou.writeUTF(ACK);
dou.writeUTF(SYN);
dou.writeUTF(serverack);
dou.writeUTF(ack);
}
//[start] Four-way handshake
/**
* Four-way handshake process to terminate the TCP connection
* First handshake: fin = 1, seq = u
* Second handshake: ACK = 1, seq = v, ack = u+1
* Third handshake: fin = 1, ACK=1, seq = w, ack = u+1
* Fourth handshake: ACK = 1, seq = u+1, ack = w+1
*/
public void fouthsend() throws Exception{
di = new DataInputStream(client.getInputStream());
dou = new DataOutputStream(client.getOutputStream());
seq = new Random().nextInt(1000)+"";
dou.writeUTF(fin);
dou.writeUTF(seq);
}
public String[] fifthhand() throws Exception{
di = new DataInputStream(client.getInputStream());
dou = new DataOutputStream(client.getOutputStream());
String str[] = new String[4];
RecACK2 = di.readUTF();
Recseq2 = di.readUTF();
Recack2 = di.readUTF();
str[0] = RecACK2;
str[1] = Recseq2;
str[2] = Recack2;
return str;
}
public void fifthPrint()throws Exception{
System.out.println("Second handshake");
System.out.println("ACK received from the server=" + RecACK2);
System.out.println("seq received from the server=" + Recseq2);
System.out.println("ack received from the server=" + Recack2);
}
public String[] sixthhand() throws Exception{
di = new DataInputStream(client.getInputStream());
dou = new DataOutputStream(client.getOutputStream());
String str[] = new String[4];
Recfin3 = di.readUTF();
Recseq3 = di.readUTF();
Recack3 = di.readUTF();
str[0] = Recfin3;
str[1] = Recseq3;
str[2] = Recack3;
return str;
}
public void sixthPrint()throws Exception{
System.out.println("Third handshake");
System.out.println("FIN received from the server=" + Recfin3);
System.out.println("seq received from the server=" + Recseq3);
System.out.println("ack received from the server=" + Recack3);
String RecACK4 = "1";
String Recseq4 = seqTransferAck(seq);
String Recack4 = seqTransferAck(Recseq3);
dou.writeUTF(RecACK4);
dou.writeUTF(Recseq4);
dou.writeUTF(Recack4);
}
原文地址: https://www.cveoy.top/t/topic/h4Vk 著作权归作者所有。请勿转载和采集!