Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

problem with accept() in C

I am creating a simple chat program in C. In my server program I have a code that accepts connections using accept(). My problem is that accept() waits for an incomming connection and I cannot proceed to the other statements. Why is this and how can I fix it?
0
xofrats
Asked:
xofrats
1 Solution
 
markdocCommented:
This is because accept() blocks when there is no incomming connection and it waits for one before returning. The same goes for the recv() function. You can use the FD_ macros and the select() function to get around this problem.

select() gives you the power to monitor several sockets at the same time. It'll tell you which ones are ready for reading, which are ready for writing, and which sockets have raised exceptions.

#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

int main(void)
{
    struct timeval tv;
    fd_set readfds;
    sockaddr_in s_adr;
    int len = sizeof(s_adr);
    int newSock;

    /* ... Establish listener here ... */      

    tv.tv_sec = 0;
    tv.tv_usec = 0;

    FD_ZERO(&readfds);
    FD_SET(listeningSocket, &readfds);

    while (1)
    {
        select(listeningSocket + 1, &readfds, NULL, NULL, &tv);

        if (FD_ISSET(listeningSocket, &readfds))
            newSock = accept(listeningSocket, (sockaddr *)&s_adr, &len);              
            
        /* Other statements */                  
    }

    return 0;
}


For a more complete example, visit:

http://www.ecst.csuchico.edu/~beej/guide/net/html/advanced.html#blocking
0
 
sunnycoderCommented:
>My problem is that accept() waits for an incomming connection and I cannot proceed to the other statements. Why is this
As markdoc said, accept blocks for input connections and that is expected behavior.
>and how can I fix it?
you can have a non-blocking accept but that is not a good idea ... Select will also not be very helpful in this regard ... The best way here might be to go in for multithreading ... Let your main thread listen for connections and accept them. Upon accepting, it can spawn a thread and threads can process that connection send()ing and recv()ing in a separate control flow.

Somethig like

main()
{
    ....
    ....
    while (1)
    {
       sock=accept()
        ...
        create_thread(... sock, thread_func, ...)
        ...
     }
    ...
}

thread_func( ... sock ...)
{
      ...
      send()
       ...
      recv()
      ...
}

You can chose any thread library available on your platform.

Cheers!
sunnycoder
0
 
Nikhil GuptaCommented:
you can try multiple process also.
 try this code.
if(fork()!=0)
{
 //parent will wait for new connection.
 sock_fd=accept();

}
else
{
   //this is child process.
  //put your stuff here
}

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now