-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUDP-client.c
150 lines (121 loc) · 4.54 KB
/
UDP-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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#ifdef _WIN32
#define _WIN32_WINNT _WIN32_WINNT_WIN7 // select minimal legacy support, needed for inet_pton, inet_ntop
#include <winsock2.h> //for all socket programming
#include <ws2tcpip.h> //for getaddrinfo, inet_pton, inet_ntop
#include <stdio.h> //for fprintf
#include <unistd.h> //for close
#include <stdlib.h> //for exit
#include <string.h> //for memset
#include <stdint.h> //for uint16_t
void OSInit(void)
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 0), &wsaData);
}
void OSCleanup(void)
{
WSACleanup();
}
#else
#include <sys/socket.h> //for sockaddr, socket, socket
#include <sys/types.h> //for size_t
#include <netdb.h> //for getaddrinfo
#include <netinet/in.h> //for sockaddr_in
#include <arpa/inet.h> //for htons, htonl, inet_pton, inet_ntop
#include <errno.h> //for errno
#include <stdio.h> //for fprintf, perror
#include <unistd.h> //for close
#include <stdlib.h> //for exit
#include <string.h> //for memset
void OSInit(void) {}
void OSCleanup(void) {}
#endif
int initialization(struct sockaddr **internet_address, socklen_t *internet_address_length);
void execution(int internet_socket, struct sockaddr *internet_address, socklen_t internet_address_length);
void cleanup(int internet_socket, struct sockaddr *internet_address);
int biggestNumber(int *numbers, int size);
int main(int argc, char *argv[])
{
OSInit();
struct sockaddr *internet_address = NULL;
socklen_t internet_address_length = 0;
int internet_socket = initialization(&internet_address, &internet_address_length);
execution(internet_socket, internet_address, internet_address_length);
cleanup(internet_socket, internet_address);
OSCleanup();
return 0;
}
int initialization(struct sockaddr **internet_address, socklen_t *internet_address_length)
{
struct addrinfo internet_address_setup;
struct addrinfo *internet_address_result;
memset(&internet_address_setup, 0, sizeof internet_address_setup);
internet_address_setup.ai_family = AF_UNSPEC;
internet_address_setup.ai_socktype = SOCK_DGRAM;
getaddrinfo("::1", "24042", &internet_address_setup, &internet_address_result);
int internet_socket;
internet_socket = socket(internet_address_result->ai_family, internet_address_result->ai_socktype, internet_address_result->ai_protocol);
*internet_address_length = internet_address_result->ai_addrlen;
*internet_address = (struct sockaddr *)malloc(internet_address_result->ai_addrlen);
memcpy(*internet_address, internet_address_result->ai_addr, internet_address_result->ai_addrlen);
freeaddrinfo(internet_address_result);
return internet_socket;
}
void execution(int internet_socket, struct sockaddr *internet_address, socklen_t internet_address_length)
{
while(1){
int received_numbers[3];
char goMessage[] = "GO";
ssize_t number_of_bytes_received = 0;
//Send "GO" message
sendto(internet_socket, goMessage, sizeof(goMessage), 0, internet_address, internet_address_length);
int received_number = 0;
char buffer[1000];
int numbers[3];
//Receive 3 integers
for (int i = 0; i < 3; i++)
{
number_of_bytes_received = recvfrom(internet_socket,(char*) &buffer, sizeof(received_number), 0, internet_address, &internet_address_length);
if(number_of_bytes_received == -1){
perror("Error receiving data");
exit(1);
}
if (strcmp(buffer, "STOP") == 0)
{
printf("STOP RECEIVED");
i = 2;
break;
} else {
buffer[number_of_bytes_received] = '\0';
received_number = ntohs(*(uint16_t *)buffer); //convert from network byte order
numbers[i] = received_number;
printf("Received number[%d]: %d\n",i,received_number);
}
}
uint16_t biggest_num = (uint16_t)biggestNumber(numbers, 3);
printf("The biggest number is: %d\n", biggest_num); //debug print
printf("--------------------------\n");
//send biggest number to server
uint16_t network_biggest = htons(biggest_num);
sendto(internet_socket, (const char *) &network_biggest, sizeof(network_biggest), 0, internet_address, internet_address_length);
}
printf("--------------------\nProgram closed\n");
}
int biggestNumber(int *numbers, int size)
{ //bubble sort
for (int i = 0; i < size-1; i++) {
for (int j = 0; j < size-i-1; j++) {
if (numbers[j] > numbers[j+1]) {
int temp = numbers[j];
numbers[j] = numbers[j+1];
numbers[j+1] = temp;
}
}
}
return numbers[size-1];
}
void cleanup(int internet_socket, struct sockaddr *internet_address)
{
free(internet_address);
close(internet_socket);
}