Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1204751
  • 博文数量: 252
  • 博客积分: 5421
  • 博客等级: 大校
  • 技术积分: 2418
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-17 12:59
文章分类

全部博文(252)

文章存档

2017年(3)

2016年(18)

2015年(31)

2014年(18)

2013年(7)

2012年(8)

2011年(12)

2010年(30)

2009年(32)

2008年(57)

2007年(36)

分类: C/C++

2010-11-09 23:06:34

服务端示例:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <strings.h>
#include <errno.h>


#define UNIX_DOMAIN_PATH "/tmp/UNIX.domain"
#define MAXBUFSIZE 1024
#define LISTENQ 10
#define LOG_FILE_PATH "/tmp/UNIX.log"


int main()
{
    int listenfd;
    int connfd;
    int len;
    int ret;
    pid_t childpid;

    struct sockaddr_un cliaddr;
    socklen_t cliaddr_len;
    struct sockaddr_un servaddr;
    listenfd = socket(AF_LOCAL, SOCK_STREAM, 0);
    if (listenfd < 0) {
        perror("cannot create listening socket");
        return -1;
    }
    unlink(UNIX_DOMAIN_PATH);
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sun_family = AF_LOCAL;
    strncpy(servaddr.sun_path, UNIX_DOMAIN_PATH, sizeof(servaddr.sun_path)-1);
    ret = bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
    if (ret == -1) {
        perror("cannot bind server socket");
        close(listenfd);
        unlink(UNIX_DOMAIN_PATH);
        return -1;
    }
    ret = listen(listenfd, LISTENQ);
    if (ret == -1) {
        perror("cannot listen the client connect request");
        close(listenfd);
        unlink(UNIX_DOMAIN_PATH);
        return 1;
    }
    while (1) {
        len = sizeof(cliaddr);
        connfd = accept(listenfd, (struct sockaddr*)&cliaddr, &len);
        if (connfd < 0) {
            if (errno == EINTR) {
                continue;
            } else {
                perror("cannot accept the client connect request");
                close(listenfd);
                unlink(UNIX_DOMAIN_PATH);
                return -1;
            }
        }
        if ((childpid = fork()) < 0) {
            perror("cannot create child process");
            close(listenfd);
            unlink(UNIX_DOMAIN_PATH);
            return -1;
        } else if (childpid == 0) {
            ret = server_accept(connfd);
            exit(ret);
        } else {
            close(connfd);
        }
    }
    close(listenfd);
    unlink(UNIX_DOMAIN_PATH);
    return 0;
}


int server_accept(int sockfd)
{
    ssize_t n;
    char buf[MAXBUFSIZE];
    time_t timestamp;
    FILE *fp;
    memset(buf, 0, sizeof(buf));
    fp = fopen(LOG_FILE_PATH, "a");
    if (fp == NULL) {
        perror("fopen log file");
        return -1;
    }
again:
    while ((n = recv(sockfd, buf, MAXBUFSIZE, 0)) > 0) {
        buf[n] = '\n';
        buf[n+1] = '\0';
        strcpy(buf+21, buf);
        timestamp = time(NULL);
        strftime(buf, 20, "%Y-%m-%d %H:%M:%S", localtime(&timestamp));
        buf[19] = ':';
        buf[20] = ' ';
        if (fputs(buf, fp)) {
            perror("fputs log file");
            return -1;
        }
    }
    if (n<0 && errno==EINTR) {
        goto again;
    } else if (n < 0) {
        perror("recv connfd msg");
        return -1;
    }
    close(sockfd);
    fclose(fp);
    return 0;
}




客户端示例:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <strings.h>
#include <errno.h>


#define UNIX_DOMAIN_PATH "/tmp/UNIX.domain"
#define MAXBUFSIZE 1024


int main()
{
    int connfd;
    int ret;
    char buf[MAXBUFSIZE];

    static struct sockaddr_un servaddr;
    connfd = socket(AF_LOCAL, SOCK_STREAM, 0);
    if (connfd < 0) {
        perror("cannot create connect socket");
        return -1;
    }
    servaddr.sun_family = AF_LOCAL;
    strncpy(servaddr.sun_path, UNIX_DOMAIN_PATH, sizeof(servaddr.sun_path)-1);
    ret = connect(connfd, (struct sockaddr*)&servaddr, sizeof(servaddr));
    if (ret == -1) {
        perror("cannot connect to the server");
        return -1;
    }
    memset(buf, 0, MAXBUFSIZE);
    strcpy(buf, "client");
    if (send(connfd, buf, strlen(buf), 0) == -1) {
        perror("cannot send msg to the server");
        return -1;
    }
    close(connfd);
    return 0;
}


阅读(1667) | 评论(1) | 转发(0) |
0

上一篇:关于招聘与求职

下一篇:CRC算法与实现

给主人留下些什么吧!~~

chinaunix网友2010-11-10 15:21:46

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com