下载了《ACE自适配通信环境中文技术文档》,很不错,感谢马维达同志。
从中篇第二章开始看,按照上一篇的方法将数据报的代码放入项目编译、运行,发现几个
问题:
1. 服务端收不到消息,检查客户端发现发送失败,追查下去,可能由于ACE的版本更新,
按原来的写法在远端地址初始化时出现了问题,给地址时应该以"IP:Port"的方式给出。
2. 客户端发送成功后,服务端成功收到并回执,但是客户端没收到。检查后,发现是客
户端接收长度不够,将SIZE_DATA改成29就行了。
3. 继续调试,发现客户端第二次发送时,服务端突然退出了。一检查,由于客户端将新
收下的字符串发送出去了,超过了第一次发送的字符串的长度,服务器接收失败。因此,需要
在客户端接收成功后,重新设置需要发送的字符串。
下面是修改过的代码:
1
//Server
2
#include "ace/OS.h"
3
#include "ace/SOCK_Dgram.h"
4
#include "ace/INET_Addr.h"
5
#include "ace/Log_Msg.h"
6
7
#ifndef _DEBUG
8
#pragma comment( lib, "ace.lib" )
9
#else
10
#pragma comment( lib, "aced.lib" )
11
#endif
12
13
#define DATA_BUFFER_SIZE 1024
14
#define SIZE_DATA 19
15
16
class Server
17
{
18
public:
19
Server(int local_port)
20
:local_addr_(local_port),local_(local_addr_)
21
{
22
data_buf = new char[DATA_BUFFER_SIZE];
23
}
24
25
26
//Expect data to arrive from the remote machine. Accept it and display
27
//it. After receiving data, immediately send some data back to the
28
//remote.
29
int accept_data()
30
{
31
int byte_count=0;
32
while( (byte_count=local_.recv(data_buf,SIZE_DATA,remote_addr_))!=-1)
33
{
34
data_buf[byte_count]=0;
35
ACE_DEBUG((LM_DEBUG, "Data received from remote %s was %s \n"
36
,remote_addr_.get_host_name(), data_buf));
37
ACE_OS::sleep(1);
38
if(send_data()==-1) break;
39
}
40
return -1;
41
}
42
43
//Method used to send data to the remote using the datagram component
44
//local_
45
int send_data()
46
{
47
ACE_DEBUG((LM_DEBUG,"Preparing to send reply to client %s:%d\n",
48
remote_addr_.get_host_name(),remote_addr_.get_port_number()));
49
ACE_OS::sprintf(data_buf,"Server says hello to you too");
50
if( local_.send(data_buf, ACE_OS::strlen(data_buf)+1,remote_addr_)==-1)
51
return -1;
52
else
53
return 0;
54
}
55
56
private:
57
char *data_buf;
58
ACE_INET_Addr remote_addr_;
59
ACE_INET_Addr local_addr_;
60
ACE_SOCK_Dgram local_;
61
};
62
63
64
int main(int argc, char *argv[])
65
{
66
if(argc<2)
67
{
68
ACE_DEBUG((LM_DEBUG,"Usage %s <Port Number>", argv[0]));
69
ACE_OS::exit(1);
70
}
71
Server server(ACE_OS::atoi(argv[1]));
72
server.accept_data();
73
return 0;
74
}
75

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

1
//Client
2
#include "ace/OS.h"
3
#include "ace/SOCK_Dgram.h"
4
#include "ace/INET_Addr.h"
5
#include "ace/Log_Msg.h"
6
7
#ifndef _DEBUG
8
#pragma comment( lib, "ace.lib" )
9
#else
10
#pragma comment( lib, "aced.lib" )
11
#endif
12
13
#define DATA_BUFFER_SIZE 1024
14
#define SIZE_DATA 29
15
16
class Client
17
{
18
public:
19
Client(char * remote_host)
20
:remote_addr_(remote_host),
21
local_addr_((u_short)0),local_(local_addr_)
22
{
23
data_buf = new char[DATA_BUFFER_SIZE];
24
//remote_addr_.set_port_number(port);
25
}
26
27
//Receive data from the remote host using the datgram wrapper `local_’.
28
//The address of the remote machine is received in `remote_addr_’
29
//which is of type ACE_INET_Addr. Remember that there is no established
30
//connection.
31
int accept_data()
32
{
33
if(local_.recv(data_buf,SIZE_DATA,remote_addr_)!=-1)
34
{
35
ACE_DEBUG((LM_DEBUG, "Data received from remote server %s was: %s \n" ,
36
remote_addr_.get_host_name(), data_buf));
37
return 0;
38
}
39
else
40
return -1;
41
}
42
43
//Send data to the remote. Once data has been sent wait for a reply
44
//from the server.
45
int send_data()
46
{
47
ACE_DEBUG((LM_DEBUG,"Preparing to send data to server %s:%d\n",
48
remote_addr_.get_host_name(),remote_addr_.get_port_number()));
49
ACE_OS::sprintf(data_buf,"Client says hello");
50
51
while(local_.send(data_buf,ACE_OS::strlen(data_buf),remote_addr_)!=-1)
52
{
53
ACE_OS::sleep(1);
54
if(accept_data()==-1)
55
break;
56
ACE_OS::sprintf(data_buf,"Client says hello");
57
}
58
59
return -1;
60
}
61
62
private:
63
char *data_buf;
64
ACE_INET_Addr remote_addr_;
65
ACE_INET_Addr local_addr_;
66
ACE_SOCK_Dgram local_;
67
};
68
69
int main(int argc, char *argv[])
70
{
71
if(argc<2)
72
{
73
ACE_OS::printf("Usage: %s <hostname:port_number> \n", argv[0]);
74
ACE_OS::exit(1);
75
}
76
Client client(argv[1]);
77
client.send_data();
78
system("pause");
79
return 0;
80
}

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
