
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)
Gentoo: detectando impressoras de rede e como fixar uma impressora por IP
Como o GNOME conseguiu o feito de ser preterido por outras interfaces gráficas
Gentoo binário em 2026: UEFI, LUKS, Btrfs e Systemd
Trabalhando Nativamente com Logs no Linux
Jogando Daikatana (Steam) com Patch 1.3 via Luxtorpeda no Linux
Por que sua empresa precisa de uma PKI (e como automatizar EMISSÕES de certificados via Web API)
Instalando NoMachine no Gentoo com Systemd (acesso Remoto em LAN)
Gentoo: Trocando wpa_supplicant pelo iwd no NetworkManager (Systemd)
O que houve com slackware ??? (12)
Alterar conteúdo de dica [RESOLVIDO] (3)
Vou destruir sua infância:) (5)









