The translation is temporarily closed for contributions due to maintenance, please come back later.

Translation

(itstool) path: sect2/title
English
<function>gethostbyname</function>
Context English Portuguese (Brazil) State
Try again, this time as the superuser: Tente novamente, desta vez como o superusuário:
<prompt>#</prompt> <userinput>./daytimed</userinput>
<prompt>#</prompt>
<prompt>#</prompt> <userinput>./daytimed</userinput>
<prompt>#</prompt>
What... Nothing? Let us try again: O que ... Nada? Vamos tentar de novo:
<prompt>#</prompt> <userinput>./daytimed</userinput>

bind: Address already in use
<prompt>#</prompt>
<prompt>#</prompt> <userinput>./daytimed</userinput>

bind: Address already in use
<prompt>#</prompt>
Every port can only be bound by one program at a time. Our first attempt was indeed successful: It started the child daemon and returned quietly. It is still running and will continue to run until you either kill it, or any of its system calls fail, or you reboot the system. Cada porta só pode ser ligada por um programa de cada vez. Nossa primeira tentativa foi bem sucedida: ela iniciou o daemon filho e retornou silenciosamente. Ele ainda está em execução e continuará em execução até que você o mate ou qualquer um de seus sistemas chamadas falhar, ou você reiniciar o sistema
Fine, we know it is running in the background. But is it working? How do we know it is a proper <emphasis>daytime</emphasis> server? Simple: Tudo bem, sabemos que está sendo executado em segundo plano. Mas está funcionando? Como sabemos que é um bom <emphasis> dia </emphasis> servidor? Simples:
<prompt>%</prompt> <userinput>telnet localhost 13</userinput>

Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
2001-06-19T21:04:42Z
Connection closed by foreign host.
<prompt>%</prompt>
<prompt>%</prompt> <userinput>telnet localhost 13</userinput>

Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
2001-06-19T21:04:42Z
Connection closed by foreign host.
<prompt>%</prompt>
<application>telnet</application> tried the new <acronym>IP</acronym>v6, and failed. It retried with <acronym>IP</acronym>v4 and succeeded. The daemon works. <application> telnet </application> tentou o novo <acronym>IP</acronym> v6 e falhou. Ele tentou novamente com <acronym>IP</acronym> v4 e foi bem-sucedido. O daemon funciona.
If you have access to another <trademark class="registered">UNIX</trademark> system via <application>telnet</application>, you can use it to test accessing the server remotely. My computer does not have a static <acronym>IP</acronym> address, so this is what I did: Se você tiver acesso a outro <trademark class="registered"> UNIX </trademark> sistema via <application> telnet </application> , você pode usá-lo para testar acessando o servidor remotamente. Meu computador não tem um endereço <acronym>IP</acronym> estático, então foi o que eu fiz:
<prompt>%</prompt> <userinput>who</userinput>

whizkid ttyp0 Jun 19 16:59 (216.127.220.143)
xxx ttyp1 Jun 19 16:06 (xx.xx.xx.xx)
<prompt>%</prompt> <userinput>telnet 216.127.220.143 13</userinput>

Trying 216.127.220.143...
Connected to r47.bfm.org.
Escape character is '^]'.
2001-06-19T21:31:11Z
Connection closed by foreign host.
<prompt>%</prompt>
<prompt>%</prompt> <userinput>who</userinput>

whizkid ttyp0 Jun 19 16:59 (216.127.220.143)
xxx ttyp1 Jun 19 16:06 (xx.xx.xx.xx)
<prompt>%</prompt> <userinput>telnet 216.127.220.143 13</userinput>

Trying 216.127.220.143...
Connected to r47.bfm.org.
Escape character is '^]'.
2001-06-19T21:31:11Z
Connection closed by foreign host.
<prompt>%</prompt>
Again, it worked. Will it work using the domain name? Mais uma vez, funcionou. Funcionará usando o nome de domínio?
<prompt>%</prompt> <userinput>telnet r47.bfm.org 13</userinput>

Trying 216.127.220.143...
Connected to r47.bfm.org.
Escape character is '^]'.
2001-06-19T21:31:40Z
Connection closed by foreign host.
<prompt>%</prompt>
<prompt>%</prompt> <userinput>telnet r47.bfm.org 13</userinput>

