forked from rovinbhandari/RTP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_udp.c
104 lines (77 loc) · 3.01 KB
/
client_udp.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "client_udp.h"
int main(int argc, char* argv[])
{
if(argc != 2)
er("incorrect usage", -1);
// BEGIN: initialization
struct sockaddr_in sin_server;
int socket_fd, x; // socket_fd: socket file descriptor for the client to connect to. x: stores error code of system calls.
size_t size_sockaddr = sizeof(struct sockaddr), size_packet = sizeof(struct packet);
short int connection_id; // temporary storage for the connection id of the client-server communication.
struct packet* chp = (struct packet*) malloc(size_packet); // chp: client host packet
struct packet* data; // data: network packet
if((x = socket_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) // intializes the socket file descriptor for client-server communication (client side).
er("socket()", x);
memset((char*) &sin_server, 0, sizeof(struct sockaddr_in)); // clearing out the struct before initalization.
sin_server.sin_family = AF_INET;
sin_server.sin_port = htons(PORTSERVER);
if(!(x = inet_aton(IPSERVER, &sin_server.sin_addr)))
er("inet_aton()", x);
printf(ID "UDP Client started up. Attempting communication with server @ %s:%d...\n\n", IPSERVER, PORTSERVER);
// END: initialization
// BEGIN: request
set0(chp); // clearing out the struct before initalization.
chp->type = REQU;
chp->conid = -1;
strcpy(chp->buffer, argv[1]);
printpacket(chp, HP);
printf(ID "Requesting Server for timestamp of: %s ...\n", chp->buffer);
fflush(stdout);
data = htonp(chp);
if((x = sendto(socket_fd, data, size_packet, 0, (struct sockaddr*) &sin_server, size_sockaddr)) == -1)
er("request sendto()", x);
// END: request
// BEGIN: request acknowledgement
set0(data); // clearing out the struct before initalization.
if((x = recvfrom(socket_fd, data, size_packet, 0, (struct sockaddr*) &sin_server, &size_sockaddr)) == -1)
er("request acknowledgement recvfrom()", x);
chp = ntohp(data);
printpacket(chp, HP);
connection_id = chp->conid;
// do error checking here...
// ...
printf(ID "Reply from Server: %s\n", chp->buffer);
fflush(stdout);
// END: request acknowledgement
// BEGIN: done
set0(data); // clearing out the struct before initalization.
if((x = recvfrom(socket_fd, data, size_packet, 0, (struct sockaddr*) &sin_server, &size_sockaddr)) == -1)
er("done recvfrom()", x);
chp = ntohp(data);
printpacket(chp, HP);
// do error checking here...
// ...
printf(ID "Reply from Server: %s", chp->buffer);
fflush(stdout);
// END: done
// BEGIN: done acknowledgement
set0(chp); // clearing out the struct before initalization.
chp->type = DACK;
chp->conid = connection_id;
strcpy(chp->buffer, "Thanks!");
printpacket(chp, HP);
data = htonp(chp);
if((x = sendto(socket_fd, data, size_packet, 0, (struct sockaddr*) &sin_server, size_sockaddr)) == -1)
er("done acknowledgement sendto()", x);
printf(ID "Bye!\n");
fflush(stdout);
// END: done acknowledgement
// BEGIN: cleanup
free(chp);
free(data);
close(socket_fd);
printf(ID "Done.\n");
fflush(stdout);
// END: cleanup
return 0;
}