Linux编程问题,有关本地域套接字和EPOLL的
发布网友
发布时间:2022-05-01 15:10
我来回答
共1个回答
热心网友
时间:2023-10-21 00:09
我写的一个可运行的本地域socket的例子,监听端的服务地址为绝对路径。例如/tmp/ssss.socket
void *lfs_dispatcher_thread_fn (void *arg)
{
struct sockaddr_in clientaddr;
int fdmax;
int newfd;
char buf[1024];
int nbytes;
int addrlen;
int ret;
int epfd = -1;
int res = -1;
struct epoll_event ev;
int index = 0;
int listen_fd, client_fd = -1;
struct sockaddr_un srv_addr;
listen_fd = socket (AF_UNIX, SOCK_STREAM, 0);
if (listen_fd < 0)
{
perror ("cannot create listening socket");
}
else
{
srv_addr.sun_family = AF_UNIX;
strncpy (srv_addr.sun_path, UNIX_DOMAIN,
sizeof (srv_addr.sun_path) - 1);
unlink (UNIX_DOMAIN);
ret =
bind (listen_fd, (struct sockaddr *) &srv_addr,
sizeof (srv_addr));
if (ret == -1)
{
lfs_printf ("cannot bind server socket");
lfs_printf ("srv_addr:%p", &srv_addr);
close (listen_fd);
unlink (UNIX_DOMAIN);
exit (1);
}
}
ret = listen (listen_fd, 1);
if (ret == -1)
{
perror ("cannot listen the client connect request");
close (listen_fd);
unlink (UNIX_DOMAIN);
exit (1);
}
chmod (UNIX_DOMAIN, 00777);//设置通信文件权限
fdmax = listen_fd;/* so far, it's this one */
events = calloc (MAX_CON, sizeof (struct epoll_event));
if ((epfd = epoll_create (MAX_CON)) == -1)
{
perror ("epoll_create");
exit (1);
}
ev.events = EPOLLIN;
ev.data.fd = fdmax;
if (epoll_ctl (epfd, EPOLL_CTL_ADD, fdmax, &ev) < 0)
{
perror ("epoll_ctl");
exit (1);
}
//time(&start);
for (;;)
{
res = epoll_wait (epfd, events, MAX_CON, -1);
client_fd = events[index].data.fd;
for (index = 0; index < MAX_CON; index++)
{
if (client_fd == listen_fd)
{
addrlen = sizeof (clientaddr);
if ((newfd =
accept (listen_fd,
(struct sockaddr *) &clientaddr,
(socklen_t *) & addrlen)) == -1)
{
perror ("Server-accept() error lol!");
}
else
{
// lfs_printf("Server-accept() is OK...\n");
ev.events = EPOLLIN;
ev.data.fd = newfd;
if (epoll_ctl
(epfd, EPOLL_CTL_ADD, newfd, &ev) < 0)
{
perror ("epoll_ctl");
exit (1);
}
}
break;
}
else
{
if (events[index].events & EPOLLHUP)
{
// lfs_printf ("find event");
if (epoll_ctl
(epfd, EPOLL_CTL_DEL, client_fd, &ev) < 0)
{
perror ("epoll_ctl");
}
close (client_fd);
break;
}
if (events[index].events & EPOLLIN)
{
/* going to recv data
*/
if ((nbytes =
recv (client_fd, buf, 1024, 0)) <= 0)
{
if (nbytes == 0)
{
}
else
{
lfs_printf ("recv() error lol! %d",
client_fd);
perror ("");
}
if (epoll_ctl
(epfd, EPOLL_CTL_DEL, client_fd,
&ev) < 0)
{
perror ("epoll_ctl");
}
close (client_fd);
}
else
{
// lfs_printf ("nbytes=%d,recv %s,%c", nbytes,
// buf, buf[6]);
process_request (buf, client_fd);
memset (buf, 0, 4);
}
break;
}
}
}
}
return 0;
}
热心网友
时间:2023-10-21 00:09
我写的一个可运行的本地域socket的例子,监听端的服务地址为绝对路径。例如/tmp/ssss.socket
void *lfs_dispatcher_thread_fn (void *arg)
{
struct sockaddr_in clientaddr;
int fdmax;
int newfd;
char buf[1024];
int nbytes;
int addrlen;
int ret;
int epfd = -1;
int res = -1;
struct epoll_event ev;
int index = 0;
int listen_fd, client_fd = -1;
struct sockaddr_un srv_addr;
listen_fd = socket (AF_UNIX, SOCK_STREAM, 0);
if (listen_fd < 0)
{
perror ("cannot create listening socket");
}
else
{
srv_addr.sun_family = AF_UNIX;
strncpy (srv_addr.sun_path, UNIX_DOMAIN,
sizeof (srv_addr.sun_path) - 1);
unlink (UNIX_DOMAIN);
ret =
bind (listen_fd, (struct sockaddr *) &srv_addr,
sizeof (srv_addr));
if (ret == -1)
{
lfs_printf ("cannot bind server socket");
lfs_printf ("srv_addr:%p", &srv_addr);
close (listen_fd);
unlink (UNIX_DOMAIN);
exit (1);
}
}
ret = listen (listen_fd, 1);
if (ret == -1)
{
perror ("cannot listen the client connect request");
close (listen_fd);
unlink (UNIX_DOMAIN);
exit (1);
}
chmod (UNIX_DOMAIN, 00777);//设置通信文件权限
fdmax = listen_fd;/* so far, it's this one */
events = calloc (MAX_CON, sizeof (struct epoll_event));
if ((epfd = epoll_create (MAX_CON)) == -1)
{
perror ("epoll_create");
exit (1);
}
ev.events = EPOLLIN;
ev.data.fd = fdmax;
if (epoll_ctl (epfd, EPOLL_CTL_ADD, fdmax, &ev) < 0)
{
perror ("epoll_ctl");
exit (1);
}
//time(&start);
for (;;)
{
res = epoll_wait (epfd, events, MAX_CON, -1);
client_fd = events[index].data.fd;
for (index = 0; index < MAX_CON; index++)
{
if (client_fd == listen_fd)
{
addrlen = sizeof (clientaddr);
if ((newfd =
accept (listen_fd,
(struct sockaddr *) &clientaddr,
(socklen_t *) & addrlen)) == -1)
{
perror ("Server-accept() error lol!");
}
else
{
// lfs_printf("Server-accept() is OK...\n");
ev.events = EPOLLIN;
ev.data.fd = newfd;
if (epoll_ctl
(epfd, EPOLL_CTL_ADD, newfd, &ev) < 0)
{
perror ("epoll_ctl");
exit (1);
}
}
break;
}
else
{
if (events[index].events & EPOLLHUP)
{
// lfs_printf ("find event");
if (epoll_ctl
(epfd, EPOLL_CTL_DEL, client_fd, &ev) < 0)
{
perror ("epoll_ctl");
}
close (client_fd);
break;
}
if (events[index].events & EPOLLIN)
{
/* going to recv data
*/
if ((nbytes =
recv (client_fd, buf, 1024, 0)) <= 0)
{
if (nbytes == 0)
{
}
else
{
lfs_printf ("recv() error lol! %d",
client_fd);
perror ("");
}
if (epoll_ctl
(epfd, EPOLL_CTL_DEL, client_fd,
&ev) < 0)
{
perror ("epoll_ctl");
}
close (client_fd);
}
else
{
// lfs_printf ("nbytes=%d,recv %s,%c", nbytes,
// buf, buf[6]);
process_request (buf, client_fd);
memset (buf, 0, 4);
}
break;
}
}
}
}
return 0;
}