Thread: Server for an iterative client

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    35

    Server for an iterative client

    Hi everybody,

    When a socket is created, by default it is a blocking socket. Under blocking mode socket I/O operations, connect and accept operations all block until the operation in question is completed.
    I want to receive data periodically from a client. That is, the first time I receive data the socket is blocked in the accept function but the next time is not blocked anymore and I want that it waits for the next incoming data.

    Do you know what can I do?

    thank you

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You can set and unset O_NONBLOCK on a socket file descriptor with fcntl().

    The only possible purpose for using non-blocking mode is because you DO NOT want it to wait, so this:
    That is, the first time I receive data the socket is blocked in the accept function but the next time is not blocked anymore and I want that it waits for the next incoming data.
    is non-sensical.
    If you mean you have a number of clients and you want to make sure to check them all, either use select() or you can use a series of accept()/recieve()'s on a NONBLOCK socket in an infinite loop BUT MAKE SURE YOU PUT A SMALL TIME DELAY IN, otherwise that loop will max the CPU out infinitely.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    35
    Hi again,

    thank you for the answer

    But I NEED TO WAIT because I don't know how long does it take until the client have the next block of data, therefore I want the socket to wait at the accept function. And that works for the first data but for the rest it doesn't work. I have only one client. It send me packets periodically.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    I think you have the wrong idea about accept() and what you're really looking for is recv*(). accept() blocks until a tcp clients connects, once it is connected, there is no need to use accept() again except if you're obviously waiting for another client to connect. However recv() will block each time you wait for data from the connected client (if you didn't use O_NONBLOCK). For a given client, there is only one accept() and multiplex recv()/send().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. server client application - (i really need your help)
    By sarahnetworking in forum C Programming
    Replies: 3
    Last Post: 03-01-2008, 10:54 PM
  2. Socket Programming Problem!!!!
    By bobthebullet990 in forum Networking/Device Communication
    Replies: 2
    Last Post: 02-21-2008, 07:36 PM
  3. Client works on a LAN but don't send all the data
    By Niara in forum Networking/Device Communication
    Replies: 9
    Last Post: 01-04-2007, 04:44 PM
  4. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 08:04 PM
  5. Unicode vurses Non Unicode client server application with winsock2 query?
    By dp_76 in forum Networking/Device Communication
    Replies: 0
    Last Post: 05-16-2005, 07:26 AM