多进程处理tcp客户端demo

多进程处理tcp客户端demo

头文件

#ifndef __TCP_PROCESS_TEST_H__
#define __TCP_PROCESS_TEST_H__

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>         /* superset of previous */

#define SERV_PORT 5001
#define SERV_IP_ADDR "192.168.253.130"
#define BACKLOG 5

#define QUIT_STR "quit"

#endif

主程序

1,创建socket fd
2,链接服务器
3,读写数据
4,关闭套接字

//./client serv_ip serv_port

#include "net.h"

int main(int argc, char **argv){
    int fd = -1;

    int port = -1;
    struct sockaddr_in sin;

    if(argc != 3){
        printf("参数错误!\n");
        exit(1);
    }

1,创建socket fd

    /**********************************/
    /**1, 创建socket fd****************/
    /**********************************/

    if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
        perror("socket");
        exit(1);
    }

    port = atoi (argv[2]);
    if(port < 5000){
        printf("端口输入错误\n");
        exit(1);
    }

2,链接服务器


    /*********************************/
    /**2, 链接服务器******************/
    /*********************************/

    //2.1 填充struct sockaddr_in结构体
    bzero(&sin, sizeof(sin));

    sin.sin_family = AF_INET;
    sin.sin_port = htons(port);

    if(inet_pton(AF_INET, argv[1], (void *) &sin.sin_addr) != 1){
        perror("inet_pton");
        exit(1);
    }

    //2.2 链接服务器
    if(connect(fd, (struct sockaddr *) &sin, sizeof(sin)) < 0){
        perror("connet");
        exit(1);
    }

3,读写数据

    /***********************************/
    /**3, 读写数据**********************/
    /***********************************/

    char buf[BUFSIZ];
    int ret = -1;
    while(1){
        bzero(buf, BUFSIZ);
        if(fgets(buf, BUFSIZ -1, stdin) == NULL){
            continue;
        }

        do{
            ret = write(fd, buf, strlen(buf));
        }while(ret < 0 && EINTR == errno);

        if(!strncasecmp(buf, QUIT_STR, strlen(QUIT_STR))){
            printf("Client is exiting!\n");
            break;
        }
    }

    /**********************************/
    /**4, 关闭套接字*******************/
    /**********************************/
    close(fd);
    
}

头文件

#ifndef __TCPNET_H__

#define __TCPNET_H__

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */

#include <pthread.h>

#define SERV_PORT 5001
#define SERV_IPADDR "192.168.7.3"
#define QUIT_STR "quit"

        struct cli_info {
//                struct list_head list; /* 内核链表*/
                int cli_fd;
                struct sockaddr_in cin;
                int qq_num;
                char nick[32];
                //...

        };

#endif

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容