I struggle to wrap my head around file descriptors and how I could read data from one, process it and then send to another.
As a server I need to be able to accept connections, receive data, process it and then pass it to another client.
I have been introduced to epolling yesterday and I want to know if my strategy is correct for creating a client-server network.
One epollfd is created. I specify it to be edge-triggered (EPOLLET) and non-blocking (set with:
flags |= 0_NONBLOCK and
fctnl(epollfd, F_SETFL, flags).
My intention is to now create an array of networkfds (client sockets) and listen for connection/messages.
- Get notified about new data
- Read data
- Process data
- Write some data to another socket.
All the examples I found in linux man and online only offer information about how to read data from sockets and I am afraid my design is dumb and due to failure if I try to have many clients practically communicating to many clients at the same time.
I decided to ask here because I read that NGINX (the web server is using epolling)
Can anyone help?
EDIT 1: I am intending to have (many) sockets in a list (struct epoll_event *events) and access them via epoll_wait().
// If I understand correctly: int ndfs = epoll_wait(epollfd, events, MAX_EVENTS, -1); // Puts some events in the <events> array and an int in ndfs
nfds should now contain the number of available fds in
events which one can iterate through with a for loop. That's what I understand from the manual.
Once I receive a message from one of them, I would like to be able to process it (i.e read it's contents and take a decision) and eventually trigger a write to another socket.
I do all of this to AVOID multithreading. Is this achievable?