accept(ListenSocket, NULL, NULL); cause program to hang?

This is a discussion on accept(ListenSocket, NULL, NULL); cause program to hang? within the Networking/Device Communication forums, part of the General Programming Boards category; hi.... Accroding to MSDN: http://msdn.microsoft.com/library/de...nd_servers.asp the accept() method can be used such as: accept(ListenSocket, NULL, NULL); how ever I tried: ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    39

    accept(ListenSocket, NULL, NULL); cause program to hang?

    hi....

    Accroding to MSDN:

    http://msdn.microsoft.com/library/de...nd_servers.asp

    the accept() method can be used such as:

    accept(ListenSocket, NULL, NULL);

    how ever I tried:

    Code:
    ClientSocket = accept(ListenSocket, NULL, NULL);
    if (ClientSocket == INVALID_SOCKET) {
        printf("accept failed: %d\n", WSAGetLastError());
        closesocket(ListenSocket);
        WSACleanup();
        return 1;
    }
    my program hang/jam at:

    ClientSocket = accept(ListenSocket, NULL, NULL);

    so therefore accept(ListenSocket, NULL, NULL); cannot be used in such a way?

    Thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Post a whole program, we can't tell what else you might have screwed up before then.

    Like did you call WSAStartup() ?
    Did you call listen() before accept() ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    accept() is a blocking function, meaning that it will not finish until it accept()s a connection or an error occurs. That's why your program seems to be jammed.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    39
    Quote Originally Posted by Salem
    Post a whole program, we can't tell what else you might have screwed up before then.

    Like did you call WSAStartup() ?
    Did you call listen() before accept() ?
    http://msdn.microsoft.com/library/de...erver_code.asp

    Quote Originally Posted by Desolation
    accept() is a blocking function, meaning that it will not finish until it accept()s a connection or an error occurs. That's why your program seems to be jammed.
    thanks.... but is there a way to break after it accept?


    Thanks

  5. #5
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by draggy
    thanks.... but is there a way to break after it accept?
    What exactly does that mean...?
    Program execution will remain inside accept() until either the socket has an error, or until a client connects.

    Otherwise, you could look into either use of select() or nonblocking sockets. Both take a bit more skill however.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    39
    Quote Originally Posted by Cactus_Hugger
    What exactly does that mean...?
    Program execution will remain inside accept() until either the socket has an error, or until a client connects.

    Otherwise, you could look into either use of select() or nonblocking sockets. Both take a bit more skill however.

    Thanks

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    39
    ermm ......

    one more thing can we send and recive without bind and connect?

    Thanks

  8. #8
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    The function will fail with a WSAENOTCONN (not connected) error.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    one more thing can we send and recive without bind and connect?
    If you are using UDP, then you can send and receive without calling connect(). You still need to call bind() though.

  10. #10
    Registered User
    Join Date
    Jul 2005
    Posts
    39
    Quote Originally Posted by bithub
    If you are using UDP, then you can send and receive without calling connect(). You still need to call bind() though.
    Do we still need accept()?

    Thanks

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    nope

  12. #12
    Registered User
    Join Date
    Jul 2005
    Posts
    39
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Debugging my AVL tree program.
    By Nextstopearth in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 01:48 AM
  2. . . . . . . - . . . - -
    By The Brain in forum C++ Programming
    Replies: 17
    Last Post: 05-17-2005, 04:01 AM
  3. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 11:25 AM
  4. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 03:16 PM

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