Back again

Well, it’s been quite some time since I’ve posted on here. Haven’t had the time I guess. I recently found another great position at Time4Learning.com. I’m doing PHP development for them here in South Florida. I’m really enjoying working with them. All of the guys are really sharp and encourage each other to get better instead of being comfortable with your current skillset. It’s really been lighting a fire for me to continue my own endeavors in learning new skills/technologies. For example, I’ve been working on yet another implementation of a project that I’ve tried a handful of times. The project is a complete IRC client in the browser. I know this has been done and whatnot, but I always wanted to do a better version than what’s available. My current implementation is using libfcgi in C to handle web page generation as well as the networking layer linking the client side to the IRC server. The client slide will be implemented using EmberJS. Anywho, I’ve just finished learning how to use epoll to handle event driven networking. It’s a good feeling to be constantly learning. I’ve posted the code below even though I took almost no time making things clean. The idea was to quickly figure out epoll which I think should be pretty easily understood by my simple program. It just binds and listens on port 3333 waiting for connections to read and dumping the contents. I’ll definitely be using epoll to handle my network connections in the fastcgi process for my AjaxIRC app. Check it out:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/epoll.h>

int main(int argc, char **argv) {
	int epoll_fd, listen_fd, reuse = 1, ret, i, new_fd;
	struct sockaddr_in theiraddr;
	socklen_t addrsize;
	struct addrinfo hints;
	struct addrinfo *servinfo, *cur;
	struct epoll_event ev, *events;
	char read_buf[1024];
	int read_bytes;
	if((epoll_fd = epoll_create(100))==-1) {
		perror("epoll");
		exit(1);
	}
	printf("Got epoll fd: %d\n",epoll_fd);
	memset(&hints, '\0', sizeof(hints));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_flags = AI_PASSIVE;
	if((getaddrinfo(NULL,"3333",&hints, &servinfo))!=0) {
		perror("getaddrinfo");
		exit(1);
	}
	for(cur = servinfo; cur != NULL; cur = cur->ai_next) {
		listen_fd = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
		if(listen_fd == -1) {
			perror("socket");
			continue;
		}
		setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
		if((bind(listen_fd, cur->ai_addr, cur->ai_addrlen))==-1) {
			close(listen_fd);
			continue;
		}
		break;
	}
	if(cur == NULL) {
		fprintf(stderr,"Failed to bind\n");
		exit(1);
	}
	freeaddrinfo(servinfo);
	if(listen(listen_fd, 10) == -1) {
		perror("listen");
		exit(1);
	}
	ev.data.fd = listen_fd;
	ev.events = EPOLLIN;

	printf("Listening...\n");
	printf("Adding to epoll..\n");
	ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, listen_fd, &ev);
	printf("Return status of epoll_ctl: %d\n",ret);
	printf("Allocating memory for events.");
	events = (struct epoll_event *)malloc(sizeof(struct epoll_event)*100);
	if(!events) {
		perror("malloc");
		exit(1);
	}
	while((ret = epoll_wait(epoll_fd, events, 100, 1000)) >= 0) {
		if(ret == 0) {
			printf("no events this cycle.\n");
		}
		else {
                        for(i=0;i<ret;i++) {
				if(events[i].data.fd == listen_fd) {
					new_fd = accept(listen_fd, (struct sockaddr *)&theiraddr, &addrsize);
					if(new_fd != -1) {
						printf("Accepted connection adding to epoll...\n");
						ev.data.fd = new_fd;
						ev.events = EPOLLIN;
						epoll_ctl(epoll_fd, EPOLL_CTL_ADD, new_fd, &ev);
					}
				}
				else {
					new_fd = events[i].data.fd;
					memset(&read_buf,'\0',1024);
					read_bytes = recv(new_fd, &read_buf, 1024, 0);
					if(read_bytes==0) {
						printf("Closing connection on socket: %d\n",new_fd);
						close(new_fd);
						continue;
					}
					printf("Read (%d): %s\n",read_bytes, read_buf);
				}
			}
		}
	}
	perror("epoll_wait");
	return 0;
}

  1. No comments yet.

  1. No trackbacks yet.

 
Ubiety v0.0.1
Chat ()