搭建tcp客户端,双进程实现tcp服务端客户端随时收发,udp服务端客户端

tcp客户端

#include
#include
#include
#include
#include
#include
#include 

#define ERR_MSG(msg) do{\
printf("line = %d\n", __LINE__);\
perror(msg); \
}while(0)

#define IP "192.168.0.144"   //宏定义服务端私网
#define POINT 8888           //宏定义服务端端口

char buf[32] = "";
int main(int argc, char const *argv[])
{
    //创建流式套接字
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    if(-1 == sockfd){                  //如果创建失败
        ERR_MSG("socket");             //输出错误
        return -1;
    }
    printf("Client created socket success\n");   //创建套接字成功输出提示
    //绑定客户端的IP和端口号
    
    
    //连接服务器
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;              //必须填AF_INET
    addr.sin_port = htons(POINT);           //服务器绑定的端口号
    addr.sin_addr.s_addr = inet_addr(IP);   //服务器绑定的IP
    socklen_t addrlen = sizeof(addr);

    if(connect(sockfd,(struct sockaddr*)&addr,sizeof(addr)) < 0){
        ERR_MSG("send");             //输出错误
        return -1;
    }
    printf("connect server success\n");

    //发送数据给服务器
    while (1)  //循环接收、打印服务端发来的消息和发送客户端消息
        {
            
            //发送信息给服务端
            bzero(buf,sizeof(buf));   //清空字符串
            printf("请输入发送给服务端的信息:");
            fgets(buf,sizeof(buf),stdin);
            buf[strlen(buf)-1] = 0;
            
            ssize_t resend = send(sockfd,buf,sizeof(buf),0);  //发送buf中的信息给服务端
            if(-1 == resend){                  //如果发送失败
                ERR_MSG("send");             //输出错误
                return -1;
            }
            printf("client send success\n");   //发送成功输出提示

            bzero(buf,sizeof(buf));    //清空字符串,接收服务端发来的消息
            ssize_t crecv = recv(sockfd,buf,sizeof(buf),0);
            if(-1 == crecv){                  
                ERR_MSG("recv");              //接收失败输出错误
                return -1;
            }else if(0 == crecv){
                printf("服务端关闭\n");
                break;
            }
            printf("Client recv [%s: %d]: %s success\n",inet_ntoa(addr.sin_addr),ntohs(addr.sin_port),buf);
            //打印服务端ip、端口号和发来的消息

            
        }
        close(sockfd);   //客户端退出的话,关闭为这个客户端收发消息生成的文件描述符
    //关闭文件描述符
    return 0;
}

双进程tcp服务端

#include
#include
#include
#include
#include
#include
#include 

#define ERR_MSG(msg) do{\
printf("line = %d\n", __LINE__);\
perror(msg); \
}while(0)

#define IP "192.168.0.144"   //宏定义服务端私网
#define POINT 8888           //宏定义服务端端口

int main(int argc, char const *argv[])
{
    //创建套接字
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    if(-1 == sockfd){                  //如果创建失败
        ERR_MSG("socket");             //输出错误
        return -1;
    }
    printf("Server created socket success\n");   //创建套接字成功输出提示
    
    //允许端口快速被复用
    int reuse = 1;
    if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0){
        ERR_MSG("setsockopt");
        return -1;
    }

    //绑定服务器的ip和端口
    //结构体初始化
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(POINT);
    addr.sin_addr.s_addr = inet_addr(IP);
    socklen_t addrlen = sizeof(addr);

    int rb = bind(sockfd,(struct sockaddr*)&addr,addrlen);
    if(-1 == rb){                  //如果绑定失败
        ERR_MSG("bind");              //输出错误
        return -1;
    }
    printf("bind server success\n");  //套接字与服务端ip和端口绑定成功输出提示

    //将套接字设置为被动监听状态
    if(-1 == listen(sockfd,6)){
        ERR_MSG("listen");              //监听失败输出错误
        return -1;
    }
    printf("listing...\n");         //套接字设置成监听状态成功后提示

    

    //接收客户端的信息
    char buf[32] = "";      //接收客户端发来消息存储的字符串
    while (1)
    {
        //获取连接成功后的套接字
        struct sockaddr_in actaddr;      //定义接收客户端ip和端口号的结构体
        socklen_t actaddrlen = sizeof(actaddr);
        int actsockfd = accept(sockfd,(struct sockaddr*)&actaddr,&actaddrlen);
        if(-1 == actsockfd){                  //如果连接失败
            ERR_MSG("accept");             //输出错误
            return -1;
        }
        printf("%d server accept success [%s:%d]\n",actsockfd,inet_ntoa(actaddr.sin_addr),ntohs(actaddr.sin_port));
        //打印新生成的文件描述符,该文件描述符用来通信  打印客户端ip和端口
        pid_t pid = fork();
        if(0 == pid){
            while(1){
                bzero(buf,sizeof(buf));    //情况字符串,接收客户端发来的消息
            ssize_t srecv = recv(actsockfd,buf,sizeof(buf),0);
            if(-1 == srecv){                  
                ERR_MSG("recv");              //接收失败输出错误
                return -1;
            }else if(0 == srecv){
                printf("客户端关闭\n");
                break;
            }
            printf("Server recv [%s: %d]: %s success\n",inet_ntoa(actaddr.sin_addr),ntohs(actaddr.sin_port),buf);
            //打印客户端ip、端口号和发来的消息
            }
        }else if(pid > 0){
            while(1){
            //发送信息给客户端
            bzero(buf,sizeof(buf));   //清空字符串
            //strcpy(buf,"Ser reply");  //输入要发给客户端的信息
            printf("请输入发给客户端的信息:");
            fgets(buf,sizeof(buf),stdin);
            buf[strlen(buf)-1] = 0;
            ssize_t resend = send(actsockfd,buf,sizeof(buf),0);  //发送buf中的信息给客户端
            if(-1 == resend){                  //如果发送失败
                ERR_MSG("send");             //输出错误
                return -1;
            }
            printf("Server send success\n");   //发送成功输出提示
            }
        }else if(pid < 0){
            ERR_MSG("fork");
            return -1;
        }

    
    //关闭套接字
    close(sockfd);
    printf("Server exit\n");
    return 0;
}
}

