- 论坛徽章:
- 0
|
GNU C Library 里的一句话理解不了,请高手指教
when i test theses functions in the code below, it report an error like :
Transport endpoint is not connected
who can tell me why ? and how to correct it? thanks!
- #define PATH "/tmp/DC/data"
- #ifdef HAVE_CONFIG_H
- #include <config.h>;
- #endif
- #include <stdio.h>;
- #include <stdlib.h>;
- #include <stddef.h>;
- #include <errno.h>;
- #include <stdlib.h>;
- #include <sys/socket.h>;
- #include <sys/un.h>;
- void write_to_server (int filedes)
- {
- int nbytes;
- char *message = "Hey Guy!";
- nbytes = write (filedes,message , 2);
- if (nbytes < 0)
- {
- perror ("write:::::");
- unlink(PATH);
- exit (EXIT_FAILURE);
- }
- }
- int make_named_socket (const char *filename)
- {
- struct sockaddr_un name;
- int sock;
- size_t size;
- /* Create the socket. */
- sock = socket (PF_UNIX, SOCK_DGRAM, 0);
- if (sock < 0)
- {
- perror ("socket");
- unlink(PATH);
- exit (EXIT_FAILURE);
- }
- /* Bind a name to the socket. */
- // unlink(PATH);
- name.sun_family = AF_FILE;
- strcpy (name.sun_path, filename);
- /* The size of the address is
- the offset of the start of the filename,
- plus its length,
- plus one for the terminating null byte. */
- size = (offsetof (struct sockaddr_un, sun_path)
- + strlen (name.sun_path) + 1);
- if (bind (sock, (struct sockaddr *) &name, size) < 0)
- {
- perror ("bind");
- unlink(PATH);
- exit (EXIT_FAILURE);
- }
- return sock;
- }
- int main(int argc, char *argv[])
- {
- int sock_fd;
- static char *path = PATH;
- sock_fd = make_named_socket(path);
- write_to_server (sock_fd);
- return EXIT_SUCCESS;
- }
复制代码 |
|