1 /* UDP client in the internet domain */
3 #include <sys/socket.h>
4 #include <netinet/in.h>
12 void error(const char *);
13 int main(int argc
, char *argv
[])
17 struct sockaddr_in server
, from
;
21 if (argc
!= 3) { printf("Usage: server port\n");
24 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
25 if (sock
< 0) error("socket");
27 server
.sin_family
= AF_INET
;
28 hp
= gethostbyname(argv
[1]);
29 if (hp
==0) error("Unknown host");
31 bcopy((char *)hp
->h_addr
,
32 (char *)&server
.sin_addr
,
34 server
.sin_port
= htons(atoi(argv
[2]));
35 length
=sizeof(struct sockaddr_in
);
36 printf("Please enter the message: ");
39 fgets(buffer
,255,stdin
);
41 strlen(buffer
),0,(const struct sockaddr
*)&server
,length
);
42 if (n
< 0) error("Sendto");
43 n
= recvfrom(sock
,buffer
,256,0,(struct sockaddr
*)&from
, &length
);
44 if (n
< 0) error("recvfrom");
45 write(1,"Got an ack: ",12);
52 void error(const char *msg
)