oppo校招笔试题
題型:選擇題(單選和多選)+問答題+編程題(2道)
問答題:
1.簡述一下TCP三次握手,為什么是三次握手?
(1)第一次握手:Client將標志位SYN置為1,隨機產生一個值seq=J,并將該數據包發送給Server,Client進入SYN_SENT狀態,等待Server確認。
? (2)第二次握手:Server收到數據包后由標志位SYN=1知道Client請求建立連接,Server將標志位SYN和ACK都置為1,ack=J+1,隨機產生一個值seq=K,并將該數據包發送給Client以確認連接請求,Server進入SYN_RCVD狀態。
? (3)第三次握手:Client收到確認后,檢查ack是否為J+1,ACK是否為1,如果正確則將標志位ACK置為1,ack=K+1,并將該數據包發送給Server,Server檢查ack是否為K+1,ACK是否為1,如果正確則連接建立成功,Client和Server進入ESTABLISHED狀態,完成三次握手,隨后Client與Server之間可以開始傳輸數據了。
為什么是三次握手?
為什么連接建立需要三次握手,而不是兩次握手?
防止失效的連接請求報文段被服務端接收,從而產生錯誤。
PS:失效的連接請求:若客戶端向服務端發送的連接請求丟失,客戶端等待應答超時后就會再次發送連接請求,此時,上一個連接請求就是『失效的』。
若建立連接只需兩次握手,客戶端并沒有太大的變化,仍然需要獲得服務端的應答后才進入ESTABLISHED狀態,而服務端在收到連接請求后就進入ESTABLISHED狀態。此時如果網絡擁塞,客戶端發送的連接請求遲遲到不了服務端,客戶端便超時重發請求,如果服務端正確接收并確認應答,雙方便開始通信,通信結束后釋放連接。此時,如果那個失效的連接請求抵達了服務端,由于只有兩次握手,服務端收到請求就會進入ESTABLISHED狀態,等待發送數據或主動發送數據。但此時的客戶端早已進入CLOSED狀態,服務端將會一直等待下去,這樣浪費服務端連接資源。
作者:大閑人柴毛毛
鏈接:https://www.zhihu.com/question/24853633/answer/254224088
來源:知乎
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。
SYN攻擊:
? 在三次握手過程中,Server發送SYN-ACK之后,收到Client的ACK之前的TCP連接稱為半連接(half-open connect),此時Server處于SYN_RCVD狀態,當收到ACK后,Server轉入ESTABLISHED狀態。SYN攻擊就是Client在短時間內偽造大量不存在的IP地址,并向Server不斷地發送SYN包,Server回復確認包,并等待Client的確認,由于源地址是不存在的,因此,Server需要不斷重發直至超時,這些偽造的SYN包將產時間占用未連接隊列,導致正常的SYN請求因為隊列滿而被丟棄,從而引起網絡堵塞甚至系統癱瘓。SYN攻擊時一種典型的DDOS攻擊,檢測SYN攻擊的方式非常簡單,即當Server上有大量半連接狀態且源IP地址是隨機的,則可以斷定遭到SYN攻擊了,使用如下命令可以讓之現行:
? #netstat -nap | grep SYN_RECV
生產者消費者代碼:
public class ShengchanzheXiaofeizhe {private final int MAX_SIZE;private int count;public ShengchanzheXiaofeizhe(int n){MAX_SIZE=n;count=0;}public synchronized void add(){while(count>=MAX_SIZE){System.out.println("倉庫已經滿了");try{this.wait();}catch(InterruptedException e){e.printStackTrace();}}count++;System.out.println(Thread.currentThread().toString()+"put"+count);this.notifyAll();}public synchronized void remove(){while(count<=0){System.out.println("倉庫已經空了");try{this.wait();}catch(InterruptedException e){e.printStackTrace();}}System.out.println(Thread.currentThread().toString()+"get"+count);count--;this.notify();}public static void main(String []args){ShengchanzheXiaofeizhe s=new ShengchanzheXiaofeizhe(5);Thread pro=new Producer(s);Thread con=new Consumer(s);Thread pro2=new Producer(s);Thread con2=new Consumer(s);pro.setName("生產者1");pro2.setName("生產者2");con.setName("消費者1");con2.setName("消費者2");pro.start();pro2.start();con.start();con2.start();}} class Producer extends Thread{private ShengchanzheXiaofeizhe s;public Producer(ShengchanzheXiaofeizhe s){this.s=s;}public void run(){while(true){s.add();try{Thread.sleep(1000);}catch(InterruptedException e){e.printStackTrace();}}} } class Consumer extends Thread{private ShengchanzheXiaofeizhe s;public Consumer(ShengchanzheXiaofeizhe s){this.s=s;}public void run(){while(true){s.remove();try{Thread.sleep(1500);}catch(InterruptedException e){e.printStackTrace();}}} }隨機產生10個數字比如(12,32,2323,...10),變成(21,23,3232,....1),再降序輸出。
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Scanner; import java.util.Stack; public class OPPO01 {public static void main(String[] args) {int[] s1=randomNumber(1,1000,10);int[] s2=new int[10];for(int i=0;i<10;i++){int temp=0;s2[i]=0;while(s1[i]/10!=0){temp=s1[i]%10;s2[i]=(s2[i]+temp)*10;s1[i]=s1[i]/10;}s2[i]=s2[i]+s1[i]%10;}sort(s2);for(int i=0;i<10;i++){System.out.println(s2[i]);}}public static int[] sort(int[] s){for(int i=1;i<s.length;i++){int j;int e=s[i];for(j=i-1;j>=0&&s[j]<e;j--){s[j+1]=s[j];}s[j+1]=e;}return s;}public static int[] randomNumber(int min,int max,int n){//判斷是否已經達到索要輸出隨機數的個數if(n>(max-min+1) || max <min){return null;}int[] result = new int[n]; //用于存放結果的數組int count = 0;while(count <n){int num = (int)(Math.random()*(max-min))+min;boolean flag = true;for(int j=0;j<count;j++){if(num == result[j]){flag = false;break;}}if(flag){result[count] = num;count++;}}return result; } }?
總結
- 上一篇: 图像/图片怎么resize?
- 下一篇: jQuery-fancybox图片预览