-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
69 lines (56 loc) · 1.92 KB
/
client.c
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
/* client code of a half duplex chat program */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#define MYPORT 4950
#define MAXBUFLEN 100
int main(int argc, char *argv[])
{
int sockfd;
struct sockaddr_in their_addr;
struct hostent *he;
int numbytes, addr_len;
char buf[MAXBUFLEN];
char arr[MAXBUFLEN];
if (argc != 2) {
fprintf(stderr, "usage: talker hostname message\n");
exit(1);
}
if ((he = gethostbyname(argv[1])) == NULL) {
perror("gethostbyname");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(MYPORT);
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(&(their_addr.sin_zero), '\0', 8);
while(1) {
printf("Type the message: \n");
gets(arr);
if ((numbytes = sendto(sockfd, arr, strlen(arr), 0,
(struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) {
perror("sendto");
exit(1);
}
printf("send %d bytes to %s\n", numbytes, inet_ntoa(their_addr.sin_addr));
if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1, 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recv from server");
exit(1);
}
buf[numbytes] = '\0';
printf("message: %s\n", buf);
}
close(sockfd);
return 0;
}