socket本地套接字

socket本地套接字代码示例


下面是socket本地套接字使用的示例,代码中有详细注释,仅供参考

server.c

//server.c
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h> //struct sockaddr
//#include <netinet/in.h> //struct sockaddr_in
#include <sys/un.h>  //struct sockaddr_un
#include <netdb.h>
#include <time.h>
#include <strings.h>
#include <sys/un.h>
#include <sys/un.h>
#include <time.h>
#include <unistd.h> //socklen_t


int main()
{
    int  fd;
    int sock_id;
    printf("server before socket\n");
    sock_id = socket(AF_LOCAL, SOCK_STREAM, 0);//PF_LOCAL,PF_INET,PF_UNIX,AF_INET
    if(sock_id == -1)
    {
        perror("socket");
        printf("%d:socket error\n",__LINE__);
        return -1;
    }

    // 如果套接字文件存在, 删除套接字文件
    unlink("server.sock");

    struct sockaddr_un serv;
    serv.sun_family=AF_LOCAL;
    strcpy(serv.sun_path, "server.sock");//文件现在还不存在

    printf("server before bind\n");
    int ret = bind(sock_id, (struct sockaddr*)&serv, sizeof(serv));
    if(ret == -1)
    {
        perror("bind error");
        exit(1);
    }

    // 监听
    printf("server before listen\n");
    if(listen(sock_id,36) != 0)//非阻塞
    {
        printf("%d:error\n",__LINE__);
        return -1;
    }

     // 等待接收连接请求
    struct sockaddr_un client;
    socklen_t len = sizeof(client);
    printf("server before accept\n");

    //block wait client connet,自动拿到client socket file path
    fd = accept(sock_id,(struct sockaddr*)&client,&len);//阻塞
    if(fd == -1)
    {
        printf("%d:warn!\n",__LINE__);
        //break;
    }
    printf("======client bind file: %s\n", client.sun_path);//client.sock
    int i = 0;
    char ser_rec_buf[128] = {0};
    while(1)
    {
        usleep(1000*1000);
        
        time_t now;
        char* cp;
        time(&now);
        cp = ctime(&now);
        printf("cp:%s",cp);
        printf("strlen of cp:%d fd:%d\n",(int)strlen(cp),fd);
        send(fd,cp,strlen(cp),0);
        recv(fd,ser_rec_buf,sizeof(ser_rec_buf),0);
        printf("server process end,ser_rec_buf:%s\n",ser_rec_buf);
    }

    close(fd);
}

client.c

//client.c
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h> //struct sockaddr
//#include <netinet/in.h> //struct sockaddr_in
#include <sys/un.h>  //struct sockaddr_un
#include <netdb.h>
#include <time.h>
#include <strings.h>
#include <sys/un.h>
client.c#include <stdio.h>
#define BUF_LEN (128)

int main()
{
    int sock,fd;
    printf("client before socket\n");
    sock = socket(AF_LOCAL, SOCK_STREAM, 0);//非阻塞
    if(sock == -1)
    {
        printf("%d:error\n",__LINE__);
        return -1;
    }
    unlink("client.sock");


    // 给客户端绑定一个套接字文件,这一步可以省取码?
    struct sockaddr_un client;
    client.sun_family = AF_LOCAL;
    strcpy(client.sun_path, "client.sock");
    printf("client before bind\n");
    int ret = bind(sock, (struct sockaddr*)&client, sizeof(client));
    if(ret == -1)
    {
        perror("bind error");
        exit(1);
    }

    // 初始化server信息
    struct sockaddr_un serv;
    serv.sun_family = AF_LOCAL;

    //client 自己要指定server socket file path,指定错了connet报错
    strcpy(serv.sun_path, "server.sock");

    // 连接服务器,若server未开启监听,或者上一步server socet path指定错了这里都会退出
    printf("client before connect\n");
    ret = connect(sock,(struct sockaddr*)&serv,sizeof(serv)) ;
    if(ret !=0)
    {
        perror("connect");
        return -1;
    }
    printf("ret:%d, after client connect\n",ret);
    char cli_rec_buf[BUF_LEN]; //client reciving buffer
    char cli_send_buf[BUF_LEN];//client sending buffer

    int i = 0;
    int n = 0;
    while(1)
    {
        i++;
        usleep(1000*1000);
        sprintf(cli_send_buf,"client send the %dth time!",i);

        memset(cli_rec_buf,0,128);
        n=recv(sock,cli_rec_buf,BUF_LEN,0);
        send(sock,cli_send_buf,sizeof(cli_send_buf),0);
        printf("n=%d sock=%d\n",n,sock);
        printf("cli_rec_buf:%s\n",cli_rec_buf);
        printf(" client proccess end, i:%d\n",i);

    }
    
    close(fd);
}

也看参照:

https://blog.csdn.net/zhangzhi2ma/article/details/82414704

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值