Enviado em 20/06/2021 - 14:40h
Já revisei o código abaixo diversas vezes e eu ainda não encontrei um motivo claro para a função inet_ntop() falhar toda vez que é invocada pela primeira vez dentro do seu loop for.
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 9009
#define ADDR "127.0.0.1"
#define MAXCONN 5
int main(void) {
int sockfd;
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
fprintf(stderr, "socket() -> ERROR %d: %s\n", errno, strerror(errno));
exit(EXIT_FAILURE);
}
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
inet_pton(AF_INET, ADDR, &addr.sin_addr.s_addr);
if (bind(sockfd, (struct sockaddr*)&addr, sizeof(addr)) < 0 ) {
fprintf(stderr, "bind() -> ERROR %d: %s\n", errno, strerror(errno));
close(sockfd);
exit(EXIT_FAILURE);
}
if (listen(sockfd, SOMAXCONN) < 0 ) {
fprintf(stderr, "listen() -> ERROR %d: %s\n", errno, strerror(errno));
close(sockfd);
exit(EXIT_FAILURE);
}
for (size_t i = 0; i < MAXCONN; i++) {
struct sockaddr_in client;
socklen_t addrlen;
char caddr[INET_ADDRSTRLEN];
int new_sockfd;
memset(&client, 0, sizeof(client));
if ((new_sockfd = accept(sockfd, (struct sockaddr*)&client, &addrlen)) < 0 ) {
fprintf(stderr, "accept() -> ERROR %d: %s\n", errno, strerror(errno));
} else {
if (inet_ntop(client.sin_family, &client.sin_addr.s_addr, caddr, sizeof(caddr)) == NULL ) {
fprintf(stderr, "inet_ntop() -> ERROR %d: %s\n", errno, strerror(errno));
} else {
printf("%s\n", caddr);
}
const char msg[] = "What's up, [*****]. Names John. You here to help me?";
if (write(new_sockfd, msg, sizeof(msg)) < 0 ) {
fprintf(stderr, "write() -> ERROR %d: %s\n", errno, strerror(errno));
}
close(new_sockfd);
}
}
close(sockfd);
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 9009
#define ADDR "127.0.0.1"
int main(void) {
int sockfd;
struct sockaddr_in server;
memset(&server, 0, sizeof(server));
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
fprintf(stderr, "socket() -> ERROR %d: %s\n", errno, strerror(errno));
exit(EXIT_FAILURE);
}
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
inet_pton(AF_INET, ADDR, &server.sin_addr.s_addr);
if (connect(sockfd, (struct sockaddr*)&server, sizeof(server)) < 0 ) {
fprintf(stderr, "connect() -> ERROR %d: %s\n", errno, strerror(errno));
close(sockfd);
exit(EXIT_FAILURE);
} else {
char msg[256];
if (read(sockfd, msg, sizeof(msg)) < 0 ) {
fprintf(stderr, "read() -> ERROR %d: %s\n", errno, strerror(errno));
close(sockfd);
exit(EXIT_FAILURE);
}
printf("\nServer: %s\n\n", msg);
}
close(sockfd);
return EXIT_SUCCESS;
}
//Terminal para a execução do servidor
[AB2201@fedora TCP]$ gcc -Wall server.c -o server
[AB2201@fedora TCP]$ ./server
inet_ntop() -> ERROR 97: Address family not supported by protocol
127.0.0.1
127.0.0.1
127.0.0.1
127.0.0.1
[AB2201@fedora TCP]$
//Terminal para a execução do cliente
[AB2201@fedora TCP]$ gcc -Wall client.c -o client
[AB2201@fedora TCP]$ ./client
Server: What's up, [*****]. Names John. You here to help me?
[AB2201@fedora TCP]$ ./client
Server: What's up, [*****]. Names John. You here to help me?
[AB2201@fedora TCP]$ ./client
Server: What's up, [*****]. Names John. You here to help me?
[AB2201@fedora TCP]$ ./client
Server: What's up, [*****]. Names John. You here to help me?
[AB2201@fedora TCP]$ ./client
Server: What's up, [*****]. Names John. You here to help me?
[AB2201@fedora TCP]$
//Terminal para a execução servidor
[AB2201@fedora TCP]$ ./server
inet_ntop() -> ERROR 97: Address family not supported by protocol
127.0.0.1
127.0.0.1
127.0.0.1
127.0.0.1
[AB2201@fedora TCP]$
//Terminal para a execução do netcat (nc)
[AB2201@fedora TCP]$ nc localhost 9009
What's up, [*****]. Names John. You here to help me?^C
[AB2201@fedora TCP]$ nc localhost 9009
What's up, [*****]. Names John. You here to help me?^C
[AB2201@fedora TCP]$ nc localhost 9009
What's up, [*****]. Names John. You here to help me?^C
[AB2201@fedora TCP]$ nc localhost 9009
What's up, [*****]. Names John. You here to help me?^C
[AB2201@fedora TCP]$ nc localhost 9009
What's up, [*****]. Names John. You here to help me?^C
[AB2201@fedora TCP]$
if (inet_ntop(AF_INET, &client.sin_addr.s_addr, caddr, sizeof(caddr)) == NULL ) { //Troquei client.sin_family por AF_INET
fprintf(stderr, "inet_ntop() -> ERROR %d: %s\n", errno, strerror(errno));
} else {
printf("%s\n", caddr);
}
[AB2201@fedora TCP]$ gcc -Wall server.c -o server
[AB2201@fedora TCP]$ ./server
0.0.0.0
127.0.0.1
127.0.0.1
127.0.0.1
127.0.0.1
[AB2201@fedora TCP]$