用户登录  |  用户注册
首 页商业源码原创产品编程论坛
当前位置:PB创新网文章中心编程技巧C++ Builder

UDP

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2009-03-16 20:40:30

一、说明
   使用UDP时,直接使用API代替控件。

   第一个程序(ReadBufferUdp)使用来接收到缓存中。

   "Destino"变量非常重要,如果你从其他地方接收数据到Buffer,你必须设置Destino = 0 并且在以后执行的时候赋值你将要发送的包的地址给它(after the execution it will have the address which send you the packet.)。
   如果你只想从一个指定的地址接收数据,你必须设置变量Destino = <address>.

   "gvEncerrar" 用来中止处理过程。(gvEncerrar被设置为全局变量。)

   超时时间设置。"Inicio + 12" = 12 sec of timeout.

   第三个程序是用来准备WinSock程序。

二、代码

int ReadBufferUdp(unsigned long *Destino,void *T,int Size)
{
   char Buffer[128];
   SOCKADDR_IN SockAddr;
   int LenSockAddr=sizeof(SOCKADDR_IN);
   fd_set FdRead;
   struct timeval t_val;
   int Ret;
   time_t Inicio = time(NULL);

   Application->ProcessMessages();
   if(gvEncerrar)
      return false;

   FD_ZERO(&FdRead);
   FD_SET(gvSocket,&FdRead);
   t_val.tv_sec=0;
   t_val.tv_usec=0;

   while((Ret=select(0,&FdRead,NULL,NULL,&t_val))!=1 && (Inicio + 12) >
time(NULL) && !gvEncerrar)
   {
      FD_ZERO(&FdRead);
      FD_SET(gvSocket,&FdRead);
      t_val.tv_sec=0;
      t_val.tv_usec=0;
      Application->ProcessMessages();
   }
   if(Ret != 1)
      return false;

if(recvfrom(gvSocket,Buffer,Size,0,(LPSOCKADDR)&SockAddr,&LenSockAddr)!=Size)

      return false;

   if(*Destino == 0)
   {
      *Destino = SockAddr.sin_addr.s_addr;
   }
   else
   if(*Destino != SockAddr.sin_addr.s_addr)
      return false;

   memcpy(T,Buffer,Size);
   return true;
}

int WriteBufferUdp(unsigned long Destino,void *T,int Size)
{
   SOCKADDR_IN SockAddr;
   int Sent;

   Application->ProcessMessages();
   SockAddr.sin_family = AF_INET;
   SockAddr.sin_port = gvPortUdp;
   SockAddr.sin_addr.s_addr = Destino;
   Sent = sendto(gvSocket,(char
*)T,Size,0,(LPSOCKADDR)&SockAddr,sizeof(SockAddr));
   if(Sent != Size)
      return false;
   else
      return true;
}

void InicializaTCPIP()
{

   WORD wVersionRequested;
   WSADATA wsaData;
   IN_ADDR In;
   PSERVENT PServent;
   SOCKADDR_IN SockAddrIn;
   wVersionRequested = MAKEWORD( 1, 1 );

  if(WSAStartup( wVersionRequested, &wsaData ))
   {
      ShowMessage("Erro na inicializao do TCP/IP");
      Application->Terminate();
      return;
   }

   // Get the port on service file
   if((PServent=getservbyname("your_service_name","udp"))==NULL)
   {
      ShowMessage("Erro obtendo port do servi transurb/udp");
      Application->Terminate();
      return;
   }
   gvPortUdp = PServent->s_port;
   sprintf(StrAux,"Servi transurb/udp port:%d",ntohs(gvPortUdp));
   Log(StrAux);

   // Open de Socket
   if((gvSocket = socket(AF_INET,SOCK_DGRAM,0))==INVALID_SOCKET)
   {
       ShowMessage("Erro na criao do socket");
       Application->Terminate();
       return;
   }
   Log("Socket criado com sucesso");

   // Do the bind
   SockAddrIn.sin_family = AF_INET;
   SockAddrIn.sin_port = gvPortUdp;
   SockAddrIn.sin_addr.s_addr = NULL;

if(bind(gvSocket,(LPSOCKADDR)&SockAddrIn,sizeof(SockAddrIn))==SOCKET_ERROR)

   {
      ShowMessage("Erro no bind do socket");
      Application->Terminate();
      return;
   }
   Log("Bind do socket com sucesso");

}

Tags:

作者:佚名

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
PB创新网ourmis.com】Copyright © 2000-2009 . All Rights Reserved .
页面执行时间:6,296.87500 毫秒
Email:ourmis@126.com QQ:2322888 蜀ICP备05006790号