#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LOG(s) printf("[%s] {%s:%d} %s \n", __DATE__, __FILE__, __LINE__, s);
int server_handler(int server, struct sockaddr_in saveCaddr[])
{
struct sockaddr_in addr = {0};
socklen_t asize = sizeof(addr);
int ret = -1;
if((ret = accept(server, (struct sockaddr*)&addr, &asize)) == -1)
{
LOG("accept error");
perror("accept error");
return -1;
}
printf("[%s/%d] client%d 已连接\n", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port), ret);
saveCaddr[ret] = addr;
return ret;
}
int client_handler(int client, struct sockaddr_in* saveCaddr)
{
char buf[128] = {0};
bzero(buf, sizeof(buf));
int ret = read(client, buf, sizeof(buf)-1);
if(ret == 0)
{
printf("[%s/%d] client%d 已下线\n", inet_ntoa(saveCaddr[client].sin_addr), ntohs(saveCaddr[client].sin_port), client);
ret = -1;
}
else if(ret > 0)
{
buf[ret] = 0;
printf("[%s/%d] client%d:%s\n", inet_ntoa(saveCaddr[client].sin_addr), ntohs(saveCaddr[client].sin_port), client, buf);
if(strcmp(buf, "quit") != 0)
{
ret = write(client, buf, ret);
}
else
{
printf("[%s/%d] client%d 已下线\n", inet_ntoa(saveCaddr[client].sin_addr), ntohs(saveCaddr[client].sin_port), client);
ret = -1;
}
}
return ret;
}
int main(char argc, char* argv[])
{
int server = 0;
struct sockaddr_in saddr = {0};
int max = 0;
int num = 0;
fd_set reads = {0};
fd_set temps = {0};
server = socket(PF_INET, SOCK_STREAM, 0);
if(server == -1)
{
puts("server socket error");
return -1;
}
int reuse = 1;
if(setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
LOG("setsockopt");
return -1;
}
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = htonl(INADDR_ANY);
saddr.sin_port = htons(8899);
if(bind(server, (struct sockaddr*)&saddr, sizeof(saddr)) == -1)
{
puts("server bind error");
return -1;
}
if(listen(server, 5) == -1)
{
puts("server listen error");
return -1;
}
puts("server start success");
FD_ZERO(&reads);
FD_SET(server, &reads);
FD_SET(0, &reads);
max = server;
struct sockaddr_in saveCaddr[1024];
while(1)
{
temps = reads;
num = select(max+1, &temps, NULL, NULL, NULL);
if(num > 0)
{
for(int i = 0; i <= max; i++)
{
if(FD_ISSET(i, &temps) == 0)
continue;
if(0 == i)
{
puts("键盘事件");
char buf[128] = "";
bzero(buf, sizeof(buf));
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = 0;
printf("input: %s\n",buf);
}
else if(server == i)
{
puts("连接事件");
int client = server_handler(server, saveCaddr);
if(client > -1)
{
FD_SET(client, &reads);
max = (client > max)? client : max;
}
}
else
{
puts("通信事件");
int r = client_handler(i, saveCaddr);
if(r == -1)
{
FD_CLR(i, &reads);
close(i);
while(FD_ISSET(max, &reads) == 0 && max-- >= 0);
}
}
}
}
}
close(server);
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