-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_udp.c
More file actions
60 lines (50 loc) · 1.13 KB
/
client_udp.c
File metadata and controls
60 lines (50 loc) · 1.13 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
#include "common.h"
int main(int argc, char * argv[])
{
FILE *fp;
struct hostent *hp;
struct sockaddr_in sin;
char *host;
char *fname;
char buf[MAX_LINE];
int s;
int slen;
if (argc==3) {
host = argv[1];
fname= argv[2];
}
else {
fprintf(stderr, "Usage: ./client_udp host filename\n");
exit(1);
}
/* translate host name into peer’s IP address */
if (!hp) {
fprintf(stderr, "Unknown host: %s\n", host);
exit(1);
}
fp = fopen(fname, "r");
if (fp==NULL){
fprintf(stderr, "Can't open file: %s\n", fname);
exit(1);
}
hp = gethostbyname(host);
/* build address data structure */
bzero((char *)&sin, sizeof(sin));
sin.sin_family = AF_INET;
bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
sin.sin_port = htons(SERVER_PORT);
/* active open */
if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
perror("Socket");
exit(1);
}
socklen_t sock_len= sizeof sin;
printf("Sending file\r\n");
SendFile(fp,s,&sin);
*buf = 0x02;
if(sendto(s, buf, 1, 0, (struct sockaddr *)&sin, sock_len)<0){
perror("SendTo Error\n");
exit(1);
}
fclose(fp);
}