TCP併發伺服器模型(一)

2021-07-10 11:02:53 字數 3691 閱讀 8346

本篇敘述的tcp併發伺服器模型如下圖所示:

伺服器建立並繫結套接字後fork出幾個子程序,子程序中分別進行accept(該函式為阻塞函式)、recv、處理資料然後再次acept,這樣迴圈下去。所有客戶端發來的資訊都是直接由子程序處理。

例程

**如下,在處理客戶端請求之前,伺服器先fork了3個子程序,然後將客戶端的請求直接交由子程序處理。

該例程中,伺服器fork子程序後,子程序監聽並接收客戶端的資訊,然後列印客戶端發來的資訊和自己的id(id代表自己是第幾個子程序)

伺服器端**:

/**************************************

author:arvik

purpose:test the server simultaneity

email:[email protected]

csdn:

**************************************/

#include

#include

#include

#include

#include

#include

#include

#include

#define bufflen 1024

#define server_port 8887

#define backlog 5

#define pidnumb 3

static

void handle_connect(int s_s, int id)

close(s_c);

}}void sig_int(int num)

int main(int argc, char **argv)

}sleep(100);

close(s_s);

return

0;}

客戶端**:

/**************************************

author:arvik

purpose:test the server simultaneity

email:[email protected]

csdn:

**************************************/

#include

#include

#include

#include

#include

#include

#include

#include

#define bufflen 24

#define server_port 8887

int main()

到此問個問題,

當乙個客戶端送來資訊後,到底是哪個子程序接收並處理資訊呢?伺服器端3個子程序會由於爭相接收理客戶端的連線而都得到連線權(或都得不到連線權)呢?

不知其解,故展開驗證:

1. 啟動伺服器後,我依次啟動了客戶端(每次都是等伺服器處理完上個請求後才啟動客戶端的),發現三個子程序依次輪流來接收資訊。

[root@f12 t1]# ./client

this process id

is: 0

reveive from client: hello

[root@f12 t1]#

[root@f12 t1]# ./client

this process id

is: 1

reveive from client: hello

[root@f12 t1]# ./client

this process id

is: 2

reveive from client: hello

[root@f12 t1]# ./client

this process id

is: 0

reveive from client: hello

同時啟動兩個客戶端

[1] 20805

this process id

is: 1

reveive from client: hello

this process id

is: 2

reveive from client: hello

[root@f12 t1]#

[1]+ done ./client

[root@f12 t1]#

同時啟動四個客戶端,這時發現處理訊息的伺服器子程序沒那麼規律了

[1] 20809

this process id

is: 1

reveive from client: hello

this process id

is: 1

reveive from client: hello

[2] 20810

[3] 20811

this process id

is: 2

reveive from client: hello

this process id

is: 1

reveive from client: hello

[1] done ./client

[2]- done ./client

[3]+ done ./client

[root@f12 t1]#

[root@f12 t1]#

[root@f12 t1]# ./client & ./client & ./client & ./client

[1] 20832

[2] 20833

[3] 20834

this process id

is: 0

reveive from client: hello

this process id

is: 2

reveive from client: hello

this process id

is: 1

reveive from client: hello

this process id

is: 0

reveive from client: hello

[1] done ./client

[root@f12 t1]#

[2]- done ./client

[3]+ done ./client

[root@f12 t1]#

由試驗現象可對上述問題做個簡單回答,作業系統在某一時刻會安排伺服器子程序中的乙個來處理客戶端發來的連線,對於每乙個客戶端的連線,伺服器子程序有且只有乙個接收並進行處理。

IO模型與TCP併發伺服器

io模型與tcp併發伺服器 io模型 1 阻塞io 2 非阻塞io 3 io多路復用 4 訊號驅動io 1 阻塞io 最常用 預設設定 io scanf printf 預設的io裝置 fgetc fputc 標準io fgets fputs fread fwrite read write 檔案io,...

TCP的高併發伺服器模型

單客戶端單程序,統一accept 原型介紹 此併發伺服器模型並不預先分叉程序,而是主程序統一處理客戶端的連線,當客戶端的請求到達時,才臨時fork 程序,由子程序處理客戶端請求。利用socket 函式建立套接字,呼叫bind 函式繫結位址,呼叫listen 函式來監聽佇列長度,然後進入主處理過程,等...

TCP併發伺服器

int main int recvcnt 0 struct sockaddr in sock server struct sockaddr in sock client int len sizeof struct sockaddr socketfd socket pf inet,sock strea...