1. 程式人生 > >Linux(二十)編寫簡單的TCP伺服器(多程序版本)

Linux(二十)編寫簡單的TCP伺服器(多程序版本)

上一篇我們講的TCP伺服器的簡單實現是隻有一個程序連線的情況,但是實際上,這是不現實的,所以我們要改進我們的程式,使其可以服務於多個程序
直接上程式碼

server.c

#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
void processrequest(int client_sock,struct sockaddr* client_socket) { char buf[1024] = {0}; ssize_t s = read(client_sock,buf,sizeof(buf)); if(s < 0) { perror("read"); continue; } if(s == 0) { //說明對端關閉 printf("client %s say bye",inet_ntoa(client_socket->sin_addr)); close(client_sock); break
; } buf[s] = '\0'; printf("client:# %s",buf); printf("please wait...\n"); write(client_sock,buf,sizeof(buf)); } void createworker(int client_sock,struct sockaddr * client_socket) { printf("get a new connect\n"); pid_t pid = fork(); if(pid < 0) { printf("fork error\n"
); return; } else if(pid == 0) { if(fork() == 0) { processrequest(client_sock,&client_socket); } exit(0); } else { waitpid(pid,NULL,0); } } int main(int argc,char *argv[]) { if(argv != 3) { printf("usage is ./server IP PORT\n"); return 1; } int sock = socket(AF_INET,SOCK_STREAM,0); if(sock < 0) { printf("socket error\n"); close(sock); return 2; } struct sockaddr_in server_socket; bzero(&server_socket,sizeof(server_socket)); server_socket.sin_family = AF_INET; server_socket.sin_port = htons(atoi(argv[2])); server_socket.sin_addr.s_addr = inet_addr(argv[1]); if(bind(sock,(struct sockaddr*)&server_socket,sizeof(struct sockaddr_in) < 0) { printf("bind error\n"); close(sock); return 2; } if(listen(sock,_BACKLOG_) < 0) { printf("listen error\n"); close(sock); return 3; } printf("bind and listen is success,please wait accept...\n"); while(1) { struct sockaddr_in client_socket; socklen_t len = 0; int client_sock = accept(sock,(struct sockaddr*)&client_socket,&len); //int accept(int sockfd,struct sockaddr *addr,socklen_t *addrlen); //addrlen引數是一個傳入傳出引數,傳入的是呼叫者提供的,緩衝區addr的長度以避免緩衝區溢位問題, //傳出的是客戶端地址結構體的實際長度(有可能沒有佔滿呼叫者提供的緩衝區); if(client_sock < 0) { printf("accept error\n"); close(sock); return 4; } createworker(client_sock,&client_socket)//結構體client_socket傳參用指標 } }

client.c

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

int main(int argc,char *argv)
{
    if(argc != 3)
    {
        printf("usage is ./client IP PORT");
        return 1;
    }
    int sock = socket(AF_INET,SOCK_STREAM,0);
    if(sock < 0)
    {
        perror("socket");
        return 2;
    }
    struct sockaddr_in server_sock;
    bzero(&server_sock,sizeof(server_sock));
    server_sock.sin_family = AF_INET;
    server_sock.sin_port = htons(atoi(argv[2]));
    server_sock.sin_addr.s_addr = inet_addr(argv[1]);

    int ret = connect(sock,(struct sockaddr*)&server_sock,sizeof(server_sock));
    if(ret < 0)
    {
        perror("connect");
        return 3;
    }
    pintf("connect is success!\n");
    while(1)
    {
        char buf[1024] = {0};
        printf("client #:");
        fflush(0);
        read(0,buf,strlen(buf));
        buf[strlen(buf)-1] = '\0';
        write(sock,buf,strlen(buf));
        if(strncasecmp(buf,"quit",4) == 0);
        {
            printf("client quit\n");
            break;
        }
        printf("please wait...\n");
        read(sock,buf,sizeof(buf));
        printf("server $:%s\n");

    }
    close(sock);
    return 0;   
}

這裡寫圖片描述
這裡寫圖片描述