Thread: Datagram Unix socket problem

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Europe/Belgrade
    Posts
    78

    Datagram Unix socket problem

    Server receives some string from the client and sends the same string as response with

    Code:
    bytes_no = recvfrom(sock_fd, buf, 100, 0, (struct sockaddr*)&client_addr, &addr_len    );
    buf[bytes_no] = '\0';
    printf("\nReceived '%s' with %d bytes.", buf, bytes_no);
    bytes_no = sendto(sock_fd, buf, strlen(buf) + 1, 0, (struct sockaddr*)&client_addr, sizeof(struct sockaddr));
    printf("\n%d sent", bytes_no);
    while client sends some string and receives the response with

    Code:
    printf("\nRequest:  ");
    scanf("%s", message);
    sendto(sock_fd, message, strlen(message) + 1, 0, (struct sockaddr*)&sock_addr, sizeof(struct sockaddr));
    memset(message, 0, 100);
    bytes_no = recvfrom(sock_fd, message, 100, 0, (struct sockaddr*)&sock_addr, &addr_len);
    printf("Received '%s' with %d bytes.", message, bytes_no);
    Problem comes when server with sendto() sends a response to the client. If socket is created as Internet socket, then everything works fine: server sends the response, client receives it and prints out. But, if the socket is created as Unis socket, then server does not send the response with sendto(); instead sendto() returns status -1. Why? (The code for both Unix and Internet sockets is same except in the part where different socket types are created.)
    Regards!

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Did you check errno to see what error it says it's having when it returns -1?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    5
    Put this declaration at the top of your code:

    Code:
    int errno;
    And after, check it's value after any (or almost any) libc function:

    Code:
    strerror (errno)
    Regards !

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    What Ron means is actually:

    To use errno, make sure you #include <errno.h>

    And then you can do something like the following:
    Code:
    if(sendto(sock_fd, message, strlen(message) + 1, 0, (struct sockaddr*)&sock_addr, sizeof(struct sockaddr)) == -1)
      printf("CRAP! sendto() returned an error! Error is: %s\n", strerror(errno));
    There's no need to declare errno since it's declared in errno.h already.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Location
    Europe/Belgrade
    Posts
    78
    Error is "Unknown error 1073831896". Any ideas what does it mean?

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Looks like you might be using winsock, in which case you need to use WSAGetLastError instead of errno.

    We don't just ask for the operating system for no reason
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Jan 2006
    Location
    Europe/Belgrade
    Posts
    78
    I'm using Linux. (No problem when using Internet sockets, only Unix)

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Let me see your error checking code. Are you only printing errno when an error occurs?
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User
    Join Date
    Jan 2006
    Location
    Europe/Belgrade
    Posts
    78
    Code:
    if (bytes_no == -1)
        printf("\n%d, %s", errno, strerror(err));
    prints out
    22, Unknown error 1073831896.

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    printf("\n%d, %s", errno, strerror(err));
    You're supposed to pass errno like I did, not err.

    At any rate, I have this in /usr/include/asm/errno.h:
    Code:
    #define EINVAL          22      /* Invalid argument */
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket Programming Problem!!!!
    By bobthebullet990 in forum Networking/Device Communication
    Replies: 2
    Last Post: 02-21-2008, 07:36 PM
  2. Socket Datagram info sendto(), recvfrom()
    By siluro in forum C Programming
    Replies: 1
    Last Post: 02-20-2005, 12:10 PM
  3. problem closing socket
    By Wisefool in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-29-2003, 12:19 PM
  4. problem closing a socket
    By Wisefool in forum C Programming
    Replies: 1
    Last Post: 10-28-2003, 01:38 PM
  5. Socket programming problem.
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-14-2002, 08:44 PM