Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement