#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 1024
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];
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");
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 1024
int 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 1024
void * 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 1024
int 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 80
int 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];
bzero(&sin,sizeof(struct sockaddr_in));
sin.sin_family=AF_INET;
sin.sin_addr.s_addr=htonl(INADDR_ANY);
sin.sin_port=htons(portnumber);
if((lfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
fprintf(stderr,"Socket error:%s\n\a",strerror(errno));
exit(1);
}
printf("socket!\n");
setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
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++)
{
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 8000
int 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);
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
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来充当监管作用