#include <stddef.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
int
make_named_socket (const char *dosyaismi)
{
struct sockaddr_un isim;
int soket;
size_t boyut;
/* Soketi oluşturalım. */
soket = socket (PF_LOCAL, SOCK_DGRAM, 0);
if (soket < 0)
{
perror ("socket");
exit (EXIT_FAILURE);
}
/* Sokete bir isim verelim. */
isim.sun_family = AF_LOCAL;
strncpy (isim.sun_path, dosyaismi, sizeof (isim.sun_path));
isim.sun_path[sizeof (isim.sun_path) - 1] = '\0';
/* Adresin büyüklüğü,
dosya isminin başlangıç konumu
artı uzunluğu
artı bir boş karakterdir.
Bir seçenek olarak bunu kullanabilirsiniz:
boyut = SUN_LEN (&isim);
*/
boyut = (offsetof (struct sockaddr_un, sun_path)
+ strlen (isim.sun_path) + 1);
if (bind (soket, (struct sockaddr *) &isim, boyut) < 0)
{
perror ("bind");
exit (EXIT_FAILURE);
}
return soket;
}