socket

AF_UNIX:只能做本地通讯的套接字

bcd-lsp中的s类型

头文件:
    #include <sys/types.h>   
    #include <sys/socket.h>
    #include <sys/un.h>
原型:
    int socket(int domain, int type, int protocol);
参数:
    int domain:Linux支持的协议族(地址族);
        AF_UNIX, AF_LOCAL   Local communication              unix(7)
        AF_INET             IPv4 Internet protocols          ip(7)
        AF_INET6            IPv6 Internet protocols          ipv6(7);
    int type:套接字类型
        SOCK_STREAM:字节流式套接字----> TCP
        SOCK_DGRAM: 数据报式套接字----> UDP
        SOCK_RAW:原始套接字,传输协议需要在第三个参数指定;
    int protocol:传输协议。如果不需要填0;
    IPPROTO_TCP 和 IPPROTO_UDP;
返回值:
    成功,返回套接字的文件描述符;
    失败,返回-1,更新errno;  
例程:
    //UNIX
    int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
    if(sfd < 0)
    {
        perror("socket");
        return -1;
    }
    //UNIX
    int sfd = socket(AF_UNIX, SOCK_DGRAM, 0);
    if(sfd < 0)
    {
        perror("socket");
        return -1;
    }

access

判断文件是否存在以及文件是否具有某些权限

头文件:
    #include <unistd.h>
原型:
    int access(const char *pathname, int mode);
参数:
    char *pathname:指定要判断的文件路径+文件名;
    int mode:
        R_OK:判断文件是否有可读权限  
        W_OK:判断文件是否有可写权限
        X_OK:判断文件是否有可执行权限
            R_OK|W_OK|X_OK
        F_OK:判断文件是否存在;  //返回0代表存在,返回-1代表不存在;
返回值:
    成功,返回0;
    失败,返回-1;
例程:
    if(access("./unix", F_OK) == 0)
    {
        if(unlink("./unix") < 0)
        {
            perror("unlink");
            return -1;
        }
    }

unlink

删除文件

头文件:
    #include <unistd.h>
原型:
    int unlink(const char *pathname);
参数:
    char *pathname:指定要删除的文件;
返回值:
    成功,返回0;
    失败,返回-1,更新errno;
例程:
    if(access("./unix", F_OK) == 0)
    {
        if(unlink("./unix") < 0)
        {
            perror("unlink");
            return -1;
        }
    }

TCP本地通信

通过bcd-lsp中的s类型文件进行通信,每次启动进程后,创建一个s类型的套接字文件,进程结束后,若想再次通信,需删除原同名文件重新创建一个s类型的套接字文件

服务器

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/un.h>
#include <unistd.h>
#define ERR_LOG(msg) do{\
    perror(msg);\
    printf("%d %s %s\n", __LINE__, __func__, __FILE__);\
}while(0)
#define N 128
int main(int argc, const char *argv[])
{
    //1.创建套接字
    int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
    if(sfd < 0)
    {
        ERR_LOG("socket");
        return -1;
    }
    //判断文件是否存在
    //如果存在,则删除该文件
    if(access("./unix", F_OK) == 0)
    {
        if(unlink("./unix")<0)
        {
            ERR_LOG("unlink");
            return -1;
        }
    }
    //2.绑定服务器ip和端口
    struct sockaddr_un sun;
    sun.sun_family = AF_UNIX;
    strcpy(sun.sun_path, "./unix");
    if(bind(sfd, (struct sockaddr*)&sun, sizeof(sun)) <0)
    {
        ERR_LOG("bind");
        return -1;
    }
    //3.将套接字设置为被动监听状态
    if(listen(sfd, 5) < 0)
    {
        ERR_LOG("listen");
        return -1;
    }
    printf("监听套接字成功\n");
    //4.获取连接后的套接字
    struct sockaddr_un cun;
    socklen_t clen = sizeof(cun);
    int newfd = accept(sfd, (struct sockaddr*)&cun, &clen);
    if(newfd < 0)
    {
        ERR_LOG("accept");
        return -1;
    }
    printf("newfd = %d\n", newfd);
    //5.循环发送接收
    int res = 0;
    char buf[N] = "";
    while(1)
    {
        bzero(buf, N);
        res = recv(newfd, buf, N, 0);
        if(res < 0)
        {
            ERR_LOG("recv");
            return -1;
        }
        else if(0 == res)
        {
            printf("对方关闭\n");
            break;
        }
        printf("%s\n", buf);
        strcat(buf, "*_*");
        if((res=send(newfd, buf, N, 0)) < 0)
        {
            ERR_LOG("send");
            return -1;
        }
        printf("发送成功%d\n", res);
    }
    //6.关闭套接字
    close(newfd);
    close(sfd);
    return 0;
}

