BachelorThesis/demo.c
2022-05-20 12:25:09 +02:00

127 lines
3.1 KiB
C

/*
Author: Jozef Simko
School year: 3., Bachelor study, 2021/22
Study program: Computer Networks
Organization: Technical University of Kosice (TUKE), Faculty of Electrical Engineering and Informatics (FEI),
Compiler: Winlibs GCC -- MinGW-W64 x86_64-posix-seh version 11.2.0, built by Brecht Sanders (OS Win)
-- gcc version 9.3.0 (OS Linux)
compile: WIN -- gcc client.c -Wall -Wextra -lwsock32 -o client
LINUX -- gcc server.c -Wall -Wextra -o server
version: 1.2 , 15.5.2021
Usage: This demo demonstrates simple way of using Socket API and loading measurements from our server.
Results are compared with MAX and MIN value - be careful on used units.
*/
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Defines lenght of buffer for server's messages
It's better to use higher values, it's helpful in case of hardware/network overload */
#define REPLY_MSG_SIZE 2048
/* Defines public or local IP address of server in decimal form */
#define IP_ADDRESS "192.168.0.101"
/* Defines port used by server */
#define PORT 32500
/* MAX & MIN values are used to compare measurement result
Values should be adjusted to the used units (check server config file) */
#define MAX_VALUE 1265
#define MIN_VALUE 1200
/*
Closes socket (with dependency on used OS)
*/
void cleanUP(int sock){
#ifdef WIN32
closesocket(sock);
WSACleanup();
#else
close(sock);
#endif
return;
}
/* ====================================================== */
int main(){
int sock;
struct sockaddr_in server;
char server_reply[REPLY_MSG_SIZE];
#ifdef WIN32
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0){
printf("WSASturtup() failed %d\n", iResult);
return 1;
}
#endif
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
printf("socket() failed\n");
cleanUP(sock);
return 1;
}
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(IP_ADDRESS);
server.sin_port = htons(PORT);
if (connect(sock, (struct sockaddr *) &server, sizeof(server)) != 0){
printf("Cannot connect to server - check IP, port and server status!\n");
cleanUP(sock);
return 1;
}
if(recv(sock, server_reply, 2, 0) <= 0){
printf("Connection interrupted! Check server status!\n");
cleanUP(sock);
return 1;
}
while(1){
memset(server_reply, 0, REPLY_MSG_SIZE);
if(recv(sock, server_reply, REPLY_MSG_SIZE, 0) <= 0){
printf("Server crushed or disconnected!\n");
cleanUP(sock);
return 1;
}
if(strcmp(server_reply, "%") == 0){
break;
}
char *rest = server_reply;
for(int i = 0; i < 3; i++){
strtok_r(rest, ":", &rest);
}
double result = strtof(rest, NULL);
if(result > MAX_VALUE){
printf("Distance out of range - too great (%0.2f)\n", result);
}
if(result < MIN_VALUE){
printf("Distance out of range - too low (%0.2f)\n", result);
}
}
cleanUP(sock);
return 0;
}