epoll problem


 
Thread Tools Search this Thread
Top Forums Programming epoll problem
# 1  
Old 03-16-2005
epoll problem

I programmed simple tcp server using nonblocking sockets and epoll.
But I am facing some problems.

1. I can recv from events[i].data.fd but I can't send any data over
events[i].data.fd

2. When I make multiple connections at once (say 100) I get segmentation
fault...

What am I doing wrong?

(you can find source code that making multiple connections under topic
"multi-thread server, pthreads, sleep". it is test.c)

source code is here

-------------- epollserver.c ------------------
$ cc epollserver -o epollserver
--------------------------------------------------
Code:
#include <pthread.h>
#include <stdio.h>
#include <sys/timeb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>

#define MAX_CLIENT 101
#define PORT 3355
#define DEBUG

int listenfd;

void nonblock(int sockfd)
{
	int opts;
	opts = fcntl(sockfd, F_GETFL);
	if(opts < 0)
	{
		perror("fcntl(F_GETFL)\n");
		exit(1);
	}
	opts = (opts | O_NONBLOCK);
	if(fcntl(sockfd, F_SETFL, opts) < 0) 
	{
		perror("fcntl(F_SETFL)\n");
		exit(1);
	}
}


int main()
{
	int epfd;
	struct epoll_event *events;
	struct epoll_event ev;
	
	struct sockaddr_in srv;
	int clifd;
	int i;
	int n;
	int res;
	char buffer[1024];
	
	if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
	{
		perror("sockfd\n");
		exit(1);
	}

	bzero(&srv, sizeof(srv));
	srv.sin_family = AF_INET;
	srv.sin_addr.s_addr = INADDR_ANY;
	srv.sin_port = htons(PORT);

	if( bind(listenfd, (struct sockaddr *) &srv, sizeof(srv)) < 0)
	{
		perror("bind\n");
		exit(1);
	}
	
	listen(listenfd, 1024);

	epfd = epoll_create(MAX_CLIENT);
	if(!epfd)
	{
		perror("epoll_create\n");
		exit(1);
	}
	ev.events = EPOLLIN | EPOLLERR | EPOLLHUP;
	ev.data.fd = listenfd;
	if(epoll_ctl(epfd, EPOLL_CTL_ADD, listenfd, &ev) < 0)
	{
		perror("epoll_ctl, adding listenfd\n");
		exit(1);
	}

	for( ; ; )
	{
		res = epoll_wait(epfd, events, MAX_CLIENT, 0);
		for(i = 0; i < res; i++)
		{
			if(events[i].data.fd == listenfd)
			{
				clifd = accept(listenfd, NULL, NULL);
				if(clifd > 0)
				{
					nonblock(clifd);
					ev.events = EPOLLIN | EPOLLET;
					ev.data.fd = clifd;
					if(epoll_ctl(epfd, EPOLL_CTL_ADD, clifd, &ev) < 0)
					{
						perror("epoll_ctl ADD\n");
						exit(1);
					}
				}
			}
			else {
				n = recv(events[i].data.fd, buffer, 1023, 0);
				if(n == 0)
				{
#ifdef DEBUG
					printf("%d closed connection\n", events[i].data.fd);
					epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, NULL);
#endif
				}
				else if(n < 0)
				{
#ifdef DEBUG
					printf("%d error occured, errno: %d\n",
							events[i].data.fd, errno);
#endif
				}
				else {
#ifdef DEBUG
					printf("%d data received: %s\n", 
							events[i].data.fd, buffer);
					bzero(&buffer, strlen(buffer));
#endif
					send(events[i].data.fd, buffer, strlen(buffer), 0);
				}
			}
		}
	}
	
	return 0;
}


Last edited by Parahat Melayev; 03-16-2005 at 02:05 PM..
# 2  
Old 03-17-2005
solved.

1. I didn't allocate enough memory for events as;

events = calloc(MAX_CLIENT, sizeof(struct epoll_event));


2. I did bzero on buffer before send Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

sed Or Grep Problem OR Terminal Problem?

I don't know if you guys get this problem sometimes at Terminal but I had been having this problem since yesterday :( Maybe I overdid the Terminal. Even the codes that used to work doesn't work anymore. Here is what 's happening: * I wanted to remove lines containing digits so I used this... (25 Replies)
Discussion started by: Nexeu
25 Replies

2. Programming

Event driven programming / epoll / typedef union / session data array

Sorry for the “word salad” subject, but I wanted to cast a wide net for help. I've created an IP (Internet Protocol) server which serves HTTP, SMTP, and FTP requests. As you probably know, they all require creating a socket, listening on it, accepting connections, and then having a short... (3 Replies)
Discussion started by: John S.
3 Replies

3. IP Networking

Problem with forwarding emails (SPF problem)

Hi, This is rather a question from a "user" than from a sys admin, but I think this forum is apropriate for the question. I have an adress with automatic email forwarding and for some senders (two hietherto), emails are bouncing. This has really created a lot of problems those two time so I... (0 Replies)
Discussion started by: carwe
0 Replies

4. Programming

epoll problem with tcp connect()

I am using epoll to manage my network connections. At the client side, the idea is to send the message upon detecting the completion of connect(). I've set up the socket to be nonblocking, then after calling of connect(), I add the socket into the epoll and wait on the event to be detected from... (0 Replies)
Discussion started by: tuesday420
0 Replies

5. AIX

AIX OS problem? network problem?

Dear ALL. I installed AIX OS on customer sites. but Only one site is too slow when I connected telnet, ftp.. Ping is too fast. but telnet and FTP is not connected.. of course i check the configuration file on aix but it's normal. Do any Idea?? thanks in advance. - Jun - (3 Replies)
Discussion started by: Jeon Jun Seok
3 Replies

6. UNIX for Dummies Questions & Answers

DHCP problem and eth1 problem

At work I am trying to get this one Linux machine (let's call it ctesgm07) to behave like another Linux machine that we have (let's call it test007). test007 returns the following version info: cat /etc/debian_version: lenny/sid uname -a: Linux test007 2.6.27-7-generic #1 SMP Tue Nov 4... (0 Replies)
Discussion started by: sllinux
0 Replies

7. AIX

user login problem & Files listing problem.

1) when user login to the server the session got colosed. How will resolve? 2) While firing the command ls -l we are not able to see the any files in the director. but over all view the file system using the command df -g it is showing 91% used. what will be the problem? Thanks in advance. (1 Reply)
Discussion started by: pernasivam
1 Replies

8. UNIX for Advanced & Expert Users

epoll, sockets and threads

Hello Forum, There is a transient bug in my code which I just can't catch. Could you help please? The basic idea is this. I have a multithreaded server and two thread pools, the IO pool and Worker pool. The main server thread listens to incoming connections. When it gets one, it dispatches... (0 Replies)
Discussion started by: rij
0 Replies

9. Programming

epoll detecting client connections

I've got a program running using epoll to poll activity on a listening socket. I want to be able to output a message to the terminal when the socket is connected to from the client app. It appears that epoll doesn't throw any events on the connect but only on the socket activity. Is there any... (1 Reply)
Discussion started by: mstaylor
1 Replies

10. Shell Programming and Scripting

problem with dd command or maybe AFS problem

Hi, folks. Sorry for bothering, but maybe someone could help me please. The problem is the following: there is some script that copies files from local file system to AFS. The copying is performed with dd command. The script copies data into some AFS volumes. The problem appeared with one... (0 Replies)
Discussion started by: Anta
0 Replies
Login or Register to Ask a Question