|
我是基于ACE类库做了一个UDP广播的发送和接收的例子,但是当发送的数据报大于1472字节时客户端就会接收不到。
我查了很多相关问题,据说是可以手动分片的 但是我不懂这方面的东西 有没有谁来指点一下?到底该如何分片,接收端该如何重组??
嘿嘿 先谢谢了~
代码如下:
sever.cpp
///
#include <ace/os.h>
#include <ace/Log_Msg.h>
#include <ace/INET_Addr.h>
#include <ace/SOCK_Dgram_Bcast.h>
#include <ace/Auto_Ptr.h>
int send_broadcast(ACE_INET_Addr& my_addr, u_short to_port, int time)
{
static const u_short PORT = ACE_DEFAULT_SERVER_PORT;
char message[1500];
memset(message,1,1485);
message[500] = '\0';
sprintf(message,"%s%03d",message,time);
ACE_DEBUG((LM_DEBUG,
ACE_TEXT("Send data: %d %d\n"),
strlen(message),time));
ACE_SOCK_Dgram_Bcast udp(my_addr);
ssize_t sent = udp.send(message,
ACE_OS_String::strlen(message) + 1,
to_port);
udp.close();
if (sent == -1)
ACE_ERROR_RETURN((LM_ERROR,
ACE_TEXT("%p\n"),
ACE_TEXT("send")),
-1);
return 0;
}
int ACE_TMAIN(int, ACE_TCHAR *[])
{
ACE_INET_Addr my_addr;
char hostname[MAXHOSTNAMELEN];
ACE_OS::hostname(hostname, MAXHOSTNAMELEN);
struct hostent* hptr;
hptr = ACE_OS::gethostbyname(hostname); //gethostbyname·μ??ò??????òhostentêy?Y?á11μ?????
char ip[32];
char** pptr;
switch(hptr->h_addrtype)
{
case AF_INET:
case AF_INET6:
pptr=hptr->h_addr_list;
for(;*pptr!=NULL;pptr++)
{
printf("My address: %s\n", ACE_OS::inet_ntop(hptr->h_addrtype, *pptr, ip, sizeof(ip)));
my_addr = ACE_INET_Addr(ACE_OS::inet_ntop(hptr->h_addrtype, *pptr, ip, sizeof(ip)));
}
break;
default:
printf("unknown address type\n");
break;
}
int time = 100;
while(time>0)
{
send_broadcast(my_addr, 50000, time);
Sleep(200);
time--;
}
return 0;
}
client.cpp
///
#include <ace/os.h>
#include <ace/Log_Msg.h>
#include <ace/INET_Addr.h>
#include <ace/SOCK_Dgram_Bcast.h>
void echo_dgram()
{
ACE_INET_Addr my_addr(ACE_static_cast(u_short, 50000));
ACE_INET_Addr your_addr;
ACE_SOCK_Dgram_Bcast udp(my_addr);
char buff[8192];
size_t buflen = sizeof (buff);
ssize_t recv_cnt = udp.recv(buff, buflen, your_addr);
if (recv_cnt > 0)
{
char numbuf[100];
numbuf[0] = '\0';
sprintf(numbuf,"%04d %s\n",strlen(buff),&buff[recv_cnt-4]);
write(1, numbuf, strlen(numbuf));
ssize_t sent = udp.send(buff, ACE_static_cast(size_t, buflen), your_addr);
}
udp.close();
return;
}
int ACE_TMAIN(int, ACE_TCHAR *[])
{
ACE_INET_Addr to(50000, ACE_LOCALHOST);
while(1)
{
echo_dgram();
}
return 0;
} |
|