Hi All ,
I'm trying to use nonblock socket with epoll.
I wonder why I sometimes get stuck in socket function when I use VCL .
Code as following:
printf ("before socket\n");
if ((c_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("create socket fail.\n");
exit(0);
}
printf ("after socket\n");
The only output is "before socket",socket function even dosen't return.
Thank you very much
complete code is in attachments
#include <cstdlib>
#include <string>
#include <string.h>
#include <iostream>
#include <unistd.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/select.h>
#include <error.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
using namespace std;
int ep = -1;
void* epoll_loop(void* arg) {
ep = epoll_create(1024);
struct epoll_event eventArr[1000];
int status, err;
socklen_t len;
len = sizeof (err);
while (1){
int n = epoll_wait(ep, eventArr, 20, -1);
printf ("%d\n", n);
for (int i = 0; i < n; i++) {
epoll_event ev = eventArr[i];
int events = ev.events;
int c_fd = ev.data.fd;
printf ("fd:%d\n", c_fd);
if (events & EPOLLERR) {
printf ("EPOLLERR\n");
status = getsockopt(c_fd, SOL_SOCKET, SO_ERROR, &err, &len);
cout << status << "," << err << endl;
}
if (events & EPOLLIN) {
printf ("EPOLLIN\n");
status = getsockopt(c_fd, SOL_SOCKET, SO_ERROR, &err, &len);
cout << status << "," << err << endl;
}
if (events & EPOLLOUT) {
printf ("EPOLLOUT\n");
}
}
}
}
void* test_epoll (void* arg) {
string ip = "192.168.216.1";
int port = 5201;
int c_fd, flags, ret;
struct sockaddr_in s_addr;
memset(&s_addr, 0, sizeof (s_addr));
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(port);
s_addr.sin_addr.s_addr = inet_addr(ip.c_str());
printf ("before socket\n");
if ((c_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("create socket fail.\n");
exit(0);
}
printf ("after socket\n");
flags = fcntl(c_fd, F_GETFL, 0);
if (flags < 0) {
perror("get socket flags fail.\n");
exit (-1);
}
if (fcntl(c_fd, F_SETFL, flags | O_NONBLOCK) < 0) {
perror("set socket O_NONBLOCK fail.\n");
exit (-1);
}
struct epoll_event event;
event.events = EPOLLIN | EPOLLOUT | EPOLLET;
event.data.fd = c_fd;
epoll_ctl(ep, EPOLL_CTL_ADD, c_fd, &event);
ret = connect(c_fd, (struct sockaddr*) &s_addr, sizeof (struct sockaddr));
if (ret < 0) {
if (errno == EINPROGRESS) {
return 0;
} else {
perror("connect remote server fail.\n");
printf("%d\n", errno);
exit(0);
}
}
return 0;
}
int main(int argc, char** argv) {
pthread_t tid1, tid2, tid3;
int rc1 = 0, rc2 = 0, rc3 = 0;
rc1 = pthread_create(&tid1, NULL, epoll_loop, NULL);
if(rc1 != 0)
printf("%s: %s\n",__func__, strerror(rc1));
sleep (1);
rc2 = pthread_create(&tid2, NULL, test_epoll, NULL);
if(rc2 != 0)
printf("%s: %s\n",__func__, strerror(rc2));
sleep (1);
rc3 = pthread_create(&tid3, NULL, test_epoll, NULL);
if(rc3 != 0)
printf("%s: %s\n",__func__, strerror(rc3));
while (1)
{
sleep(1);
}
return 0;
}
-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group.
View/Reply Online (#21282): https://lists.fd.io/g/vpp-dev/message/21282
Mute This Topic: https://lists.fd.io/mt/90680910/21656
Mute #socket-api:https://lists.fd.io/g/vpp-dev/mutehashtag/socket-api
Group Owner: [email protected]
Unsubscribe: https://lists.fd.io/g/vpp-dev/unsub [[email protected]]
-=-=-=-=-=-=-=-=-=-=-=-