getpeername
Diferenças
Aqui você vê as diferenças entre duas revisões dessa página.
— | getpeername [2023/09/12 16:14] (atual) – criada - edição externa 127.0.0.1 | ||
---|---|---|---|
Linha 1: | Linha 1: | ||
+ | ====== Função getpeername ====== | ||
+ | Sintaxe: | ||
+ | |||
+ | ''# | ||
+ | |||
+ | |||
+ | '' | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | |||
+ | |||
+ | A função '' | ||
+ | |||
+ | |||
+ | Veja o exemplo: | ||
+ | |||
+ | |||
+ | <code c> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | int iSock; | ||
+ | | ||
+ | |||
+ | iSock = socket(AF_INET, | ||
+ | if( iSock == -1) | ||
+ | { | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | if( bind(iSock, (struct sockaddr *)& | ||
+ | { | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | if( listen( iSock, 10 ) < 0) | ||
+ | { | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | | ||
+ | { | ||
+ | int iFd; | ||
+ | struct sockaddr_in client_addr; | ||
+ | struct sockaddr_in client_peer; | ||
+ | socklen_t sin_size; | ||
+ | socklen_t len_peer; | ||
+ | int port; | ||
+ | char ipstr[INET_ADDRSTRLEN]; | ||
+ | |||
+ | sin_size = sizeof(struct sockaddr_in); | ||
+ | |||
+ | if( (iFd = accept(iSock, | ||
+ | { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | printf(" | ||
+ | |||
+ | |||
+ | len_peer = sizeof(client_peer); | ||
+ | |||
+ | getpeername(iFd, | ||
+ | |||
+ | port = ntohs(client_peer.sin_port); | ||
+ | inet_ntop(AF_INET, | ||
+ | |||
+ | printf(" | ||
+ | printf(" | ||
+ | close(iFd); | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | Programa que só se conecta no servidor e já finaliza. | ||
+ | <code c> | ||
+ | /* teste_getpeername.c */ | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | int iSock; | ||
+ | int iBytes; | ||
+ | | ||
+ | char buffer[100]; | ||
+ | |||
+ | iSock = socket(AF_INET, | ||
+ | if( iSock == -1) | ||
+ | { | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | if( connect(iSock, | ||
+ | { | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | --- // |