Need help with Linux sockets

This is a discussion on Need help with Linux sockets within the Linux Programming forums, part of the Platform Specific Boards category; I made a simple internet program for Linux and was playing with it when I realised that it takes up ...

  1. #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]

  2. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with sockets under linux
    By principii in forum Linux Programming
    Replies: 7
    Last Post: 10-20-2010, 03:31 AM
  2. HPUX sockets vs Linux?
    By cpjust in forum Linux Programming
    Replies: 4
    Last Post: 12-06-2007, 02:51 PM
  3. A table for errno values by linux sockets?
    By hardi in forum Networking/Device Communication
    Replies: 2
    Last Post: 12-20-2006, 02:10 PM
  4. Segfault with C sockets on Linux
    By arti_valekar in forum C Programming
    Replies: 3
    Last Post: 02-16-2005, 02:25 AM

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