Skip to content

Instantly share code, notes, and snippets.

@vedgar
Created October 27, 2016 07:34
Show Gist options
  • Save vedgar/3dfec1ce8bbc0818360e385c730ce262 to your computer and use it in GitHub Desktop.
Save vedgar/3dfec1ce8bbc0818360e385c730ce262 to your computer and use it in GitHub Desktop.
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
int main(void){
struct hostent *hostinfo = gethostbyname("time-a.nist.gov");
if(!hostinfo) herror("gethostbyname");
struct in_addr binIP = *((struct in_addr*) hostinfo->h_addr);
printf("IP adresa: %s\n", inet_ntoa(binIP));
int sok = socket(PF_INET, SOCK_STREAM, 0);
if(sok == -1){perror("socket"); return 2;}
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(13);
server.sin_addr = binIP;
memset(server.sin_zero, 0, 8);
if(connect(sok, (struct sockaddr*)&server, sizeof(server)) == -1){
perror("connect");
return 1;
}
char buffer[200];
int primljeno = 0;
while(1){
int novoprimljeno = recv(sok, buffer + primljeno,
sizeof(buffer) - primljeno - 1, 0);
if(novoprimljeno == 0) break;
else if(novoprimljeno == -1) perror("recv");
else primljeno += novoprimljeno;
}
buffer[primljeno] = 0;
printf("%s\n", buffer);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment