Ferramentas do usuário

Ferramentas do site


htonl

Diferenças

Aqui você vê as diferenças entre duas revisões dessa página.


htonl [2023/09/12 16:14] (atual) – criada - edição externa 127.0.0.1
Linha 1: Linha 1:
 +====== Funções htonl, htons, ntohl, ntohs ======
  
 +Sintaxe:
 +
 +''[[uint32_t]] htonl([[uint32_t]] hostlong);''
 +
 +''[[uint16_t]] htons([[uint16_t]] hostshort);''
 +
 +''[[uint32_t]] ntohl([[uint32_t]] netlong);''
 +
 +''[[uint16_t]] ntohs([[uint16_t]] netshort);''
 +
 +----
 +
 +As funções convertem e retornam um o endereço passado como parâmetro para um ordenamento de byte significativo. Sendo que as funções **htons** e **htonl** retornam o valor na ordem de bytes da rede e as funções **ntohs** e **ntohl** retornam o valor na ordem de bytes de um host.
 +
 +  * htons - //Host to Network Short//
 +  * htonl - //Host to Network Long//
 +  * ntohs - //Network to Host Short//
 +  * ntohl - //Network to Host Long//
 +
 +Veja o exemplo:
 +<code c>
 +#include <stdio.h>
 +#include <stdlib.h>
 +#include <strings.h>
 +#include <errno.h>
 +#include <sys/types.h>
 +#include <sys/socket.h>
 +#include <netinet/in.h>
 +
 +int main(void)
 +{
 +   int iSock;
 +   struct sockaddr_in my_addr;
 +
 +   iSock = socket(AF_INET, SOCK_STREAM, 0);
 +   if( iSock == -1)
 +   {
 +      perror("socket:");
 +      exit(1);
 +   }
 +
 +   my_addr.sin_family = AF_INET;
 +   my_addr.sin_port = htons(4950); /* convertendo endereço de host (porta) no formato decimal para ordenamento de bytes */
 +   my_addr.sin_addr.s_addr = INADDR_ANY;
 +   bzero(&(my_addr.sin_zero), 8);
 +
 +   return 0;
 +}
 +</code>
 +
 + --- //[[marcos@laureano.eti.br|Marcos Laureano]] 2008/04/25 06:09//