Enviado em 25/10/2018 - 21:52h
Olá, boa noite a todos!
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT "9009"
int make_server_socket(void){
int sockfd;
struct addrinfo *res=NULL, hints;
memset(&hints, 0, sizeof(hints));
hints.ai_flags=AI_PASSIVE;
hints.ai_family=AF_UNSPEC;
hints.ai_socktype=SOCK_STREAM;
hints.ai_protocol=IPPROTO_TCP;
if(getaddrinfo(NULL, PORT, &hints, &res)!=0){
sockfd=-1;
}else{
if((sockfd=socket(res->ai_family, SOCK_STREAM, IPPROTO_TCP))!=-1){
if(bind(sockfd, res->ai_addr, res->ai_addrlen)<0){
sockfd=-1;
}
}
freeaddrinfo(res);
}
return sockfd;
}
int main(void){
int sockfd=make_server_socket();
if(sockfd!=-1){
listen(sockfd, 1);
int csockfd=accept(sockfd, NULL, NULL);
if(csockfd!=-1){
send(csockfd, "Real Muthaphuckkin G's", 23, 0);
}
close(sockfd);
close(csockfd);
}
return 0;
}
zherkezhi@zherkezhi :~/Documents/C/Perguntas$ nc -4 127.0.0.1 9009
Real Muthaphuckkin G's
zherkezhi@zherkezhi:~/Documents/C/Perguntas$ #Funcionou perfeitamente!
zherkezhi@zherkezhi:~/Documents/C/Perguntas$ nc -6 ::1 9009
zherkezhi@zherkezhi:~/Documents/C/Perguntas$ #não aconteceu nada *_*
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
Visualizar arquivos em formato markdown (ex.: README.md) pelo terminal
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)
Alguém já usou o framework Avalonia para desenvolver interfaces de usu... (2)
Gestão de Ambiente em uma rede Linux (3)