⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client1.c

📁 《Beginning linux programming》一书相应的源代码
💻 C
字号:
/*  Make the necessary includes and set up the variables.  */#include <sys/types.h>#include <sys/socket.h>#include <stdio.h>#include <sys/un.h>#include <unistd.h>int main(){    int sockfd;    int len;    struct sockaddr_un address;    int result;    char ch = 'A';/*  Create a socket for the client.  */    sockfd = socket(AF_UNIX, SOCK_STREAM, 0);/*  Name the socket, as agreed with the server.  */    address.sun_family = AF_UNIX;    strcpy(address.sun_path, "server_socket");    len = sizeof(address);/*  Now connect our socket to the server's socket.  */    result = connect(sockfd, (struct sockaddr *)&address, len);    if(result == -1) {        perror("oops: client1");        exit(1);    }/*  We can now read/write via sockfd.  */    write(sockfd, &ch, 1);    read(sockfd, &ch, 1);    printf("char from server = %c\n", ch);    close(sockfd);    exit(0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -