Linux c==网络编程、循环服务器、并发服务器、I/O多路转接

来源:互联网 发布:python 1 12 123 1234 编辑:程序博客网 时间:2024/05/17 04:53
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <sys/stat.h>#include <netdb.h>#define PORT 3333#define MAX_SIZE 1024int main(){    int sockfd;    int new_fd;    struct sockaddr_in server_addr;    struct sockaddr_in client_addr;    int n_read;    int ser_size;    int opt = 1;    char buffer[MAX_SIZE];    if((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0)    {        perror("socket error!\n");    exit(1);    }    printf("socket success.............!\n");    if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt)) < 0)    {        perror("set socket option error!\n");    exit(1);    }    bzero(&server_addr,0);    server_addr.sin_family = AF_INET;    server_addr.sin_port = htons(PORT);    server_addr.sin_addr.s_addr = inet_addr("192.168.1.10");     if(bind(sockfd,(struct sockaddr *)&server_addr,sizeof server_addr) < 0)    {        perror("bind error!\n");    exit(1);    }    printf("bind success.............!\n");    listen(sockfd,5);    printf("listen success.............!\n");    printf("accepting..................!\n");    while(1)    {        ser_size = sizeof client_addr;        if((new_fd = accept(sockfd,(struct sockaddr *)&client_addr,&ser_size)) < 0)        {            perror("accept error!\n");        exit(1);        }        printf("accept success.................!\n");    //read\recv        printf("reading............!\n");        n_read = read(new_fd,buffer,sizeof(buffer));        if(n_read < 0)        {            perror("read client msg error!\n");        exit(1);        }        buffer[n_read] = '\0';        printf("recv msg = %s\n",buffer);    }    return 0;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89

客户端:

#include <stdio.h>#include <string.h>#include <stdlib.h>#include <sys/types.h>#include <sys/socket.h>#include <netdb.h>#define PORT 3333#define MAX_SIZE 1024int main(int argc, char *argv[]){    if(argc != 2)    {        printf("Please input server ip!\n");    exit(1);    }    int sockfd;    int n_write;    struct sockaddr_in server_addr;    char buffer[MAX_SIZE];    if((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0)    {        perror("client socket error!\n");    exit(1);    }    bzero(&server_addr,0);    server_addr.sin_family = AF_INET;    server_addr.sin_port = htons(PORT);    server_addr.sin_addr.s_addr = inet_addr(argv[1]);    if(connect(sockfd,(struct sockaddr *)&server_addr,sizeof server_addr) < 0)    {        perror("connect server error!\n");    exit(1);    }    while(1)    {        memset(buffer,0,sizeof(buffer));    printf("Please input send msg:\n");        gets(buffer);        n_write = write(sockfd,buffer,strlen(buffer));    if(n_write == -1)    {        perror("send to server msg error!\n");        exit(1);    }    }    return 0;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

缺点:只能接收到一个客户端,且等待其结束后才能接受下一个客户端

并发服务器:

服务器:

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <sys/stat.h>#include <netdb.h>#define PORT 3333#define MAX_SIZE 1024void * read_msg(void *arg){    int n_read;    int new_fd = *((int *)arg);    char buffer[MAX_SIZE];    printf("new_fd = %d\n",new_fd);    while(1)    {        memset(buffer,0,sizeof(buffer));    n_read = read(new_fd,buffer,sizeof(buffer));    if(n_read < 0)    {        perror("recv client msg error!\n");        exit(1);    }    if(n_read == 0)    {        printf("client is close!!\n");        close(new_fd);        pthread_exit(NULL);    }    buffer[n_read] = '\0';    printf("recv msg:%s\n",buffer);    }}int main(){    int sockfd;    int new_fd;    struct sockaddr_in server_addr;    struct sockaddr_in client_addr;    int n_read;    int ser_size;    int opt = 1;    char buffer[MAX_SIZE];    pthread_t id;    if((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0)    {        perror("socket error!\n");    exit(1);    }    printf("socket success.............!\n");    if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt)) < 0)    {        perror("set socket option error!\n");    exit(1);    }    bzero(&server_addr,0);    server_addr.sin_family = AF_INET;    server_addr.sin_port = htons(PORT);    server_addr.sin_addr.s_addr = inet_addr("192.168.1.10");     if(bind(sockfd,(struct sockaddr *)&server_addr,sizeof server_addr) < 0)    {        perror("bind error!\n");    exit(1);    }    printf("bind success.............!\n");    listen(sockfd,5);    printf("listen success.............!\n");    printf("accepting..................!\n");    while(1)    {        ser_size = sizeof client_addr;        if((new_fd = accept(sockfd,(struct sockaddr *)&client_addr,&ser_size)) < 0)        {            perror("accept error!\n");        exit(1);        }        printf("accept success.................!\n");    pthread_create(&id,NULL,read_msg,&new_fd);    }    return 0;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113

客户端:

#include <stdio.h>#include <string.h>#include <stdlib.h>#include <sys/types.h>#include <sys/socket.h>#include <netdb.h>#define PORT 3333#define MAX_SIZE 1024int main(int argc, char *argv[]){    if(argc != 2)    {        printf("Please input server ip!\n");    exit(1);    }    int sockfd;    int n_write;    struct sockaddr_in server_addr;    char buffer[MAX_SIZE];    if((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0)    {        perror("client socket error!\n");    exit(1);    }    bzero(&server_addr,0);    server_addr.sin_family = AF_INET;    server_addr.sin_port = htons(PORT);    server_addr.sin_addr.s_addr = inet_addr(argv[1]);    if(connect(sockfd,(struct sockaddr *)&server_addr,sizeof server_addr) < 0)    {        perror("connect server error!\n");    exit(1);    }    while(1)    {        memset(buffer,0,sizeof(buffer));    printf("Please input send msg:\n");        gets(buffer);        n_write = write(sockfd,buffer,strlen(buffer));    if(n_write == -1)    {        perror("send to server msg error!\n");        exit(1);    }    }    return 0;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

缺点:在客户端没有发送数据时,每个线程仍需轮流切换,浪费CPU资源

I/O多路转接:

服务器:

#include <stdlib.h>#include <stdio.h>#include <errno.h>#include <string.h>#include <netdb.h>#include <sys/types.h>#include <netinet/in.h>#include <sys/socket.h>#include<unistd.h>#include<arpa/inet.h>#include<ctype.h>/* 宏定义端口号 */#define portnumber 8000#define MAX_LINE 80int main(void){    int  lfd;    int cfd;    int sfd;    int rdy;    struct sockaddr_in sin;    struct sockaddr_in cin;    int client[FD_SETSIZE];  /* 客户端连接的套接字描述符数组 */    int maxi;    int maxfd;                        /* 最大连接数 */    fd_set rset;    fd_set allset;    socklen_t addr_len;         /* 地址结构长度 */    char buffer[MAX_LINE];    int i;    int n;    int len;    int opt = 1;   /* 套接字选项 */    char addr_p[20];    /* 对server_addr_in  结构进行赋值  */    bzero(&sin,sizeof(struct sockaddr_in));   /* 先清零 */    sin.sin_family=AF_INET;                 //    sin.sin_addr.s_addr=htonl(INADDR_ANY);  //表示接受任何ip地址   将ip地址转换成网络字节序    sin.sin_port=htons(portnumber);         //将端口号转换成网络字节序    /* 调用socket函数创建一个TCP协议套接口 */    if((lfd=socket(AF_INET,SOCK_STREAM,0))==-1) // AF_INET:IPV4;SOCK_STREAM:TCP    {        fprintf(stderr,"Socket error:%s\n\a",strerror(errno));        exit(1);    }    printf("socket!\n");    /*设置套接字选项 使用默认选项*/    setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));    /* 调用bind函数 将serer_addr结构绑定到sockfd上  */    if(bind(lfd,(struct sockaddr *)(&sin),sizeof(struct sockaddr))==-1)    {        fprintf(stderr,"Bind error:%s\n\a",strerror(errno));        exit(1);    }    printf("bind!\n");    /* 开始监听端口   等待客户的请求 */    if(listen(lfd,20)==-1)    {        fprintf(stderr,"Listen error:%s\n\a",strerror(errno));        exit(1);    }    printf("listen!\n");    printf("Accepting connections .......\n");    maxfd = lfd;                                /*对最大文件描述符进行初始化*/    maxi = -1;    /*初始化客户端连接描述符集合*/    for(i = 0;i < FD_SETSIZE;i++)    {        client[i] = -1;    }    FD_ZERO(&allset);                     /* 清空文件描述符集合 */    FD_SET(lfd,&allset);                    /* 将监听字设置在集合内 */    /* 开始服务程序的死循环 */    while(1)    {        rset = allset;        printf("selecting!\n");        /*得到当前可以读的文件描述符数*/        rdy = select(maxfd + 1, &rset, NULL, NULL, NULL);        printf("selected!\n");        if(FD_ISSET(lfd, &rset))        {            addr_len = sizeof(sin);            printf("accepting!\n");            /* 接受客户端的请求 */            if((cfd=accept(lfd,(struct sockaddr *)(&cin),&addr_len))==-1)            {                fprintf(stderr,"Accept error:%s\n\a",strerror(errno));                exit(1);            }            printf("accepted!\n");                     /*查找一个空闲位置*/            for(i = 0; i<FD_SETSIZE; i++)            {       //printf("%d\t",client[i]);                if(client[i] <= 0)                {                    client[i] = cfd;   /* 将处理该客户端的连接套接字设置到该位置 */                    break;                }            }        /* 太多的客户端连接   服务器拒绝俄请求  跳出循环 */            if(i == FD_SETSIZE)            {                printf("too many clients");                exit(1);            }            FD_SET(cfd, &allset);     /* 设置连接集合 */            if(cfd > maxfd)                  /* 新的连接描述符 */            {                maxfd = cfd;            }            if(i > maxi)            {                maxi = i;            }            if(--rdy <= 0)                /* 减少一个连接描述符 */            {                continue;            }        }        /* 对每一个连接描述符做处理 */        for(i = 0;i< FD_SETSIZE;i++)        {            if((sfd = client[i]) < 0)            {                continue;            }            if(FD_ISSET(sfd, &rset))            {                printf("reading!\n");                n = read(sfd,buffer,MAX_LINE);                printf("%s\n",buffer);                printf("read!\n");                if(n == 0)                {                    printf("the other side has been closed. \n");                    fflush(stdout);                                    /* 刷新 输出终端 */                    close(sfd);                    FD_CLR(sfd, &allset);                        /*清空连接描述符数组*/                    client[i] = -1;                }                else                {                    /* 将客户端地址转换成字符串 */                    inet_ntop(AF_INET, &cin.sin_addr, addr_p, sizeof(addr_p));                    addr_p[strlen(addr_p)] = '\0';                    /*打印客户端地址 和 端口号*/                    printf("Client Ip is %s, port is %d\n",addr_p,ntohs(cin.sin_port));                    /* 谐函数出错 */                    if(n == 1)                    {                        exit(1);                    }                }                /*如果没有可以读的套接字   退出循环*/                if(--rdy <= 0)                {                    break;            }        }    }    close(lfd);       /* 关闭链接套接字 */    return 0;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212

