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)
Aprenda a Gerenciar Permissões de Arquivos no Linux
Como transformar um áudio em vídeo com efeito de forma de onda (wave form)
Como aprovar Pull Requests em seu repositório Github via linha de comando
Dando - teoricamente - um gás no Gnome-Shell do Arch Linux
Como instalar o Google Cloud CLI no Ubuntu/Debian
Mantenha seu Sistema Leve e Rápido com a Limpeza do APT!
Procurando vídeos de YouTube pelo terminal e assistindo via mpv (2025)
Zorin OS - Virtual Box não consigo abrir maquinas virtuais (1)
O que você está ouvindo agora? [2] (180)