This repository was archived by the owner on Feb 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathudpclient.c
More file actions
57 lines (47 loc) · 1.27 KB
/
udpclient.c
File metadata and controls
57 lines (47 loc) · 1.27 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
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#define SERV_PORT 20001
#define BUFSIZE 1024
#define SADDR struct sockaddr
#define SLEN sizeof(struct sockaddr_in)
int main(int argc, char **argv) {
int sockfd, n;
char sendline[BUFSIZE], recvline[BUFSIZE + 1];
struct sockaddr_in servaddr;
struct sockaddr_in cliaddr;
if (argc != 2) {
printf("usage: client <IPaddress of server>\n");
exit(1);
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);
if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) < 0) {
perror("inet_pton problem");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket problem");
exit(1);
}
write(1, "Enter string\n", 13);
while ((n = read(0, sendline, BUFSIZE)) > 0) {
if (sendto(sockfd, sendline, n, 0, (SADDR *)&servaddr, SLEN) == -1) {
perror("sendto problem");
exit(1);
}
if (recvfrom(sockfd, recvline, BUFSIZE, 0, NULL, NULL) == -1) {
perror("recvfrom problem");
exit(1);
}
printf("REPLY FROM SERVER= %s\n", recvline);
}
close(sockfd);
}