#include
#include
#include
#include
#include
#include
#include
#include
#define GSMD_UNIX_SOCKET_TYPE SOCK_STREAM
#define GSMD_UNIX_SOCKET "\0gsmd"
服务器端:
int usock_init(struct gsmd *g)
{
struct sockaddr_un sun;
int fd, rc;
__ucmd_ctx = talloc_named_const(gsmd_tallocs, 1, "ucmd");
__gu_ctx = talloc_named_const(gsmd_tallocs, 1, "gsmd_user");
fd = socket(PF_UNIX, GSMD_UNIX_SOCKET_TYPE, 0);
if (fd < 0)
return fd;
memset(&sun, 0, sizeof(sun));
sun.sun_family = AF_UNIX;
memcpy(sun.sun_path, GSMD_UNIX_SOCKET, sizeof(GSMD_UNIX_SOCKET));
rc = bind(fd, (struct sockaddr *)&sun, sizeof(sun));
if (rc < 0) {
close(fd);
return rc;
}
rc = listen(fd, 10);
if (rc < 0) {
close(fd);
return rc;
}
g->gfd_sock.fd = fd;
g->gfd_sock.when = GSMD_FD_READ | GSMD_FD_EXCEPT;
g->gfd_sock.data = g;
g->gfd_sock.cb = &gsmd_usock_cb;
return gsmd_register_fd(&g->gfd_sock);
}
客户端:
static int lgsm_open_backend(struct lgsm_handle *lh, const char *device)
{
int rc;
if (!strcmp(device, LGSMD_DEVICE_GSMD)) {
struct sockaddr_un sun;
/* use unix domain socket to gsm daemon */
lh->fd = socket(PF_UNIX, GSMD_UNIX_SOCKET_TYPE, 0);
if (lh->fd < 0)
return lh->fd;
memset(&sun, 0, sizeof(sun));
sun.sun_family = AF_UNIX;
memcpy(sun.sun_path, GSMD_UNIX_SOCKET, sizeof(GSMD_UNIX_SOCKET));
rc = connect(lh->fd, (struct sockaddr *)&sun, sizeof(sun));
if (rc < 0) {
close(lh->fd);
lh->fd = -1;
return rc;
}
} else
return -EINVAL;
return 0;
}
这里客户端通过GSMD_UNIX_SOCKET的路径和服务器端匹配,只要是这个路径,服务器端发出的 数据,连接好的客户端都能收到,即广播式的
阅读(2304) | 评论(0) | 转发(0) |