應(yīng)用程序:文章來源:http://www.zghlxwxcb.cn/news/detail-617343.html
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <fcntl.h>
#include <stdlib.h>
#define ERR_MSG(msg) \
do \
{ \
printf("line:%d\n", __LINE__); \
perror(msg); \
} while (0)
#define PORT 6666 //端口號(hào)
#define IP "192.168.250.100" // IP地址
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;
}
printf("socket create success sfd=%d __%d__\n", sfd, __LINE__);
//設(shè)置端口快速被復(fù)用
int reuse = 1;
if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
//填充服務(wù)器自身的地址信息結(jié)構(gòu)體
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
//存儲(chǔ)客戶端信息結(jié)構(gòu)體
int s_res = 0;
struct sockaddr_in cin;
socklen_t len = sizeof(cin);
struct sockaddr_in savecin[1024];
//將IP地址和端口號(hào)綁定到指定的套接字文件描述符上
if (bind(sfd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success __%d__\n", __LINE__);
//將套接字設(shè)置為被動(dòng)監(jiān)聽狀態(tài)
if (listen(sfd, 128) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success __%d__\n", __LINE__);
char buf[128] = "";
ssize_t res = 0;
// epoll
int fd, newfd, epfd;
struct epoll_event event;
struct epoll_event events[10]; //存放就緒事件描述符的數(shù)組
//創(chuàng)建epoll句柄
epfd = epoll_create(1);
if (epfd < 0)
{
printf("epoll_create filed\n");
exit(-1);
}
event.events = EPOLLIN;
event.data.fd = sfd;
if (epoll_ctl(epfd, EPOLL_CTL_ADD, sfd, &event) < 0)
{
printf("epoll_ctl add filed\n");
}
//監(jiān)聽事件是否發(fā)生
while (1)
{
//等待事件發(fā)生
int ret = epoll_wait(epfd, events, 10, -1);
if (ret < 0)
{
printf("epoll_wait filed\n");
exit(-1);
}
int i;
//循環(huán)遍歷數(shù)組,做事件的處理
for (i = 0; i < ret; i++)
{
//監(jiān)聽到新連接
if (events[i].data.fd == sfd)
{
newfd = accept(sfd, (struct sockaddr *)&cin, &len);
if (newfd < 0)
{
perror("accept");
return -1;
}
printf("[%s:%d] 客戶端連接成功 newfd=%d __%d__\n",
inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, __LINE__);
savecin[newfd] = cin;
//新客戶端套接字加入epoll
event.events = EPOLLIN;
event.data.fd = newfd;
if (epoll_ctl(epfd, EPOLL_CTL_ADD, newfd, &event) < 0)
{
printf("epoll_ctl add filed\n");
}
}
else if (events[i].data.fd == 0) //鍵盤輸入事件
{
printf("觸發(fā)鍵盤輸入事件 __%d__\n", __LINE__);
int sndfd = -1;
res = scanf("%d %s", &sndfd, buf);
while (getchar() != 10)
;
if (res != 2)
{
printf("請輸入正確數(shù)據(jù)格式:fd string\n");
continue;
}
}
else
{
//客戶端交互事件
printf("觸發(fā)客戶端交互事件__%d__\n", __LINE__);
bzero(buf, sizeof(buf));
//接收數(shù)據(jù)
res = recv(events[i].data.fd, buf, sizeof(buf), 0);
if (res < 0)
{
ERR_MSG("recv");
return -1;
}
else if (0 == res)
{
printf("[%s:%d] 客戶端下線 newfd=%d __%d__\n",
inet_ntoa(savecin[i].sin_addr), ntohs(savecin[i].sin_port), i, __LINE__);
close(i); //未接收到數(shù)據(jù),關(guān)閉文件描述符
continue; //進(jìn)入下一次循環(huán)
}
//發(fā)送數(shù)據(jù)
strcat(buf, "*_*");
if (send(events[i].data.fd, buf, sizeof(buf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("發(fā)送成功\n");
}
}
}
if (close(sfd) < 0)
{
ERR_MSG("close");
return -1;
}
return 0;
}
驅(qū)動(dòng)程序:文章來源地址http://www.zghlxwxcb.cn/news/detail-617343.html
#include <linux/init.h>
#include <linux/module.h>
#include<linux/fs.h>
#include<linux/io.h>
#include<linux/device.h>
#include<linux/uaccess.h>
#include<linux/poll.h>
struct class *cls;
struct device *dev;
unsigned int major;//定義一個(gè)變量保存主設(shè)備號(hào)
char kbuf[128]={0};
//定義等待隊(duì)列頭
wait_queue_head_t wq_head;
int condition=0;//數(shù)據(jù)是否準(zhǔn)備好的標(biāo)志變量
//封裝操作方法
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
if(size>sizeof(kbuf))//用戶的需求內(nèi)核滿足不了
{
size=sizeof(kbuf);
}
long ret;
ret=copy_to_user(ubuf,kbuf,size);
if(ret)
{
printk("copy_to_user filed\n");
return -EIO;
}
condition=0;//表示下一次數(shù)據(jù)沒有準(zhǔn)備好
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
if(size>sizeof(kbuf))//用戶的需求內(nèi)核滿足不了
{
size=sizeof(kbuf);
}
long ret;
ret=copy_from_user(kbuf,ubuf,size);//表示模擬硬件數(shù)據(jù)就緒
if(ret)
{
printk("copy_from_user filed\n");
return -EIO;
}
condition=1;
wake_up_interruptible(&wq_head);//喚醒休眠的進(jìn)程
return 0;
}
//封裝poll方法
__poll_t mycdev_poll(struct file *file, struct poll_table_struct *wait)
{
__poll_t mask=0;
//1.向上提交等待隊(duì)列頭
poll_wait(file,&wq_head,wait);
//2.根據(jù)事件是否發(fā)生給一個(gè)合適的返回值
if(condition)
{
mask=POLLIN;
}
return mask;
}
int mycdev_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
//定義一個(gè)操作方法結(jié)構(gòu)體變量并且初始化
struct file_operations fops={
.open=mycdev_open,
.release=mycdev_close,
.read=mycdev_read,
.poll=mycdev_poll,
.write=mycdev_write,
};
static int __init mycdev_init(void)
{
//初始化等待隊(duì)列頭
init_waitqueue_head(&wq_head);
//注冊字符設(shè)備驅(qū)動(dòng)
major=register_chrdev(0,"mychrdev",&fops);
if(major<0)
{
printk("注冊字符設(shè)備驅(qū)動(dòng)失敗\n");
return major;
}
printk("注冊字符設(shè)備驅(qū)動(dòng)成功major=%d\n",major);
// 向上提交目錄
cls = class_create(THIS_MODULE, "myled");
if (IS_ERR(cls))
{
printk("向上提交目錄失敗\n");
return -PTR_ERR(cls);
}
printk("向上提交目錄信息成功\n");
// 向上提交設(shè)備節(jié)點(diǎn)信息
dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "mycdev");
if (IS_ERR(dev))
{
printk("向上提交設(shè)備節(jié)點(diǎn)信息失敗\n");
return -PTR_ERR(dev);
}
printk("向上提交設(shè)備節(jié)點(diǎn)成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
// 銷毀節(jié)點(diǎn)信息
device_destroy(cls, MKDEV(major, 0));
// 銷毀目錄信息
class_destroy(cls);
//注銷字符設(shè)備驅(qū)動(dòng)
unregister_chrdev(major,"mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
到了這里,關(guān)于epoll多路復(fù)用_并發(fā)服務(wù)器的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!