-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
242 lines (196 loc) · 5.02 KB
/
server.cpp
File metadata and controls
242 lines (196 loc) · 5.02 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
//#define DEFAULT_PORT 5678
#define DEFAULT_BUFFER 81920//让其大点
typedef struct _PARAM
{
unsigned short bindPort;
unsigned char isEchoSrv;
long bigDataSize;
int perSize;
} PARAM;
int send_data( int sockfd, char* buf, int size )
{
int rsize = 0;
int errorcode;
while ( rsize != size )
{
errorcode = send( sockfd, buf + rsize, size - rsize, 0 );
if ( errorcode == SOCKET_ERROR )
{
printf("send Error[%d]\n",GetLastError());
errorcode = GetLastError();
return errorcode;
}
rsize += errorcode;
}
return 0;
}
int echo(SOCKET sock, int index)
{
char tmpBuffer[DEFAULT_BUFFER];
int errCode;
long totalEchoSize = 0;
while(1)
{
if((errCode = recv(sock, tmpBuffer, DEFAULT_BUFFER, 0)) == SOCKET_ERROR )
{
printf("第[%d]客户端,收数据发生错误[%d]\n", index, GetLastError());
return -1;
}
if(errCode == 0)
{
printf("第[%d]客户端,客户端已经关闭了套接字\n", index);
return -2;
}
if(send_data(sock, tmpBuffer, errCode) != 0)
{
printf("第[%d]客户端,发数据出错[%d]\n", index, GetLastError());
return -3;
}
totalEchoSize += errCode;
printf("第[%d]客户端,这次回显了[%d]字节,总共回显[%d]字节\n", index, errCode, totalEchoSize);
}
return 0;
}
int sendBigData(SOCKET sClient, int index, PARAM ¶m)
{
char recvBuffer[DEFAULT_BUFFER];
long needSendSize = param.bigDataSize;
int perSendSize = param.perSize;
long hadSendSize = 0;
char *pSendBuffer = NULL;
int errCode;
int onceSend;
unsigned int recvTimeOut = 5000;//5秒
if(setsockopt(sClient, SOL_SOCKET, SO_RCVTIMEO, (char *)&recvTimeOut, sizeof(unsigned int)) != 0)
{
printf("设置套接字超时时间出错\n");
return -1;
}
// if(recv(sClient, recvBuffer, DEFAULT_BUFFER, 0) == SOCKET_ERROR)
// {
// printf("recv Error[%d]\n", GetLastError());
//
// return -2;
// }
if((pSendBuffer = (char* )malloc(needSendSize)) == NULL)
{
printf("第[%d]客户端,malloc [%d]字节失败\n", index, needSendSize);
return -3;
}
printf("第[%d]客户端,开始向客户端发[%d]字节\n", index, needSendSize);
while(1)
{
onceSend = min(needSendSize, perSendSize);
errCode = send_data(sClient, pSendBuffer+hadSendSize, onceSend);
if(errCode != 0)
{
printf("第[%d]客户端,发数据出错[%d]\n", index, errCode);
errCode = -4;
goto end;
}
hadSendSize += onceSend;
needSendSize -= onceSend;
printf("第[%d]客户端,此次发了[%d]字节,总共已经发了[%d]字节\n", index, onceSend, hadSendSize);
if(needSendSize <= 0)
break;
}
printf("第[%d]客户端,总共向客户端发送了[%d]\n",index, hadSendSize);
end:
if(pSendBuffer != NULL)
{
free(pSendBuffer);
pSendBuffer = NULL;
}
return errCode;
}
int doDeal(SOCKET sClient, int index, PARAM ¶m)
{
int err;
switch (param.isEchoSrv)
{
case 0:
err = sendBigData(sClient, index, param);
break;
case 1:
err = echo(sClient, index);
break;
default:
err = -1;
break;
}
return err;
}
int main(int argc, char **argv)
{
WSADATA wsd;
SOCKET sListen, sClient;
int iAddrSize;
struct sockaddr_in local, client;
PARAM param;
if(argc != 5)
{
printf("usage: bindPort isEcho bigDataSize perSendSize\n");
return -1;
}
param.bindPort = atoi(argv[1]);
param.isEchoSrv = atoi(argv[2]);
param.bigDataSize = atol(argv[3]);
param.perSize = atoi(argv[4]);
if(param.isEchoSrv > 1 || param.isEchoSrv < 0)
{
printf("isEcho 参数只能为0或者1\n");
return -2;
}
if(param.isEchoSrv)
{
printf("本程序是回显服务器,收客户端的数据,然后原样返回给客户端\n");
}else
{
printf("本程序,当收到客户端的连接后,不收数据,直接返回给客户端[%d]字节\n\t如果发的数据超过了[%d]字节,则每次发[%d]字节\n\n", param.bigDataSize, param.perSize, param.perSize);
}
if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
{
printf("Failed to load Winsock!\n");
return -1;
}
sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if (sListen == SOCKET_ERROR)
{
printf("socket() failed: %d\n", WSAGetLastError());
return -2;
}
local.sin_addr.s_addr = htonl(INADDR_ANY);
local.sin_family = AF_INET;
local.sin_port = htons(param.bindPort);
if (bind(sListen, (struct sockaddr *)&local,
sizeof(local)) == SOCKET_ERROR)
{
printf("bind() failed: %d\n", WSAGetLastError());
return 1;
}
listen(sListen, 5);
int index =0;
printf("开始监听,监听端口[%d]\n", param.bindPort);
while (1)
{
iAddrSize = sizeof(client);
sClient = accept(sListen, (struct sockaddr *)&client,
&iAddrSize);
if (sClient == INVALID_SOCKET)
{
printf("accept() failed: %d\n", WSAGetLastError());
break;
}
printf("第[%d]连接: %s:%d到达\n", ++index,
inet_ntoa(client.sin_addr), ntohs(client.sin_port));
doDeal(sClient, index, param);
closesocket(sClient);
//echo(sClient, index);
}
closesocket(sListen);
WSACleanup();
return 0;
}