-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
102 lines (82 loc) · 2.04 KB
/
server.c
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
#include "common.h"
int main ()
{
int sockfd = 0, iRet = 0, sockClient = 0;
char caBuf[BUFFER_SIZE];
char caRspMsg[BUFFER_SIZE];
RSPSERVERCONFIG rspServerConfig;
struct timeval timeout;
int iRspLen = 0;
memset ( caBuf, 0x00, sizeof caBuf );
iRspLen = readRspServerConfig ( &rspServerConfig );
if ( iRspLen < 0 )
{
writeLog ( ERR_LOG, "readRspServerConfig error", 0, 0 );
return -1;
}
sockfd = initSocket ();
if ( sockfd < 0 )
{
writeLog ( ERR_LOG, "initSocket error", 0, 0 );
return -1;
}
writeLog ( INFO_LOG, "initSocket...", 0, 0 );
printf ( "initSocket...\n" );
iRet = sockListen ( sockfd );
if ( sockfd < 0 )
{
writeLog ( ERR_LOG, "sockListen error", 0, 0 );
return -1;
}
writeLog ( INFO_LOG, "listen...", 0, 0 );
printf ( "listen...\n" );
while ( 1 )
{
sockClient = sockAccept ( sockfd );
if ( sockClient < 0 )
{
writeLog ( ERR_LOG, "sockAccept error", 0, 0 );
return -1;
}
writeLog ( INFO_LOG, "accept...", 0, 0 );
printf ( "accept...\n" );
/* 设置接收超时时间 */
timeout.tv_sec = rspServerConfig.timeout;
timeout.tv_usec = 0;
iRet = setsockopt ( sockClient, SOL_SOCKET, SO_RCVTIMEO, ( char * ) &timeout, sizeof( timeout ) );
if ( iRet < 0 )
{
writeLog ( ERR_LOG, "setsockopt失败", 0, 0 );
return -1;
}
while ( 1 )
{
iRet = sockRecv ( sockClient, caBuf, sizeof caBuf );
if ( iRet < 0 )
{
writeLog ( ERR_LOG, "sockRecv error", 0, 0 );
break;
}else if ( ! iRet )
{
printf ( "对方关闭链接\n" );
break;
}
printf ( "服务器接收到报文:\n" );
printLog ( caBuf, iRet );
sprintf ( caRspMsg, "%08d", iRspLen + 68 );
memcpy ( caRspMsg + 8, caBuf + 8, 68 );
memcpy ( caRspMsg + 8 + 68, rspServerConfig.caMsg, iRspLen );
iRet = sockSend ( sockClient, caRspMsg, iRspLen + 8 + 68 );
if ( iRet < 0 )
{
writeLog ( ERR_LOG, "sockSend error", 0, 0 );
break;
}
printf ( "服务器接回应报文:\n" );
printLog ( caRspMsg, iRspLen + 8 + 68 );
}
close ( sockClient );
}
close ( sockfd );
return 0;
}