国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

網(wǎng)絡(luò)編程 IO多路復(fù)用 [epoll版] (TCP網(wǎng)絡(luò)聊天室)

這篇具有很好參考價值的文章主要介紹了網(wǎng)絡(luò)編程 IO多路復(fù)用 [epoll版] (TCP網(wǎng)絡(luò)聊天室)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

//head.h? ? ? ? ? ? 頭文件

//TcpGrpSer.c? ? ?服務(wù)器端

//TcpGrpUsr.c? ? ?客戶端

通過IO多路復(fù)用實現(xiàn)服務(wù)器在單進程單線程下可以與多個客戶端交互

?API

epoll函數(shù)

#include<sys/epoll.h>
int epoll_create(int size);
功能:創(chuàng)建一個epoll句柄//創(chuàng)建紅黑樹根節(jié)點
epoll把要監(jiān)測的事件文件描述符掛載到紅黑樹上
參數(shù):size 沒有意義,但是必須>0
返回值:成功返回根節(jié)點對應(yīng)的文件描述符,失敗返回-1

int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
功能:實現(xiàn)對于epoll的控制
參數(shù):
epfd:epoll_create創(chuàng)建的句柄
op:控制方式
     EPOLL_CTL_ADD:添加要監(jiān)測的事件文件描述符
     EPOLL_CTL_MOD:修改epoll檢測的事件類型
     EPOLL_CTL_DEL:將文件描述符從epoll刪除
fd:要操作的文件描述符
event:事件結(jié)構(gòu)體
typedef union epoll_data {
               void        *ptr;
               int          fd;//使用這個
               uint32_t     u32;
               uint64_t     u64;
           } epoll_data_t;

 struct epoll_event {
               uint32_t     events; //EPOLLIN(讀) EPOLLOUT(寫)
               epoll_data_t data;        /* User data variable */
           };
返回值:成功返回0,失敗返回-1

int epoll_wait(int epfd, struct epoll_event *events,
                      int maxevents, int timeout);
功能:阻塞等待準(zhǔn)備好的文件描述符
參數(shù):
epfd:epoll句柄
events:存放就緒事件描述符的結(jié)構(gòu)體數(shù)組首地址
maxevents:監(jiān)聽的事件最大個數(shù)
timeout:超時檢測
    >0:毫秒級檢測
    ==0:立即返回
    -1:不關(guān)心是否超時

返回值:
>0:準(zhǔn)備好的文件描述符的個數(shù)
==0:超時
<0:失敗

?head.h

#ifndef __HEAD_H__
#define __HEAD_H__


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<math.h>
#include<errno.h>
#include<fcntl.h>
#include<signal.h>

#include<sys/stat.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/shm.h>
#include<sys/time.h>
#include<sys/sem.h>

#include<pthread.h>
#include<semaphore.h>

#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/select.h>
#include<poll.h>
#include<sys/epoll.h>
#include<sys/fcntl.h>


#define NUM 10
#define ERR_MSG(msg)                    \
	do                                  \
	{                                   \
		printf("line: %d\n", __LINE__); \
		perror(msg);                    \
	} while (0)

#define PORT 6666			// 端口號的網(wǎng)絡(luò)字節(jié)序  1024~49151
#define IP "192.168.250.100" // ifconfig查看本機IP  (ipv4)

#endif

TcpGrpSer.c

#include "head.h"



