Linux并發(fā)服務(wù)器模型二多線(xiàn)程

偽代碼實(shí)現(xiàn): typedef struct info
int fd; struct sockaddr_in addr;
{
}Info;
int main()
int i = 0; int fds[200]; // 創(chuàng)建監(jiān)聽(tīng) 的套接字 int lfd = socket(); // 綁定 bind(lfd, struct sockaddr_in, len) // 監(jiān)聽(tīng) listen(lfd, num);
while(1) {
// 等待并接受連接請(qǐng)求 int fds[i] = accept(lfd, &struct sockaddr_in, &len);
// 創(chuàng)建子線(xiàn)程 pthread_create(&threadId, NULL, do_child, (void*)&fds[i]);
// 設(shè)置線(xiàn)程分離 pthread_deatch(threadid); i++;
}

}
void* do_child(void* arg)
{
int cfd = (int)arg; // 與客戶(hù)端通信
// 與客戶(hù)端通信
recv(cfd, buf, len, 0);
printf(連接過(guò)來(lái)客戶(hù)端信息)
send();
}

需要注意:全局變量: 共享1. 如何做數(shù)據(jù)傳輸2. 如果線(xiàn)程之間有數(shù)據(jù)交互 -線(xiàn)程同步 3. 文件描述符的個(gè)數(shù)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容