Enviado em 05/11/2018 - 23:04h
Olá, boa madrugada!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
int make_server_socket(void){
int sockfd;
struct addrinfo *res=NULL, hints;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_flags=AI_PASSIVE;
hints.ai_family=AF_INET6;
hints.ai_socktype=SOCK_STREAM;
hints.ai_protocol=IPPROTO_TCP;
if(getaddrinfo(NULL, "9009", &hints, &res)!=0){
printf("\n* getaddrinfo() -> failed\n");
exit(1);
}else{
struct addrinfo *it;
for(it=res; it!=NULL; it=it->ai_next){
if(it->ai_family==AF_INET || it->ai_family==AF_INET6){
sockfd=socket(it->ai_family, SOCK_STREAM, IPPROTO_TCP);
if(sockfd<0){
continue;
}else{
if(bind(sockfd, it->ai_addr, it->ai_addrlen)==0){
break;
}
}
close(sockfd);
}
}
freeaddrinfo(res);
}
return sockfd;
}
void show_addr(const struct sockaddr *addr){
char strAddr[64];
if(addr->sa_family==AF_INET){
struct sockaddr_in *addr4=(struct sockaddr_in*)addr;
inet_ntop(AF_INET, &addr4->sin_addr.s_addr,
strAddr, INET_ADDRSTRLEN);
printf("[ipv4 addr] -> %s\n", strAddr);
}else if(addr->sa_family==AF_INET6){
struct sockaddr_in6 *addr6=(struct sockaddr_in6*)addr;
inet_ntop(AF_INET6, addr6->sin6_addr.s6_addr,
strAddr, INET6_ADDRSTRLEN);
printf("[ipv6 addr] -> %s\n", strAddr);
}else{
printf("\n* Unknown address family\n"); //essa parte não faz
//muito sentido!
}
}
int main(void){
int sockfd=make_server_socket();
if(sockfd<0){
printf("\n* make_server_socket() -> failed\n");
exit(1);
}
if(listen(sockfd, 10)<0){
printf("\n* listen() -> failed\n");
exit(1);
}
size_t max;
int csockfd;
struct sockaddr addr;
socklen_t socklen=sizeof(struct sockaddr);
printf("Enter the maximum number of connections >");
scanf("%ld", &max);
for(size_t i=0; i<max; i++){
csockfd=accept(sockfd, &addr, &socklen);
if(csockfd!=-1){
show_addr(&addr);
send(csockfd, "Bonjour!\0", 8, 0);
close(csockfd);
}
}
close(csockfd);
return 0;
}
zherkezhi@zherkezhi:~/Documents/C$ ./server
Enter the maximum number of connections >5
[ipv4 addr] -> 127.0.0.1
[ipv4 addr] -> 127.0.0.1
[ipv4 addr] -> 127.0.0.1
[ipv4 addr] -> 127.0.0.1
[ipv4 addr] -> 127.0.0.1
zherkezhi@zherkezhi:~/Documents/C$ ./server
Enter the maximum number of connections >5
[ipv6 addr] -> ::8064:a4fb:fe7f:0
[ipv6 addr] -> ::1
[ipv6 addr] -> ::1
[ipv6 addr] -> ::1
[ipv6 addr] -> ::1
*** stack smashing detected ***: <unknown> terminated
Aborted (core dumped)
Como gerar qualquer emoji ou símbolo unicode a partir do seu teclado
Instalar e Configurar o Slackware Linux em 2025
Como configurar os repositórios do apt no Debian 12 em 2025
Passkeys: A Evolução da Autenticação Digital
Instalação de distro Linux em computadores, netbooks, etc, em rede com o Clonezilla
Muitas dificuldades ao instalar distro Linux em Notebook Sony Vaio PCG-6131L (VPCEA24FM)
Slackpkg+ (Slackpkg Plus) está de volta!
Como dividir duas janelas igualmente e lado-a-lado na sua tela
Configurando o Conky para iniciar corretamente no sistema
3 configurações básicas que podem melhorar muito a sua edição pelo editor nano
Minha rede tem um espaço invisível que não dá pra digitar o nome da re... (1)
Pedagogia no brasil não passa de alfabetização por m4sturbação mental ... (2)
compilação samba 4.22 rock linux 9.5 (3)
Erro de segmentação «Segmentation fault (core dumped)» ao retornar obj... (7)