双进程tcp客户端

#include
#include
#include
#include
#include
#include
#include 

#define ERR_MSG(msg) do{\
printf("line = %d\n", __LINE__);\
perror(msg); \
}while(0)

#define IP "192.168.0.144"   //宏定义服务端私网
#define POINT 8888           //宏定义服务端端口

char buf[32] = "";
int main(int argc, char const *argv[])
{
    //创建流式套接字
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    if(-1 == sockfd){                  //如果创建失败
        ERR_MSG("socket");             //输出错误
        return -1;
    }
    printf("Client created socket success\n");   //创建套接字成功输出提示
    //绑定客户端的IP和端口号
    // int rb = bind(sockfd,(struct sockaddr*)&addr,addrlen);
    // if(-1 == rb){                  //如果绑定失败
    //     ERR_MSG("bind");              //输出错误
    //     return -1;
    // }
    // printf("bind client success\n");  //套接字与客户端ip和端口绑定成功输出提示
    
    //连接服务器
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;              //必须填AF_INET
    addr.sin_port = htons(POINT);           //服务器绑定的端口号
    addr.sin_addr.s_addr = inet_addr(IP);   //服务器绑定的IP
    socklen_t addrlen = sizeof(addr);

    if(connect(sockfd,(struct sockaddr*)&addr,sizeof(addr)) < 0){
        ERR_MSG("connect");             //输出错误
        return -1;
    }
    printf("connect server success\n");


    pid_t pid = fork();
    if(pid > 0){
        while (1)
        {
            //发送信息给服务端
            bzero(buf,sizeof(buf));   //清空字符串
            printf("请输入发送给服务端的信息:");
            fgets(buf,sizeof(buf),stdin);
            buf[strlen(buf)-1] = 0;
            
            ssize_t resend = send(sockfd,buf,sizeof(buf),0);  //发送buf中的信息给服务端
            if(-1 == resend){                  //如果发送失败
                ERR_MSG("send");             //输出错误
                return -1;
            }
            printf("client send success\n");   //发送成功输出提示
        }
    }else if(0 == pid){
            while (1)
            {
                bzero(buf,sizeof(buf));    //清空字符串,接收服务端发来的消息
            ssize_t crecv = recv(sockfd,buf,sizeof(buf),0);
            if(-1 == crecv){                  
                ERR_MSG("recv");              //接收失败输出错误
                return -1;
            }else if(0 == crecv){
                printf("服务端关闭\n");
                break;
            }
            printf("Client recv [%s: %d]: %s success\n",inet_ntoa(addr.sin_addr),ntohs(addr.sin_port),buf);
            //打印服务端ip、端口号和发来的消息
            }
            
        }else if(0 > pid){
            ERR_MSG("fork");
            return -1;
        }
        
    
    
        close(sockfd);   //服务端退出的话,关闭为这个客户端收发消息生成的文件描述符
    //接收服务器返回值
    //关闭文件描述符
    return 0;
}

双进程tcp通信效果展示

搭建tcp客户端,双进程实现tcp服务端客户端随时收发,udp服务端客户端_第1张图片

 udp服务端

#include
#include
#include
#include
#include
#include
#include 

#define ERR_MSG(msg) do{\
printf("line = %d\n", __LINE__);\
perror(msg); \
}while(0)

#define IP "192.168.0.144"   //宏定义服务端私网
#define POINT 8888           //宏定义服务端端口

