国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

UDP編程

2019-11-17 05:46:33
字體:
供稿:網(wǎng)友

  一、說明
   使用UDP時(shí),直接使用API代替控件。   第一個(gè)程序(ReadBufferUdp)使用來接收到緩存中。    "Destino"變量非常重要,假如你從其他地方接收數(shù)據(jù)到Buffer,你必須設(shè)置Destino = 0 并且在以后執(zhí)行的時(shí)候賦值你將要發(fā)送的包的地址給它(after the execution it will have the address which send you the packet.)。
   假如你只想從一個(gè)指定的地址接收數(shù)據(jù),你必須設(shè)置變量Destino = <address>.    "gvEncerrar" 用來中止處理過程。(gvEncerrar被設(shè)置為全局變量。)    超時(shí)時(shí)間設(shè)置。"Inicio + 12" = 12 sec of timeout.
 
   第三個(gè)程序是用來預(yù)備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"); }

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 太谷县| 怀柔区| 曲周县| 融水| 白银市| 嘉义县| 洮南市| 晋城| 大理市| 沛县| 龙里县| 墨竹工卡县| 安徽省| 仙游县| 东乡族自治县| 巴楚县| 哈密市| 南充市| 麦盖提县| 运城市| 景洪市| 中卫市| 偏关县| 西和县| 福州市| 新乐市| 万年县| 都兰县| 尉氏县| 桓台县| 凤台县| 平南县| 台北县| 榕江县| 张掖市| 陈巴尔虎旗| 淮南市| 寻甸| 昌平区| 盐城市| 南江县|