Trying 216.127.220.143...
Connected to r47.bfm.org.
Escape character is '^]'.
2001-06-19T21:31:40Z
Connection closed by foreign host.
<prompt>%</prompt>
By the way, <application>telnet</application> prints the <emphasis>Connection closed by foreign host</emphasis> message after our daemon has closed the socket. This shows us that, indeed, using <function>fclose(client);</function> in our code works as advertised. A propósito, <application> telnet </application> imprime o <emphasis> Conexão fechada por host estrangeiro </emphasis> mensagem depois que nosso daemon fechou o soquete. Isso nos mostra que, de fato, usando <function> fclose (cliente); </function> em nosso código funciona como anunciado.
Helper Functions Funções auxiliares
FreeBSD C library contains many helper functions for sockets programming. For example, in our sample client we hard coded the <systemitem class="fqdomainname">time.nist.gov</systemitem> <acronym>IP</acronym> address. But we do not always know the <acronym>IP</acronym> address. Even if we do, our software is more flexible if it allows the user to enter the <acronym>IP</acronym> address, or even the domain name. A biblioteca C do FreeBSD contém muitas funções auxiliares para a programação de sockets. Por exemplo, em nosso cliente de amostra nós codificamos <systemitem class="fqdomainname"> time.nist.gov </systemitem> Endereço <acronym>IP</acronym> . Mas nem sempre sabemos o endereço <acronym>IP</acronym> . Mesmo se o fizermos, nosso software será mais flexível se permitir que o usuário insira o endereço <acronym>IP</acronym> ou até mesmo o nome do domínio.
<function>gethostbyname</function> <function> gethostbyname </function>
While there is no way to pass the domain name directly to any of the sockets functions, the FreeBSD C library comes with the <citerefentry><refentrytitle>gethostbyname</refentrytitle><manvolnum>3</manvolnum></citerefentry> and <citerefentry><refentrytitle>gethostbyname2</refentrytitle><manvolnum>3</manvolnum></citerefentry> functions, declared in <filename>netdb.h</filename>. Enquanto não há como passar o nome de domínio diretamente para qualquer uma das funções dos sockets, a biblioteca C do FreeBSD vem com <citerefentry><refentrytitle> gethostbyname </refentrytitle><manvolnum> 3 </manvolnum></citerefentry> e <citerefentry><refentrytitle> gethostbyname2 </refentrytitle><manvolnum> 3 </manvolnum></citerefentry> funções declaradas em <filename> netdb.h </filename>
struct hostent * gethostbyname(const char *name);
struct hostent * gethostbyname2(const char *name, int af);

struct hostent * gethostbyname(const char *name);
struct hostent * gethostbyname2(const char *name, int af);
Both return a pointer to the <varname>hostent</varname> structure, with much information about the domain. For our purposes, the <varname>h_addr_list[0]</varname> field of the structure points at <varname>h_length</varname> bytes of the correct address, already stored in the <emphasis>network byte order</emphasis>. Ambos retornam um ponteiro para o <varname> hostent </varname> estrutura, com muita informação sobre o domínio. Para nossos propósitos, o <varname> h_addr_list [0] </varname> campo da estrutura aponta para <varname> h_length </varname> bytes do endereço correto, já armazenados no <emphasis> ordem de bytes de rede </emphasis>
This allows us to create a much more flexible—and much more useful—version of our <application>daytime</application> program: Isso nos permite criar uma versão muito mais flexível - e muito mais útil - do nosso <application> dia </application> programa:
/*
* daytime.c
*
* Programmed by G. Adam Stanislav
* 19 June 2001
*/
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;netdb.h&gt;

int main(int argc, char *argv[]) {
register int s;
register int bytes;
struct sockaddr_in sa;
struct hostent *he;
char buf[BUFSIZ+1];
char *host;

if ((s = socket(PF_INET, SOCK_STREAM, 0)) &lt; 0) {
perror("socket");
return 1;
}

bzero(&amp;sa, sizeof sa);

sa.sin_family = AF_INET;
sa.sin_port = htons(13);

host = (argc &gt; 1) ? (char *)argv[1] : "time.nist.gov";

if ((he = gethostbyname(host)) == NULL) {
herror(host);
return 2;
}

bcopy(he-&gt;h_addr_list[0],&amp;sa.sin_addr, he-&gt;h_length);

if (connect(s, (struct sockaddr *)&amp;sa, sizeof sa) &lt; 0) {
perror("connect");
return 3;
}

while ((bytes = read(s, buf, BUFSIZ)) &gt; 0)
write(1, buf, bytes);

close(s);
return 0;
}

/*
* daytime.c
*
* Programmed by G. Adam Stanislav
* 19 June 2001
*/
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;netdb.h&gt;

