I'm currently working with some gps tracking devices for a college project but I need to setup a local server to send data to (I can define to which IP address and PORT the data is sent to).
Since the data sent from the device is a string with multiple bytes written on binary code (it contains information about geographical localization, temperature and humidity values, etc.), I'm looking to actually see the whole string (my idea would be to store it on a notepad so I could manually see how the string looks like) as well as decode the whole string (slicing it into little information packages: "first part" corresponds to coordinates; "from here to there" corresponds to the value of temperature sensor 1; etc.).
Since all of this takes time and multiple codifications, I would like to first understand how to setup a local server (or if thats even possible) in order to simply see the string sent by my "client".
My knowledge in client/server communication is basic, I understand the base concept and I'm reasonable at coding sockets (which I probably need to use to establish my connection, server side).
Any help would be appreciated!
EDIT :
Client
// Client side implementation of UDP client-server model
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define PORT 8080
// Driver code
int main() {
int sockfd;
// Creating socket file descriptor
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
// Filling server information
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr = INADDR_ANY;
// Sending message to server
sendto(sockfd, "Hello", 5, 0, (const struct sockaddr *) &servaddr, sizeof(servaddr));
printf("Message sent.\n");
close(sockfd);
return 0;
}
Server
// Server side implementation of UDP client-server model
#define PORT 8080
#define MAXLINE 1024
// Driver code
int main() {
int sockfd, len;
// Creating socket file descriptor
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}
struct sockaddr_in servaddr, cliaddr;
memset(&servaddr, 0, sizeof(servaddr));
memset(&cliaddr, 0, sizeof(cliaddr));
// Filling server information
servaddr.sin_family = AF_INET; // IPv4
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(PORT);
// Bind the socket with the server address
if ( bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0 ) {
perror("bind failed");
exit(EXIT_FAILURE);
}
char buffer[MAXLINE];
len = sizeof(cliaddr);
int n = recvfrom(sockfd, buffer, sizeof(buffer) -1, 0, ( struct sockaddr *) &cliaddr, &len);
if (n < 0)
perror("rcvfrom failed");
buffer[n] = '\0';
printf("%d bytes sent by client: %s\n", n, buffer);
return 0;
}