客户端

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/un.h>
#include <unistd.h>
#define ERR_LOG(msg) do{\
    perror(msg);\
    printf("%d %s %s\n", __LINE__, __func__, __FILE__);\
}while(0)
#define N 128
int main(int argc, const char *argv[])
{
    //1.创建套接字
    int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
    if(sfd < 0)
    {
        ERR_LOG("socket");
        return -1;
    }
    //2.bind非必须
    //3.连接服务器
    //填充服务器的地址信息
    struct sockaddr_un sun;
    sun.sun_family = AF_UNIX;
    strcpy(sun.sun_path, "./unix");
    if(connect(sfd, (struct sockaddr*)&sun, sizeof(sun)) <0)
    {
        ERR_LOG("connect");
        return -1;
    }
    //4.循环发送接收
    int res = 0;
    char buf[N] = "";
    while(1)
    {
        bzero(buf, N);
        printf("请输入>>>");
        fgets(buf, N, stdin);
        buf[strlen(buf)-1] = 0;
        if((res=send(sfd, buf, N, 0)) < 0)
        {
            ERR_LOG("send");
            return -1;
        }
        printf("发送成功%d\n", res);
        bzero(buf, N);
        res = recv(sfd, buf, N, 0);
        if(res < 0)
        {
            ERR_LOG("recv");
            return -1;
        }
        else if(0 == res)
        {
            printf("对方关闭\n");
            break;
        }
        printf("%s\n", buf);
    }
    //6.关闭套接字
    close(sfd);
    return 0;
}

UDP本地通信

服务器

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/un.h>
int main(int argc, const char *argv[])
{
    //1.创建报式套接字
    int sfd = socket(AF_UNIX, SOCK_DGRAM, 0);
    if(sfd < 0)
    {
        perror("socket");
        return -1;
    }
    //2.判断文件是否存在
    if(access("./unix", F_OK) == 0)
    {
        if(unlink("./unix") < 0)
        {
            perror("unlink");
            return -1;
        }
    }
    //3.绑定服务器ip和端口号
    struct sockaddr_un sun;
    sun.sun_family = AF_UNIX;
    strcpy(sun.sun_path , "./unix");
    if(bind(sfd, (struct sockaddr*)&sun, sizeof(sun)) < 0)
    {
        perror("bind");
        return -1;
    }
    char buf[128] = "";
    struct sockaddr_un cun;
    socklen_t addrlen = sizeof(cun);
    //4.收发
    while(1)
    {
        bzero(buf, sizeof(buf));
        if(recvfrom(sfd, buf, 128, 0, (struct sockaddr*)&cun, &addrlen) < 0)
        {
            perror("recvfrom");
            return -1;
        }
        printf("[%s]:%s\n",cun.sun_path, buf);
        strcat(buf, "*_*");
        if(sendto(sfd, buf, 128, 0, (struct sockaddr*)&cun, addrlen)<0)
        {
            perror("sendto");
            return -1;
        }
    }
    //5.关闭套接字
    close(sun);
    close(cun);
    return 0;
}

客户端

必须bind客户端自己的套接字文件,如果不绑定,服务端无法获得客户端的套接字文件的sun_path

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/un.h>
int main(int argc, const char *argv[])
{
    //1.创建报式套接字
    int sfd = socket(AF_UNIX, SOCK_DGRAM, 0);
    if(sfd < 0)
    {
        perror("socket");
        return -1;
    }
    //2.判断文件是否存在
    if(access("./unix1", F_OK) == 0)
    {
        unlink("./unix1");
    }
    //3.绑定(必须绑定)
    struct sockaddr_un cun;
    cun.sun_family = AF_UNIX;
    strcpy(cun.sun_path, "unix1");
    if(bind(sfd, (struct sockaddr*)&cun, sizeof(cun)) < 0)
    {
        perror("bind");
        return -1;
    }
    //4.填充服务器ip和端口号
    struct sockaddr_un sun;
    sun.sun_family = AF_UNIX;
    strcpy(sun.sun_path , "./unix");
    char buf[128] = "";
    socklen_t addrlen = sizeof(sun);
    //5.收发
    while(1)
    {
        bzero(buf, sizeof(buf));
        printf("请输入>>>");
        fgets(buf, 128, stdin);
        buf[strlen(buf)-1] = 0;
        if(sendto(sfd, buf, 128, 0, (struct sockaddr*)&sun, addrlen)<0)
        {
            perror("sendto");
            return -1;
        }
        if(recvfrom(sfd, buf, 128, 0, NULL, NULL) < 0)
        {
            perror("recvfrom");
            return -1;
        }
        printf("%s\n",buf);
    }
    //6.关闭套接字
    close(sun);
    return 0;
}
Last modification:2021 年 04 月 19 日 15 : 51 : 46