int main(int argc, char const *argv[])
{
    //创建报式套接字
    int sockfd = socket(AF_INET,SOCK_DGRAM,0);
    if(-1 == sockfd){                  //如果创建失败
        ERR_MSG("socket");             //输出错误
        return -1;
    }
    printf("Server created socket success\n");   //创建套接字成功输出提示
    
    

    //绑定服务器的ip和端口
    //结构体初始化
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(POINT);
    addr.sin_addr.s_addr = inet_addr(IP);
    socklen_t addrlen = sizeof(addr);

    int rb = bind(sockfd,(struct sockaddr*)&addr,addrlen);
    if(-1 == rb){                  //如果绑定失败
        ERR_MSG("bind");              //输出错误
        return -1;
    }
    printf("bind server success\n");  //套接字与服务端ip和端口绑定成功输出提示

   
    //接收客户端的信息
    char buf[32] = "";      //接收客户端发来消息存储的字符串
    ssize_t res = 0;

    struct sockaddr_in actaddr;      //定义接收客户端ip和端口号的结构体
    socklen_t actaddrlen = sizeof(actaddr);
    
    while (1)
    {
        
            bzero(buf,sizeof(buf));    //情况字符串,接收客户端发来的消息
            ssize_t srecv = recvfrom(sockfd,buf,sizeof(buf),0,(struct sockaddr*)&actaddr,&addrlen);
            if(-1 == srecv){                  
                ERR_MSG("recv");              //接收失败输出错误
                return -1;
            }else if(0 == srecv){
                printf("客户端关闭\n");
                break;
            }
            printf("Server recv [%s: %d]: %s success\n",inet_ntoa(actaddr.sin_addr),ntohs(actaddr.sin_port),buf);
            //打印客户端ip、端口号和发来的消息

            //发送信息给客户端
            bzero(buf,sizeof(buf));   //清空字符串
            strcpy(buf,"Ser reply");  //输入要发给客户端的信息
            ssize_t resend = sendto(sockfd,buf,sizeof(buf),0,(struct sockaddr*)&actaddr,addrlen);  //发送buf中的信息给客户端
            if(-1 == resend){                  //如果发送失败
                ERR_MSG("send");             //输出错误
                return -1;
            }
            printf("Server sendto success\n");   //发送成功输出提示
        
    }

    //关闭套接字
    close(sockfd);
    printf("Server exit\n");
    return 0;
}

udp客户端

#include
#include
#include
#include
#include
#include
#include 

#define ERR_MSG(msg) do{\
printf("line = %d\n", __LINE__);\
perror(msg); \
}while(0)

#define IP "192.168.0.144"   //宏定义服务端私网
#define POINT 8888           //宏定义服务端端口

int main(int argc, char const *argv[])
{
    //创建报式套接字
    int sockfd = socket(AF_INET,SOCK_DGRAM,0);
    if(-1 == sockfd){                  //如果创建失败
        ERR_MSG("socket");             //输出错误
        return -1;
    }
    printf("Server created socket success\n");   //创建套接字成功输出提示

    //绑定服务器的ip和端口
    //结构体初始化
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(POINT);
    addr.sin_addr.s_addr = inet_addr(IP);
    socklen_t addrlen = sizeof(addr);

    
    //接收客户端的信息
    char buf[32] = "";      //接收客户端发来消息存储的字符串
    
    while (1)
    {
        //发送信息给服务端
            bzero(buf,sizeof(buf));   //清空字符串
            printf("请输入发送给服务端的信息:");
            fgets(buf,sizeof(buf),stdin);
            buf[strlen(buf)-1] = 0; //将从终端收到的消息\n改为\0
            ssize_t resend = sendto(sockfd,buf,sizeof(buf),0,(struct sockaddr*)&addr,addrlen);  //发送buf中的信息给服务端
            if(-1 == resend){                  //如果发送失败
                ERR_MSG("send");             //输出错误
                return -1;
            }
            printf("Client sendto success\n");   //发送成功输出提示

        
            bzero(buf,sizeof(buf));    //清空字符串,接收客户端发来的消息
            ssize_t crecv = recvfrom(sockfd,buf,sizeof(buf),0,(struct sockaddr*)&addr,&addrlen);
            if(-1 == crecv){                  
                ERR_MSG("recvfrom");              //接收失败输出错误
                return -1;
            }else if(0 == crecv){
                printf("服务端关闭\n");
                break;
            }
            printf("Client recv [%s: %d]: %s success\n",inet_ntoa(addr.sin_addr),ntohs(addr.sin_port),buf);
            //打印服务端ip、端口号和发来的消息

    }

    //关闭套接字
    close(sockfd);
    printf("Client exit\n");
    return 0;
}

udp通信效果展示

搭建tcp客户端,双进程实现tcp服务端客户端随时收发,udp服务端客户端_第2张图片

你可能感兴趣的:(tcp/ip,服务器,网络)