int main(int argc, const char *argv[])
{
	// 創(chuàng)建流式套接字
	int sfd = socket(AF_INET, SOCK_STREAM, 0);
	if (sfd < 0)
	{
		ERR_MSG("socket");
		return -1;
	}

	// 填充服務(wù)器自身的地址信息結(jié)構(gòu)體
	// 真實的地址信息結(jié)構(gòu)體根據(jù)地址族制定AF_INET ;
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;			 // 必須填充AF_INET
	sin.sin_port = htons(PORT);			 // 端口號的網(wǎng)絡(luò)字節(jié)序  1024~49151
	sin.sin_addr.s_addr = inet_addr(IP); // ifconfig查看本機IP

	int reuse = 1;
	if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) // 允許端口快速被重復(fù)使用
	{
		ERR_MSG("setsockopt");
		return -1;
	}
	// 綁定連接
	if (bind(sfd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
	{
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success\n");

	// 設(shè)置監(jiān)聽
	if (listen(sfd, 128) < 0)
	{
		ERR_MSG("listen");
		return -1;
	}

	struct epoll_event event;
	struct epoll_event events[10]; // 存放就緒事件描述符的數(shù)組
	char buf[128] = {0};

	// 創(chuàng)建epoll句柄
	int epfd = epoll_create(1);
	if (epfd < 0)
	{
		ERR_MSG("epoll_create");
		exit(-1);
	}

	// 添加準(zhǔn)備就緒事件進入epoll;
	event.events = EPOLLIN; // 讀事件
	event.data.fd = sfd;	// 監(jiān)聽套接字放入列表
	if (epoll_ctl(epfd, EPOLL_CTL_ADD, sfd, &event) < 0)
	{
		ERR_MSG("epoll_ctl");
		exit(-1);
	}
	event.events = EPOLLIN; // 讀事件
	event.data.fd = 0;		// 終端輸入套接字放入列表
	if (epoll_ctl(epfd, EPOLL_CTL_ADD, 0, &event) < 0)
	{
		ERR_MSG("epoll_ctl");
		exit(-1);
	}
	// 監(jiān)聽事件是否發(fā)生

	int s_res = 0;
	struct sockaddr_in cin;
	socklen_t len = sizeof(cin);
	struct sockaddr_in savcin[1024];
	int flags[1024] = {0};
	int newfd = -1;
	ssize_t res = 0;

	while (1)
	{
		// 監(jiān)測文件描述符是否準(zhǔn)備就緒
		// 如果成功,s_res接收返回的事件個數(shù),就緒的事件存儲到events數(shù)組中
		s_res = epoll_wait(epfd, events, 10, -1);
		if (s_res < 0)
		{
			ERR_MSG("select");
			return -1;
		}

		// 與客戶端通信
		for (int i = 0; i < s_res; i++)
		{
			if (events[i].events & EPOLLIN)
			{
				if (events[i].data.fd == sfd)
				{
					printf("客戶端連技事件\n");
					newfd = accept(sfd, (struct sockaddr *)&cin, &len);
					if (newfd < 0)
					{
						perror("accept");
						return -1;
					}
					savcin[newfd] = cin;
					flags[newfd] = 1;
					printf("[%s:%d] 客戶端連接成功 newfd = %d __%d__ \n",
						   inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, __LINE__);

					event.events = EPOLLIN | EPOLLET; // 讀事件
					event.data.fd = newfd;	// 新套接字放入鏈表
					if (epoll_ctl(epfd, EPOLL_CTL_ADD, newfd, &event) < 0)
					{
						ERR_MSG("epoll_ctl");
						exit(-1);
					}
				}

				else if (0 == events[i].data.fd)
				{
					printf("觸發(fā)鍵盤輸入事件\n");
				int sndfd=-1;
				res=scanf("%d %s",&sndfd,buf);
				while(getchar() !=10);
				if(res!=2)
				{
					printf("請輸入正確數(shù)據(jù)格式:[fd(4~1023)] string\n");
		
					continue;
				}
				//判斷文件是否合法
				if(flags[i])
				{
					printf("sndfd = %d 是非法文件描述符\n",sndfd);
					continue;
				}
				if(send(sndfd,buf,sizeof(buf),0)<0)
				{
					ERR_MSG("send");
				}
				bzero(buf,sizeof(buf));
				}
				else
				{

					printf("客戶端交互事件\n");
					// res = scanf("%s", buf);
					// while (getchar() != 10)
						res = recv(events[i].data.fd, buf, sizeof(buf), 0);
					if (res < 0)
					{
						ERR_MSG("send");
						return -1;
					}
					else if (0 == res)
					{
						printf("[%s:%d] 客戶端下線 newfd = %d __%d__ \n",
							   inet_ntoa(savcin[i].sin_addr), ntohs(savcin[i].sin_port), events[i].data.fd, __LINE__);

						close(i); // 關(guān)閉文件描述符
						flags[i] = 0;
					
					}
					printf("[%s:%d] 客戶端 newfd = %d : %s, __%d__ \n",
						   inet_ntoa(savcin[i].sin_addr), ntohs(savcin[i].sin_port), events[i].data.fd, buf, __LINE__);
				}
			}
		}
	}

	if (close(sfd) < 0)
	{
		ERR_MSG("close");
		return -1;
	}

	return 0;
}

TcpGrpUsr.c

#include "head.h"


int main(int argc, const char *argv[])
{
	//創(chuàng)建流式套接字
	int sfd = socket(AF_INET,SOCK_STREAM,0);
	if(sfd<0)
	{
		ERR_MSG("socket");
		return -1;
	}

	int reuse = 1;
	if(setsockopt(sfd, SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0) //允許端口快速被重復(fù)使用
	{
		ERR_MSG("setsockopt");
		return -1;
	}
	//填充服務(wù)器自身的地址信息結(jié)構(gòu)體
	//真實的地址信息結(jié)構(gòu)體根據(jù)地址族制定AF_INET ; man 7 ip
	struct sockaddr_in sin; 
	sin.sin_family      = AF_INET;            //必須填充AF_INET
	sin.sin_port        = htons(PORT);        //端口號的網(wǎng)絡(luò)字節(jié)序  1024~49151
	sin.sin_addr.s_addr = inet_addr(IP);      //ifconfig查看本機IP

	if(connect(sfd,(struct sockaddr *)&sin,sizeof(sin))<0)
	{
		perror("connect");
		return -1;
	}
	printf("連接成功\n");

	//創(chuàng)建集合
	struct pollfd fds[2];

	fds[0].fd = 0;
	fds[0].events = POLLIN;
	fds[1].fd = sfd;
	fds[1].events = POLLIN;

	char buf[128]="";
	int res=0;

	while(1)
	{
		//阻塞方式監(jiān)測集合
		res = poll(fds,2,-1);
		if(res < 0)
		{
			ERR_MSG("poll");
			return -1;
		}
		else if(0 == res)
		{
			printf("time out...\n");      //超時
			break;
		}

		//判斷0文件描述符是否右POLLIN事件
		if((fds[0].revents & POLLIN))
		{
			fgets(buf,sizeof(buf),stdin);
			buf[strlen(buf)-1] = 0;
			if(send(sfd,buf,sizeof(buf),0) < 0)
			{
				ERR_MSG("send");
				return -1;
			}
			printf("發(fā)送成功\n");
		}
		//判斷sfd文件描述符是否右POLLIN事件
		if(fds[1].revents & POLLIN)
		{
			//接收數(shù)據(jù)
			bzero(buf,sizeof(buf));
			res = recv(sfd,buf,sizeof(buf),0);
			if(res<0)
			{
				ERR_MSG("recv");
				return -1;
			}
			else if(res == 0)
			{
				printf("[%s:%d] 服務(wù)器下線__%d__ \n",\
					inet_ntoa(sin.sin_addr),ntohs(sin.sin_port),__LINE__);
				break;
			}
			printf("[%s:%d] cfd = %d : %s__%d__ \n",\
					inet_ntoa(sin.sin_addr),ntohs(sin.sin_port),sfd,buf,__LINE__);
		}	
	}

	if(close(sfd)<0)
	{
		ERR_MSG("close");
		return -1;
	}

	return 0;
}

網(wǎng)絡(luò)編程 IO多路復(fù)用 [epoll版] (TCP網(wǎng)絡(luò)聊天室),網(wǎng)絡(luò)編程,網(wǎng)絡(luò),php,開發(fā)語言?文章來源地址http://www.zghlxwxcb.cn/news/detail-665075.html

到了這里,關(guān)于網(wǎng)絡(luò)編程 IO多路復(fù)用 [epoll版] (TCP網(wǎng)絡(luò)聊天室)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 【高并發(fā)網(wǎng)絡(luò)通信架構(gòu)】3.引入IO多路復(fù)用(select,poll,epoll)實現(xiàn)高并發(fā)tcp服務(wù)端

    【高并發(fā)網(wǎng)絡(luò)通信架構(gòu)】3.引入IO多路復(fù)用(select,poll,epoll)實現(xiàn)高并發(fā)tcp服務(wù)端

    目錄 一,往期文章 二,基本概念 IO多路復(fù)用 select 模型 poll 模型 epoll 模型 select,poll,epoll 三者對比 三,函數(shù)清單 1.select 方法 2.fd_set 結(jié)構(gòu)體 3.poll 方法 4.struct pollfd 結(jié)構(gòu)體 5.epoll_create 方法 6.epoll_ctl 方法 7.epoll_wait 方法 8.struct epoll_event 結(jié)構(gòu)體 四,代碼實現(xiàn) select 操作流程 s

    2024年02月14日
    瀏覽(25)
  • BIO、NIO、IO多路復(fù)用模型詳細(xì)介紹&Java NIO 網(wǎng)絡(luò)編程

    BIO、NIO、IO多路復(fù)用模型詳細(xì)介紹&Java NIO 網(wǎng)絡(luò)編程

    上文介紹了網(wǎng)絡(luò)編程的基礎(chǔ)知識,并基于 Java 編寫了 BIO 的網(wǎng)絡(luò)編程。我們知道 BIO 模型是存在巨大問題的,比如 C10K 問題,其本質(zhì)就是因其阻塞原因,導(dǎo)致如果想要承受更多的請求就必須有足夠多的線程,但是足夠多的線程會帶來內(nèi)存占用問題、CPU上下文切換帶來的性能問題

    2024年02月14日
    瀏覽(29)
  • 使用Linux系統(tǒng)IO多路復(fù)用中eopll創(chuàng)建基于TCP通信協(xié)議的多人聊天室

    一.1.搭建好TCP的通信模型 2.創(chuàng)建紅黑樹根節(jié)點?3.將套接字事件添加到紅黑樹中,使其被監(jiān)聽 4.當(dāng)套接字事件發(fā)生,表示有客戶端連接,將連接事件加入到紅黑樹節(jié)點當(dāng)中 5.每當(dāng)連接事件發(fā)生時,表示客戶端發(fā)送信息到服務(wù)器 6.每當(dāng)有事件準(zhǔn)備就緒時,將對應(yīng)的紅黑樹節(jié)點信息

    2024年02月13日
    瀏覽(19)
  • 【TCP服務(wù)器的演變過程】使用IO多路復(fù)用器epoll實現(xiàn)TCP服務(wù)器

    【TCP服務(wù)器的演變過程】使用IO多路復(fù)用器epoll實現(xiàn)TCP服務(wù)器

    手把手教你從0開始編寫TCP服務(wù)器程序,體驗開局一塊磚,大廈全靠壘。 為了避免篇幅過長使讀者感到乏味,對【TCP服務(wù)器的開發(fā)】進行分階段實現(xiàn),一步步進行優(yōu)化升級。 本節(jié),在上一章節(jié)的基礎(chǔ)上,將IO多路復(fù)用機制select改為更高效的IO多路復(fù)用機制epoll,使用epoll管理每

    2024年01月17日
    瀏覽(16)
  • Linux多路IO復(fù)用:epoll

    Linux多路IO復(fù)用:epoll

    ? ? ? ? epoll是為克服select、poll每次監(jiān)聽都需要在用戶、內(nèi)核空間反復(fù)拷貝,以及需要用戶程序自己遍歷發(fā)現(xiàn)有變化的文件描述符的缺點的多路IO復(fù)用技術(shù)。 epoll原理 創(chuàng)建內(nèi)核空間的紅黑樹; 將需要監(jiān)聽的文件描述符上樹; 內(nèi)核監(jiān)聽紅黑樹上文件描述符的變化; 返回有變化

    2024年02月04日
    瀏覽(21)
  • 驅(qū)動開發(fā),IO多路復(fù)用實現(xiàn)過程,epoll方式

    驅(qū)動開發(fā),IO多路復(fù)用實現(xiàn)過程,epoll方式

    被稱為當(dāng)前時代最好用的io多路復(fù)用方式; 核心操作:一棵樹(紅黑樹)、一張表(內(nèi)核鏈表)以及三個接口; ?思想:(fd代表文件描述符) ????????epoll要把檢測的事件fd掛載到內(nèi)核空間紅黑樹上,遍歷紅黑樹,調(diào)用每個fd對應(yīng)的操作方法,找到發(fā)生事件的fd,如果沒有發(fā)

    2024年02月07日
    瀏覽(30)
  • IO多路復(fù)用之select/poll/epoll

    IO多路復(fù)用之select/poll/epoll

    掌握select編程模型,能夠?qū)崿F(xiàn)select版本的TCP服務(wù)器. 掌握poll編程模型,能夠?qū)崿F(xiàn)poll版本的TCP服務(wù)器. 掌握epoll的編程模型,能夠?qū)崿F(xiàn)epoll版本的TCP服務(wù)器. epoll的LT模式和ET模式. 理解select和epoll的優(yōu)缺點對比. 提示:以下是本篇文章正文內(nèi)容,下面案例可供參考 多路轉(zhuǎn)接天然的是讓我

    2023年04月09日
    瀏覽(20)
  • Linux網(wǎng)絡(luò)編程:多路I/O轉(zhuǎn)接服務(wù)器(select poll epoll)

    Linux網(wǎng)絡(luò)編程:多路I/O轉(zhuǎn)接服務(wù)器(select poll epoll)

    文章目錄: 一:select 1.基礎(chǔ)API? select函數(shù) 思路分析 select優(yōu)缺點 2.server.c 3.client.c 二:poll 1.基礎(chǔ)API? poll函數(shù)? poll優(yōu)缺點 read函數(shù)返回值 突破1024 文件描述符限制 2.server.c 3.client.c 三:epoll 1.基礎(chǔ)API epoll_create創(chuàng)建? ?epoll_ctl操作? epoll_wait阻塞 epoll實現(xiàn)多路IO轉(zhuǎn)接思路 epoll優(yōu)缺點

    2024年02月11日
    瀏覽(23)
  • 【APUE】網(wǎng)絡(luò)socket編程溫度采集智能存儲與上報項目技術(shù)------多路復(fù)用

    【APUE】網(wǎng)絡(luò)socket編程溫度采集智能存儲與上報項目技術(shù)------多路復(fù)用

    作者簡介: 一個平凡而樂于分享的小比特,中南民族大學(xué)通信工程專業(yè)研究生在讀,研究方向無線聯(lián)邦學(xué)習(xí) 擅長領(lǐng)域:驅(qū)動開發(fā),嵌入式軟件開發(fā),BSP開發(fā) 作者主頁:一個平凡而樂于分享的小比特的個人主頁 文章收錄專欄:網(wǎng)絡(luò)socket編程之溫度采集智能存儲與上報項目,本

    2024年04月10日
    瀏覽(34)
  • 網(wǎng)絡(luò)編程代碼實例:IO復(fù)用版

    網(wǎng)絡(luò)編程代碼實例:IO復(fù)用版

    網(wǎng)絡(luò)編程代碼實例:IO復(fù)用版。 yezhening/Environment-and-network-programming-examples: 環(huán)境和網(wǎng)絡(luò)編程實例 (github.com) Environment-and-network-programming-examples: 環(huán)境和網(wǎng)絡(luò)編程實例 (gitee.com) 使用傳輸控制協(xié)議(TCP) 服務(wù)端多進程,一個服務(wù)端可連接多個客戶端 用戶在客戶端終端輸入,可多次

    2024年02月02日
    瀏覽(20)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包