int main(int argc, char *argv[]) {
register int s;
register int bytes;
struct sockaddr_in sa;
struct hostent *he;
char buf[BUFSIZ+1];
char *host;

if ((s = socket(PF_INET, SOCK_STREAM, 0)) &lt; 0) {
perror("socket");
return 1;
}

bzero(&amp;sa, sizeof sa);

sa.sin_family = AF_INET;
sa.sin_port = htons(13);

host = (argc &gt; 1) ? (char *)argv[1] : "time.nist.gov";

if ((he = gethostbyname(host)) == NULL) {
herror(host);
return 2;
}

bcopy(he-&gt;h_addr_list[0],&amp;sa.sin_addr, he-&gt;h_length);

if (connect(s, (struct sockaddr *)&amp;sa, sizeof sa) &lt; 0) {
perror("connect");
return 3;
}

while ((bytes = read(s, buf, BUFSIZ)) &gt; 0)
write(1, buf, bytes);

close(s);
return 0;
}
We now can type a domain name (or an <acronym>IP</acronym> address, it works both ways) on the command line, and the program will try to connect to its <emphasis>daytime</emphasis> server. Otherwise, it will still default to <systemitem class="fqdomainname">time.nist.gov</systemitem>. However, even in this case we will use <function>gethostbyname</function> rather than hard coding <systemitem class="ipaddress">192.43.244.18</systemitem>. That way, even if its <acronym>IP</acronym> address changes in the future, we will still find it. Agora podemos digitar um nome de domínio (ou um endereço <acronym>IP</acronym> , funciona nos dois sentidos) na linha de comando, e o programa tentará se conectar à sua <emphasis> dia </emphasis> servidor. Caso contrário, ainda será padrão para <systemitem class="fqdomainname"> time.nist.gov </systemitem> . No entanto, mesmo neste caso, vamos usar <function> gethostbyname </function> em vez de codificação rígida <systemitem class="ipaddress"> 192.43.244.18 </systemitem> . Dessa forma, mesmo que seu endereço <acronym>IP</acronym> mude no futuro, ainda vamos encontrá-lo.
Since it takes virtually no time to get the time from your local server, you could run <application>daytime</application> twice in a row: First to get the time from <systemitem class="fqdomainname">time.nist.gov</systemitem>, the second time from your own system. You can then compare the results and see how exact your system clock is: Como não é preciso tempo para aproveitar o tempo do seu servidor local, você pode executar <application> dia </application> duas vezes seguidas: primeiro para obter o tempo de <systemitem class="fqdomainname"> time.nist.gov </systemitem> , a segunda vez do seu próprio sistema. Você pode então comparar os resultados e ver como o relógio do sistema é exato:
<prompt>%</prompt> <userinput>daytime ; daytime localhost</userinput>


52080 01-06-20 04:02:33 50 0 0 390.2 UTC(NIST) *
2001-06-20T04:02:35Z
<prompt>%</prompt>
<prompt>%</prompt> <userinput>daytime ; daytime localhost</userinput>


52080 01-06-20 04:02:33 50 0 0 390.2 UTC(NIST) *
2001-06-20T04:02:35Z
<prompt>%</prompt>
As you can see, my system was two seconds ahead of the <acronym>NIST</acronym> time. Como você pode ver, o meu sistema estava dois segundos adiantado do tempo do <acronym>NIST</acronym> .
<function>getservbyname</function> <function> getservbyname </function>
Sometimes you may not be sure what port a certain service uses. The <citerefentry><refentrytitle>getservbyname</refentrytitle><manvolnum>3</manvolnum></citerefentry> function, also declared in <filename>netdb.h</filename> comes in very handy in those cases: Às vezes você pode não ter certeza de qual porta um determinado serviço usa. <citerefentry><refentrytitle> getservbyname </refentrytitle><manvolnum> 3 </manvolnum></citerefentry> função, também declarada em <filename> netdb.h </filename> é muito útil nesses casos:
struct servent * getservbyname(const char *name, const char *proto);
struct servent * getservbyname(const char *name, const char *proto);
The <varname>servent</varname> structure contains the <varname>s_port</varname>, which contains the proper port, already in <emphasis>network byte order</emphasis>. O <varname> servo </varname> estrutura contém o <varname> esporte </varname> , que contém a porta adequada, já em <emphasis> ordem de bytes de rede </emphasis>
Had we not known the correct port for the <emphasis>daytime</emphasis> service, we could have found it this way: Se não tivéssemos conhecido a porta correta para o <emphasis> dia </emphasis> serviço, poderíamos ter encontrado desta forma:
struct servent *se;
...
if ((se = getservbyname("daytime", "tcp")) == NULL {
fprintf(stderr, "Cannot determine which port to use.\n");
return 7;
}
sa.sin_port = se-&gt;s_port;

struct servent *se;
...
if ((se = getservbyname("daytime", "tcp")) == NULL {
fprintf(stderr, "Cannot determine which port to use.\n");
return 7;
}
sa.sin_port = se-&gt;s_port;

Loading…

New source string a year ago
Browse all component changes

Things to check

Trailing space

Source and translation do not both end with a space

Fix string

Reset

Starting spaces

Source and translation do not both start with same number of spaces

Fix string

Reset

Glossary

English Portuguese (Brazil)
No related strings found in the glossary.

Source information

Source string comment
(itstool) path: sect2/title
Source string location
book.translate.xml:6396
String age
a year ago
Source string age
a year ago
Translation file
books/pt_BR/developers-handbook.po, string 1040