UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » Networking
  • » How to connect to a server socket present at /tmp/sockfd

#1 2012-11-27 06:32 PM

Kamlesh Mishra
Guest

How to connect to a server socket present at /tmp/sockfd

Hi
We have multiple server sockets created using AF_LOCAL and SOCK_SEQPACKET, and we are able to locate them physically in the system ( at /tmp/sockfd ).

But in the client code , I have the following

int main()
{
   int sockfd, numbytes; 
   char buf[300];
   struct hostent *he;

// connector's address information
   struct sockaddr_in their_addr;
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
   {
    perror("socket()");
    exit(1);
    }

    else
       printf("Client-The socket() sockfd is OK...\n");

 // host byte order

     their_addr.sin_family = AF_INET;

   // short, network byte order

      printf("Server-Using %s and port %d...\n", argv[1], PORT);

      their_addr.sin_port = htons(PORT);

      their_addr.sin_addr = (struct in_addr *)("/tmp/sockfd");

       // zero the rest of the struct

        memset(&(their_addr.sin_zero), '\0', 8);

 

if(connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1)

{

    perror("connect()");

    exit(1);

}
}

#2 2012-11-27 09:40 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: How to connect to a server socket present at /tmp/sockfd

Um, your problem is that you're trying to use an AF_INET (IPv4 Internet) socket to connect to an AF_UNIX/AF_LOCAL (Unix domain) socket...  Not to mention using SOCK_STREAM instead of SOCK_SEQPACKET as you claim it really is...  What you're trying to do simply makes no sense at all...  You can't cast a string literal to struct in_addr!  It's expecting a raw IPv4 address in sin_addr...

Presumably, what you want to be using is socket(AF_UNIX,SOCK_SEQPACKET,0) along with struct sockaddr_un rather than sockaddr_in...  That will have a sun_path field, which is where you want to fill in your "/tmp/sockfd" pathname...

Offline

  • Index
  • » Networking
  • » How to connect to a server socket present at /tmp/sockfd

Board footer

Powered by FluxBB