UNIX网络编程--1个server,多个client

一、概述

1、需求

使用TCP连接,1个server,多个client。server和多个client之间,可以互相发送消息。

 

二、分析

1、使用fork实现

2、基础知识参考TCP套接字编程

 

三、代码

1、服务端server.cpp

//
#include <sys/types.h>  
#include <sys/socket.h>  
#include <stdio.h>  
#include <netinet/in.h>  
#include <arpa/inet.h>  
#include <unistd.h>  
#include <string.h>  
#include <stdlib.h>  
#include <fcntl.h>  
#include <sys/shm.h>  
  
#define PORT  8890  
#define QUEUE_SIZE   10  
#define BUFFER_SIZE 1024  
  
//传进来的sockfd,就是互相建立好连接之后的socket文件描述符  
//通过这个sockfd,可以完成 [服务端]<--->[客户端] 互相收发数据  
void str_echo(int sockfd)  
{  
    char buffer[BUFFER_SIZE];  
    pid_t pid = getpid();  
    while(1)  
    {  
        memset(buffer,0,sizeof(buffer));  
        int len = recv(sockfd, buffer, sizeof(buffer),0);  
        if(strcmp(buffer,"exit\n")==0)  
        {  
            printf("child process: %d exited.\n",pid);  
            break;  
        }  
        printf("pid:%d receive:\n",pid);  
        fputs(buffer, stdout);  
        send(sockfd, buffer, len, 0);  
    }  
    close(sockfd);  
}  
  
int main(int argc, char **argv)  
{  
    //定义IPV4的TCP连接的套接字描述符  
    int server_sockfd = socket(AF_INET,SOCK_STREAM, 0);  
  
    //定义sockaddr_in  
    struct sockaddr_in server_sockaddr;  
    server_sockaddr.sin_family = AF_INET;  
    server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);  
    server_sockaddr.sin_port = htons(PORT);  
  
    //bind成功返回0,出错返回-1  
    if(bind(server_sockfd,(struct sockaddr *)&server_sockaddr,sizeof(server_sockaddr))==-1)  
    {  
        perror("bind");  
        exit(1);//1为异常退出  
    }  
    printf("bind success.\n");  
  
    //listen成功返回0,出错返回-1,允许同时帧听的连接数为QUEUE_SIZE  
    if(listen(server_sockfd,QUEUE_SIZE) == -1)  
    {  
        perror("listen");  
        exit(1);  
    }  
    printf("listen success.\n");  
  
    for(;;)  
    {  
        struct sockaddr_in client_addr;  
        socklen_t length = sizeof(client_addr);  
        //进程阻塞在accept上,成功返回非负描述字,出错返回-1  
        int conn = accept(server_sockfd, (struct sockaddr*)&client_addr,&length);  
        if(conn<0)  
        {  
            perror("connect");  
            exit(1);  
        }  
        printf("new client accepted.\n");  
  
        pid_t childid;  
        if(childid=fork()==0)//子进程  
        {  
            printf("child process: %d created.\n", getpid());  
            close(server_sockfd);//在子进程中关闭监听  
            str_echo(conn);//处理监听的连接  
            exit(0);  
        }  
    }  
  
    printf("closed.\n");  
    close(server_sockfd);  
    return 0;  
}  
//

2、客户端client.cpp

//
#include <sys/types.h>  
#include <sys/socket.h>  
#include <stdio.h>  
#include <netinet/in.h>  
#include <arpa/inet.h>  
#include <unistd.h>  
#include <string.h>  
#include <stdlib.h>  
#include <fcntl.h>  
#include <sys/shm.h>  
  
#define PORT  8890  
#define BUFFER_SIZE 1024  
  
int main(int argc, char **argv)  
{  
    if(argc!=2)  
    {  
        printf("usage: client IP \n");  
        exit(0);  
    }  
  
    //定义IPV4的TCP连接的套接字描述符  
    int sock_cli = socket(AF_INET,SOCK_STREAM, 0);  
  
    //定义sockaddr_in  
    struct sockaddr_in servaddr;  
    memset(&servaddr, 0, sizeof(servaddr));  
    servaddr.sin_family = AF_INET;  
    servaddr.sin_addr.s_addr = inet_addr(argv[1]);  
    servaddr.sin_port = htons(PORT);  //服务器端口  
  
    //连接服务器,成功返回0,错误返回-1  
    if (connect(sock_cli, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)  
    {  
        perror("connect");  
        exit(1);  
    }  
    printf("connect server(IP:%s).\n",argv[1]);  
  
    char sendbuf[BUFFER_SIZE];  
    char recvbuf[BUFFER_SIZE];  
    //客户端将控制台输入的信息发送给服务器端,服务器原样返回信息  
    while (fgets(sendbuf, sizeof(sendbuf), stdin) != NULL)  
    {  
        send(sock_cli, sendbuf, strlen(sendbuf),0); ///发送  
        if(strcmp(sendbuf,"exit\n")==0)  
        {  
            printf("client exited.\n");  
            break;  
        }  
        printf("client receive:\n");  
        recv(sock_cli, recvbuf, sizeof(recvbuf),0); ///接收  
        fputs(recvbuf, stdout);  
  
        memset(sendbuf, 0, sizeof(sendbuf));  
        memset(recvbuf, 0, sizeof(recvbuf));  
    }  
  
    close(sock_cli);  
    return 0;  
}  
//

3、编译

//
g++ -o server_fork sever_fork.cpp  
g++ -o client  client.cpp
//

4、测试

(1)server

(2)client

 

致谢

1、TCP多连接,1个server,多个client

2、TCP套接字编程

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读