[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]<winsock2.h>[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]<ws2tcpip.h>[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]<stdio.h>[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]<stdio.h>[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]<string.h>[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]<winsock.h>[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]/*#include <sys/types.h>[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]#include <sys/socket.h>[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]#include <netdb.h>[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]#include <arpa/inet.h>*/[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]typedef[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]char[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] BYTE;[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]struct[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] a{[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ai_family, ai_socktype, ai_flags, ai_protocol, ai_addrlen, ai_addr;[/SIZE]
[SIZE=2]};[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]struct[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] b {[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] addr_size; [/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//tamanho da struct com as infos do cliente[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]};[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]struct[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] c {[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] their_addr; [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//informações do cliente que se vai ligar[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]};[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] main()[/COLOR][/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]struct[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] a hints, *res; [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//hints: setup da nossa ligação, *res: resposta para getaddrinfo()[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]struct[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] b socklen_t;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]struct[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] c sockaddr_storage;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] sockfd, new_fd; [/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//socket descriptor do server e do cliente a ligar-se[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]BYTE buffer[1024];[/SIZE]
[SIZE=2]memset( &hints, 0, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]sizeof[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](hints) );[/SIZE]
[SIZE=2]hints.ai_family = AF_UNSPEC; [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//diz-lhe para usar IPv4 ou IPv6, o que estiver disponivel[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]hints.ai_socktype = SOCK_STREAM; [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//quero streams TCP[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]hints.ai_flags = AI_PASSIVE; [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//diz ao socket para preencher o endereço IP automaticamente, vai ser localhost[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]getaddrinfo( NULL, 300, &hints, &res ); [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//NULL porque é servidor, 300 = porta, pode ser a que quiseres[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]//cria o socket usando as opçoes que definimos antes[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]sockfd = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol );[/SIZE]
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//faz bind desse socket ao endereço e porta passada anteriormente[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]bind(sockfd, hints.ai_addr, hints.ai_addrlen);[/SIZE]
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//fica a "ouvir" ligações, o 2º argumento é o numero maximo de ligações que queres aceitar[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]listen( sockfd, 1 );[/SIZE]
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//aceita a ligação[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]socklen_t.addr_size = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]sizeof[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](sockaddr_storage.their_addr);[/SIZE]
[SIZE=2]new_fd = accept(sockfd, &sockaddr_storage.their_addr, &socklen_t.addr_size);[/SIZE]
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//new_fd = accept(sockfd, (sockaddr_storage*)&sockaddr_storage.their_addr, &socklen_t.addr_size)[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]/*e pronto, a parte mais complicada já está, agora é so receber e enviar dados[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]1º argumento socket de onde se vai ler, 2º buffer com os dados, 3º tamanho maximo, 4º flags (pode ser 0)[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]atenção embora muitas implementações escondam isso (java, .net, etc) sockets so podem enviar/receber 1024 bytes de cada vez*/[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]recv( sockfd, (BYTE*)buffer, 1024, 0 );[/SIZE]
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//buffer agora tem os dados que quiseste, aquilo é cast de void* portanto pode ser qq tipo de dados que queiras.[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]printf([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"%c"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], buffer);[/SIZE]
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//fechar sockets e shutdown:[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]//close(sockfd);[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]//close(new_fd);[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]close(sockfd);[/SIZE]
[SIZE=2]close(new_fd);[/SIZE]
[SIZE=2]}[/SIZE]