1. 程式人生 > >socket程式設計(2)—— 一對多通訊

socket程式設計(2)—— 一對多通訊

1 一對多模型,TCP的程式設計步驟

服務端: 1、socket()獲得一個sockfd。注意第二個引數必須SOCK_STREAM. 2、準備通訊地址(必須伺服器的) 3、bind()繫結。(開放了埠,允許客戶端連線) 4、監聽客戶端 listen()函式 5、等待客戶端的連線 accept(),返回用於互動的socket描述符 6、使用第5步返回sockt描述符,進行讀寫通訊。 7、關閉sockfd。

客戶端: 客戶端的程式碼與一對一的一樣。注意第二個引數必須SOCK_STREAM.

TCP一對多模型,有兩類描述符: 第一步的描述符不再參與資訊互動,只是等待客戶端的連線(accept),accept()在客戶端連線上來後,會返回一個新的描述符,用於讀寫通訊。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>

int main(){
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    if (sockfd == -1){
        perror("socket"),exit(-1);
    }

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(2222);
    addr.sin_addr.s_addr = inet_addr("192.168.66.11");
    int res = bind(sockfd,(struct sockaddr*)&addr,
            sizeof(addr));
    if (res == -1){
        perror("bind"),exit(-1);
    }
    printf("bind ok\n");

    listen(sockfd,100);//監聽

    struct sockaddr_in client;
    socklen_t len = sizeof(client);
    int fd = accept(sockfd,(struct sockaddr*)&client,&len);
    char *from = inet_ntoa(client.sin_addr);//十六進位制轉點分十進位制
    printf("%s連線成功\n",from);
    char buf[100] = {};
    res = read(fd,buf,sizeof(buf));
    printf("接受了%d位元組,內容:%s",res,buf);
    write(fd,"welcome",7);
    close(fd);
    close(sockfd);
    return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>

int main(){
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    if (sockfd == -1){
        perror("socket"),exit(-1);
    }

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(2222);//連線埠
    addr.sin_addr.s_addr = inet_addr("192.168.66.11");//都是伺服器的,改成連線IP
    int res = connect(sockfd,(struct sockaddr*)&addr,
            sizeof(addr));
    if (res == -1){
        perror("connect"),exit(-1);
    }
    printf("連線成功\n");
    write(sockfd,"hello",5);
    char buf[100] = {};
    res = read(sockfd,buf,sizeof(buf));
    printf("讀到了%d位元組,內容:%s\n",res,buf);
    close(sockfd);
    return 0;
}

上面的服務端還只是實現了一對一的通訊,一對多通訊加一個while迴圈即可。

2 練習

1、在客戶端加上輸入功能,允許傳送不同的資訊,並且客戶端和伺服器端程式碼要支援多次輸入和輸出(讀寫上加迴圈),輸入bye退出。客戶端傳送的內容改為輸入,伺服器發回給客戶端的內容改成客戶端的輸入。

2、可以考慮在伺服器端啟動多程序fork(),支援多個客戶端的並行。

server.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<signal.h>

int sockfd;
void fa(int signo){
    printf("伺服器正在關閉\n");
    sleep(1);
    close(sockfd);
    exit(0);
}
int main(){
    printf("ctrl+c退出伺服器\n");
    signal(SIGINT,fa);
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    if (sockfd == -1){
        perror("socket"),exit(-1);
    }

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(2222);
    addr.sin_addr.s_addr = inet_addr("192.168.66.11");

    int reuseaddr = 1;//解決地址已被佔用問題
    setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,
            &reuseaddr,sizeof(reuseaddr));

    int res = bind(sockfd,(struct sockaddr*)&addr,
            sizeof(addr));
    if (res == -1){
        perror("bind"),exit(-1);
    }
    printf("bind ok\n");
    listen(sockfd,100);//監聽
    while (1){
        struct sockaddr_in client;
        socklen_t len = sizeof(client);
        int fd = accept(sockfd,(struct sockaddr*)&client,
                &len);//阻塞函式
        char *from = inet_ntoa(client.sin_addr);
        printf("%s連線成功\n",from);
        pid_t pid = fork();
        if (pid == 0){
        char buf[100] = {};
        while (1){
            res = read(fd,buf,sizeof(buf));
            printf("接受了%d位元組,內容:%s\n",res,buf);
            if (res <= 0){//包括0和-1
                break;
            }
            if (strcmp(buf,"bye") == 0){
                break;
            }
            write(fd,buf,strlen(buf));
            memset(buf,0,sizeof(buf));
        }
        close(fd);
        exit(0);
        }
    close(fd);
    }
}
client.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>

int main(){
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    if (sockfd == -1){
        perror("socket"),exit(-1);
    }

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(2222);//連線埠
    addr.sin_addr.s_addr = inet_addr("192.168.66.11");//都是伺服器的,改成連線IP
    int res = connect(sockfd,(struct sockaddr*)&addr,sizeof(addr));
    if (res == -1){
        perror("connect"),exit(-1);
    }
    printf("連線成功\n");

    char buf[100] = {};
    while (1){
        memset(buf,0,sizeof(buf));//buf清0
        printf("請輸入要說的話\n");
        scanf("%s",buf);
        write(sockfd,buf,strlen(buf));
        if (strcmp(buf,"bye") == 0){//退出的合適位置
            break;
        }
        memset(buf,0,sizeof(buf));//buf清0
        res = read(sockfd,buf,sizeof(buf));
        printf("讀到了%d位元組,內容:%s\n",res,buf);
    }

    close(sockfd);
    return 0;
}