欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

多线程发送bit流,服务端

程序员文章站 2022-03-08 19:34:52
...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <pthread.h>

static int PORT = 8000;
const int LISTENQ = 1024;
static int bit = 10;
static char* addr_ip = NULL;

void *thread_run(void *arg);

int main(int argc,char**argv)
{
    for(int i=1;i<argc -1;i++)
    {
        if(strncmp(argv[i],"-p",2)==0){
            PORT = atoi(argv[i + 1]);
            if(PORT<=0){
                printf("PORT:%d error\n",PORT);
                return -1;
            }
            printf("PORT:%d\n",PORT);
        }
        if(strncmp(argv[i],"-b",2)==0){
            bit = atoi(argv[i+1]);
            if(bit<=0){
                printf("bit:%d error\n",bit);
                return -1;
            }
            printf("bit:%d\n",bit);
        }
        if(strncmp(argv[i],"-s",2)==0){
            addr_ip = (char*)malloc(sizeof(char)*128);
            memmove(addr_ip,argv[i+1],strlen(argv[i+1]) + 1);
            printf("%s\n",addr_ip);
            if(bit<=0){
                printf("bit:%d error\n",bit);
                return -1;
            }
            printf("bit:%d\n",bit);
        }
    }
    /*声明服务器地址和客户 协议族 */
    struct sockaddr_in servaddr,cliaddr;
	
    int listenfd,connfd;
	
    pid_t childpid;
    socklen_t clilen;
	
	
    /*(1) 初始化监听套接字listenfd*/
    listenfd = socket(AF_INET,SOCK_STREAM,0);
    if(listenfd<0)
    {
        perror("socket error");
        exit(1);
    }
    /*(2) 设置服务器协议族sockaddr_in结构*/
    bzero(&servaddr,sizeof(servaddr));

    servaddr.sin_family = AF_INET;
    if(addr_ip==NULL){
        servaddr.sin_addr.s_addr = htonl(INADDR_ANY);   //表明可接受任意IP地址
    }
    else{
        if(inet_pton(AF_INET , addr_ip , &servaddr.sin_addr) < 0){
		    printf("inet_pton error for %s\n",addr_ip);
		    exit(1);
	    }
    }
    servaddr.sin_port = htons(PORT);

    /*(3) 绑定套接字和端口*/
    if(bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
    {
        perror("bind error");
        exit(1);
    }

    /*(4) 监听客户请求*/
    if(listen(listenfd,LISTENQ)<0)
    {
        perror("listen error");
        exit(1);
    }
	printf("bind and listen success!wait accept...\n");
    while (1)
    {
        clilen = sizeof(cliaddr);
        connfd=accept(listenfd , (struct sockaddr *)&cliaddr , &clilen);
        if(connfd<0)
        {
            perror("accept error");
            exit(1);
        }
		printf("Connect from %s:%u \n",inet_ntoa(cliaddr.sin_addr),ntohs(cliaddr.sin_port)); 
        pthread_t id;
		//新建子进程单独处理链接
        pthread_create(&id,NULL,thread_run,(void*)&connfd);
        pthread_detach(id);
    }
    /*(6) 关闭监听套接字*/
    close(listenfd);
    return 0;
}

void *thread_run(void *arg)
{
    printf("creat a new thread\n");
    int fd = *(int*)arg;
    char* buf = (char*)malloc(bit*sizeof(char) + 1);
    memset(buf,'1',bit);
    printf("fd:%d\nbuf:%s\n",fd,buf);
    struct timeval timeout;
    // timeout.tv_sec=0;
    // timeout.tv_usec = 500000;
    timeout.tv_sec=1;
    timeout.tv_usec = 0;
    int ret = setsockopt(fd,SOL_SOCKET,SO_RCVTIMEO,&timeout,sizeof(timeout));   //设置recv超时时间为1s
    while (1)
    {
        /* code */
        int flag = send(fd,buf,bit,0);
        // printf("send size:%d\n",flag);
        // usleep(1000000);
        ret = recv(fd,buf,1,0);
        if(ret==0){
            printf("断开连接\n");
            break;
        }
        if(ret == -1)
        {
            if(errno == EWOULDBLOCK)
            {
                printf("超时\n");
            }
        }
    }
	close(fd);
}
相关标签: Linux经验