C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-21-2002, 03:30 AM   #1
Registered User
 
Join Date: Jul 2002
Posts: 3
Question Need help with Linux sockets

I made a simple internet program for Linux and was playing with it when I realised that it takes up to 99% of
cpu resources. I believe there's something wrong with my main loop, but can't seem to fix it, even though I
gone to various measures to find the source of the problem. Tutorials on the subject suggests that my method
is already pretty efficient, though I can't be sure. Anyone has time, please look at the code attached and give
me comments please? Thanks!


Teng Junbin


void MainLoop(int sockfd) {
fd_set rea_set, err_set;
static struct timeval nulltime;

for (;;) {
// Check for incoming data and errors
FD_ZERO(&rea_set);
FD_ZERO(&err_set);
FD_SET(fileno(stdin), &rea_set);
FD_SET(sockfd, &rea_set);
FD_SET(sockfd, &err_set);

if (select(fileno(stdin) > sockfd ? fileno(stdin) + 1 : sockfd + 1, &rea_set, NULL, &err_set, &nulltime) < 0) {
puts("ERROR: Unable to pool incoming data and errors\n");
puts(CONNECTIONCLOSED);
close(sockfd);
exit(3);
}

// Check for error in connection
if (FD_ISSET(sockfd, &err_set)) {
// Disconnected
close(sockfd);

puts(CONNECTIONCLOSED);
break;
} else {
// Get server input
if (FD_ISSET(sockfd, &rea_set)) {
if (!ProcessData(sockfd)) {
// Disconnected
close(sockfd);

puts(CONNECTIONCLOSED);
break;
}
}

// Get keyboard input
if (FD_ISSET(fileno(stdin), &rea_set)) {
if (!ProcessData(fileno(stdin)) {
// IO Error
puts("ERROR: Unable to read from stdin\n\n");
close(sockfd);
exit(5);
}
}
}
}
}[quote]
junbin is offline   Reply With Quote
Old 07-21-2002, 12:42 PM   #2
Im back!
 
shaik786's Avatar
 
Join Date: Jun 2002
Location: Bangalore, India
Posts: 345
Are there more processes running when you get this 99%? And is this 99% constant throughout till this proram is alive? If no, it's just common, try running more processes simultanesouly and check to see if you still get this 99% for this program, if you do, there's some serious problem, while the code looks optimized.
shaik786 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
HPUX sockets vs Linux? cpjust Linux Programming 4 12-06-2007 02:51 PM
A table for errno values by linux sockets? hardi Networking/Device Communication 2 12-20-2006 02:10 PM
Segfault with C sockets on Linux arti_valekar C Programming 3 02-16-2005 02:25 AM
Problems with sockets under linux principii Linux Programming 5 09-18-2004 03:09 PM


All times are GMT -6. The time now is 12:56 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22