Enviado em 19/06/2021 - 22:56h
Recentemente comecei a estudar a API de sockets do Unix (Berkeley sockets) e acabei encontrando uma função bastante interessante e comumente utilizada em conexões half-duplex e full-duplex: a shutdown().int shutdown(int sockfd, int how);
#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 size;
char caddr[INET_ADDRSTRLEN];
int new_sockfd;
memset(&client, 0, sizeof(client));
if ((new_sockfd = accept(sockfd, (struct sockaddr*)&client, &size)) < 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, _Bitch. Names John. You here to help me?\n";
shutdown(new_sockfd, SHUT_WR); //Usando new_sockfd o programa para, já o mesmo não acontece com o descritor sockfd
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;
}
Atenção a quem posta conteúdo de dicas, scripts e tal (2)
Criando um gateway de internet com o Debian
Configuração básica do Conky para mostrar informações sobre a sua máquina no Desktop
Aprenda a criar músicas com Inteligência Artificial usando Suno AI
Entendendo o que é URI, URL, URN e conhecendo as diferenças entre POST e GET
Ativando o Modo Noturno via Linha de Comando no GNOME/Wayland
Preparando pendrive com GNU/Linux [Corretamente!]
Instalando Google Chrome no Fedora 40
Habilitando a importação de senhas no Firefox
Como corrigir o erro do VirtualBox travar a máquina virtual em tela cheia
Notebuxo positivo vaio VJFE43F11X Something has gone seriously wrong... (6)
Acesso remoto via SSH por Script numa virtualbox (0)
Contador de impressão - Plugin (0)
Problema ao configurar Zabbix no ubuntu (13)
Como faço pra um script ser executado apenas abrindo um terminal e dan... (8)