客户端:

#include <stdlib.h> #include <stdio.h> #include <errno.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #define portnumber 8000int main(int argc, char *argv[]) {     int nbytes;    int sockfd;     char buffer[80];     char buffer_2[80];    struct sockaddr_in server_addr;     struct hostent *host;     if(argc!=2)     {         fprintf(stderr,"Usage:%s hostname \a\n",argv[0]);         exit(1);     }     if((host=gethostbyname(argv[1]))==NULL)     {         fprintf(stderr,"Gethostname error\n");         exit(1);     }      /* 调用socket函数创建一个TCP协议套接口 */     if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) // AF_INET:Internet;SOCK_STREAM:TCP    {         fprintf(stderr,"Socket Error:%s\a\n",strerror(errno));         exit(1);     }     bzero(&server_addr,sizeof(server_addr)); //     server_addr.sin_family=AF_INET;    server_addr.sin_port=htons(portnumber);    server_addr.sin_addr = *((struct in_addr *)host->h_addr);//?    if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)     {         fprintf(stderr,"Connect Error:%s\a\n",strerror(errno));         exit(1);     }     while(1){    printf("Please input char:\n");    fgets(buffer,1024,stdin);     write(sockfd,buffer,strlen(buffer));       #if 1    if((nbytes=read(sockfd,buffer_2,81))==-1)         {             fprintf(stderr,"Read Error:%s\n",strerror(errno));            exit(1);         }        buffer_2[nbytes]='\0';        printf("Client received from Server %s\n",buffer_2);        #endif    }       close(sockfd);    exit(0); } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76

优点:使用select来充当监管作用

0 0