Non-blocking socket connection problem

This is a discussion on Non-blocking socket connection problem within the Linux Programming forums, part of the Platform Specific Boards category; MK27, now i end up in a different problem i have used the same code which i have attached in ...

  1. #16
    VIM addict
    Join Date
    May 2009
    Location
    Chennai, India
    Posts
    43
    MK27, now i end up in a different problem

    i have used the same code which i have attached in the previous post and declare two macro

    Code:
    #define SERVER_ADDR "10.0.3.92"
    #define CLIENT_ADDR "10.0.3.92"
    and instead of INADDR_ANY i have replced it with corresponding call to include the IP which i have defined as macro

    Code:
    /* Bind to socket */
    bzero(&serveraddr, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_addr.s_addr = inet_addr(SERVER_ADDR);
    serveraddr.sin_port = htons(PORT);
    
    ......
    
    clientaddr.sin_family = AF_INET;
    clientaddr.sin_addr.s_addr = inet_addr(CLIENT_ADDR);
    clientaddr.sin_port = htons(PORT);
    memset(&(clientaddr.sin_zero),0,8);
    Now if i try to execute the code, listen call gets success and when tried to connect my program hangs there and when i try to see the netstat -na results i am getting the following things

    Code:
    root@10:~# netstat -na
    Active Internet connections (servers and established)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State
    tcp        0      0 10.0.3.92:9090          0.0.0.0:*               LISTEN
    tcp        0      1 10.0.3.92:2240          10.0.3.92:9090          SYN_SENT
    ......
    But if i change both SERVER_ADDR and CLIENT_ADDR macro to "127.0.0.1" then the same code works fine. You have any idea about this?

  2. #17
    VIM addict
    Join Date
    May 2009
    Location
    Chennai, India
    Posts
    43
    Sorry MK27, actually i have iptables enabled which prevented this behavior. It is working fine with hard coded IP.

  3. #18
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by cbalu View Post
    But if i change both SERVER_ADDR and CLIENT_ADDR macro to "127.0.0.1" then the same code works fine. You have any idea about this?
    It should not work. The address is a big endian (network byte order) number, not a string, which is the purpose of inet_aton and inet_ntoa.

    Also: you have not posted your working code. You last post still had no fork in it, meaning it should halt at listen().

    Keep it mind that it is possible to have something appear to work correctly in some sense BY FLUKE, even if it is done improperly.

    The real test will be: Have you actually been able to send() and recv() any content?
    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

  4. #19
    VIM addict
    Join Date
    May 2009
    Location
    Chennai, India
    Posts
    43
    Ya i can able to send and receive contents without using fork() call. The code which i have attached has the complete logic which i have been using.

    So you mean to say that without fork() call my code is expected to fail?

  5. #20
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by cbalu View Post
    Ya i can able to send and receive contents without using fork() call. The code which i have attached has the complete logic which i have been using.

    So you mean to say that without fork() call my code is expected to fail?
    That's pretty interesting, maybe because of the NON_BLOCK. The client and server are all part of a single process? Post the code you have right now, I'm interested in trying it...ah, I see you do have inet_aton in there.

    I just wanted to indicate to you that that is *not* the normal way to go. But if you think it is a good idea, then by all means forge ahead. And do post the code.
    Last edited by MK27; 05-27-2009 at 08:59 AM.
    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

  6. #21
    VIM addict
    Join Date
    May 2009
    Location
    Chennai, India
    Posts
    43
    Here is the final code which i have with sockets as blocking and logic of getting the system IP dynamically without system() call and send & receive on sockets
    Attached Files Attached Files

  7. #22
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    If you want to get your assigned/existing ip manually, AFAIK the only way is to parse the data returned by "ifconfig" (which is a system, not a C, command). That is awkward, which implies to me that INADDR_ANY is the proper solution.
    INADDR_ANY tells the socket to bind to all interfaces. If you pass a specific IP, then it will bind to the specified interface. To enumerate through the interfaces, you can use:
    Code:
    ioctl(socket, SIOCGIFADDR, ifreq);
    You last post still had no fork in it, meaning it should halt at listen().
    Why would his code ever halt at listen()? That makes no sense.

    It should not work. The address is a big endian (network byte order) number, not a string, which is the purpose of inet_aton and inet_ntoa.
    His code should work. inet_addr does the same thing as inet_aton().

  8. #23
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Why would his code ever halt at listen()? That makes no sense.
    It is always nice to learn when you have been a victim of unquestioned doctrine, I guess. I was under the impression that the client and server had to be separate processes. I'm thanking cbalu for this right here and now. Life is probably easier if they are separate processes, but assuming they have to be led me to pile one assumption on top of the other. Now I think about it, I'm sure the reason I said that was to somehow explain the first assumption, because you're right, it doesn't make much sense to say it will halt at listen(). "Listen" just sets a characteristic on the socket so a connection can be made. Some pretty lousy advice I gave in this thread, where was everyone?

    SO: I am not always right
    Last edited by MK27; 05-27-2009 at 10:53 AM.
    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

  9. #24
    VIM addict
    Join Date
    May 2009
    Location
    Chennai, India
    Posts
    43
    Thanks bithub and MK27, actually i was also having the same assumption that server and client had to be in separate processes. To clarify my doubt i started with this code and found out that both server and client can be made as single process.

    But, can any of you explain me one thing, if i try to change the socket to non-blocking then the same code is not working. It is giving me the following error

    Connect call failed: Operation now in progress

    I am changing the sockets to non-blocking by adding the following code
    Code:
        /* make both send and receive non-blocking */
        if ( (flag = fcntl(listenfd, F_GETFL, NULL)) < 0)
        {
            perror("listenfd fcntl:");
            retc = FAIL;
            goto done;
        }
    
        flag |= O_NONBLOCK;
    
        if ( fcntl(listenfd, F_SETFL, flag) < 0)
        {
            perror("listenfd fcntl:");
            retc = FAIL;
            goto done;
        }
        if ( (flag = fcntl(sendfd, F_GETFL, NULL)) < 0)
        {
            perror("sendfd fcntl:");
            retc = FAIL;
            goto done;
        }
    
        flag |= O_NONBLOCK;
    
        if ( fcntl(sendfd, F_SETFL, flag) < 0)
        {
            perror("sendfd fcntl:");
            retc = FAIL;
            goto done;
        }
    -BalaC-
    Last edited by cbalu; 05-27-2009 at 11:26 PM. Reason: Corrected typo in code

  10. #25
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    I only read the first page, but it seems that no one actually pointed this out, even though it was obvious:

    Using a non-blocket socket, connect and all the functions that would normally block return immediately with operating now in progress. Your code is fine - you are just handling an error that isn't really an error.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  11. #26
    VIM addict
    Join Date
    May 2009
    Location
    Chennai, India
    Posts
    43
    Hi IceDane thanks for your input, actually i need to wait till connection has been established in case of non-blocking sockets using the select call.

    Now i have the code working with non-blocking socket also. Thank you all for your time

    -BalaC-

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with simple socket client/server program
    By spencer88 in forum C Programming
    Replies: 6
    Last Post: 05-05-2009, 11:05 PM
  2. Socket Connection Still Alive!!
    By maven in forum Networking/Device Communication
    Replies: 2
    Last Post: 07-21-2006, 02:14 PM
  3. socket problem
    By RevengerPT in forum C Programming
    Replies: 9
    Last Post: 01-17-2006, 08:07 AM
  4. Weird connection problem.
    By tilex in forum Networking/Device Communication
    Replies: 6
    Last Post: 02-06-2005, 09:11 AM
  5. problem closing socket
    By Wisefool in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-29-2003, 11:19 AM

Tags for this Thread


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