-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmid
More file actions
72 lines (55 loc) · 1.5 KB
/
mid
File metadata and controls
72 lines (55 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
Script started on 2020년 10월 22일 (목) 오전 09시 36분 25초
[?1034hnetpa15@~/ 567$ catt[K prog.[K/mid_daytime.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <errno.h>
#define MAXLINE 511
int main(int argc, char *argv[]){
struct sockaddr_in servaddr;
int s,nbyte,addrlen=sizeof(servaddr);
int i;
char buf[MAXLINE+1];
struct hostent *hp;
struct in_addr in;
char buf1[MAXLINE+1];
if (argc !=3){
printf("Usage: %s hostname\n",argv[0]);
exit(0);
}
if((s=socket(PF_INET,SOCK_DGRAM,0))<0){
perror("socket fail");
exit(1);
}
hp=gethostbyname(argv[1]);
bzero((char*)&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
for(i=0; hp->h_addr_list[i]; i++){
memcpy(&in.s_addr,hp->h_addr_list[i],sizeof(in.s_addr));
inet_ntop(AF_INET, &in,buf1,sizeof(buf));
}
inet_pton(AF_INET,buf1,&servaddr.sin_addr);
servaddr.sin_port=htons(atoi(argv[2]));
if(sendto(s,buf,strlen(buf),0,(struct sockaddr *)&servaddr,addrlen)<0){
perror("sendto fail");
exit(0);
}
if((nbyte=recvfrom(s,buf,MAXLINE,0,(struct sockaddr*)&servaddr, &addrlen))<0){
perror("recevfrom fail");
exit(0);
}
buf[nbyte]=0;
printf("%s",buf);
close(s);
return 0;
}
netpa15@~/ 568$ prof[Kg/mid_daytime css.skuniv.ac.kr 9999
Thu Oct 22 09:36:58 2020
netpa15@~/ 569$ exit
Script done on 2020년 10월 22일 (목) 오전 09시 37분 03초