0

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; 
} 
  • Step 1 is to decide whether to use UDP or TCP. What do the devices actually use? – user3386109 Aug 10 '20 at 19:06
  • @user3386109 it uses either UDP or TCP. I was told to use UDP (since I'll later implement this on a real company, UDP is used since it lowers the general costs of internet bandwidth, etc.). However, if using TCP would simplify this (by a lot) I would prefer using it since its not a MUST to use UDP. – joaoasilva Aug 10 '20 at 19:12
  • [Here's an example of a simple UDP server](https://stackoverflow.com/questions/35568996/socket-programming-udp-client-server-in-c/35570418#35570418). Run it as is on a single computer. Then figure out how to run the server and client on two separate computers (i.e. the client code needs the correct IP address for the server). Finally, try it with your GPS device. The GPS device needs to be told the IP address of the server, and there might be some protocol that you need to follow to get the GPS device to start sending. – user3386109 Aug 10 '20 at 19:18
  • Using UDP on a congested internet connection may actually be more costly than TCP. TCP has flow control UDP doesn't. It may therefore be considered rude to send UDP data on a network shared by other users without implementing some sort of flow control. You should allso be prepared to handle missing and double packets, and packets out of order. As a rule of thumb use TCP if you are new to network programming. – HAL9000 Aug 11 '20 at 07:25
  • @HAL9000 I understand all the "theoretical things" that come with TCP vs UDP. With this said (and from what I asked on my internship company), they use UDP since besides lowering the overall costs, is enough for "what they want" (it's alright if information is missed since the GPS sends information every X minutes). The worst that can happen would be to not refresh its localization or each temperature value, etc.. I guess I would also prefer using TCP (since it's safer obviously) but it's not really my choice. I can always create a UDP client/server code and think about building a TCP later! – joaoasilva Aug 11 '20 at 18:49
  • My TCP is a little bit rusty, but if i remember correctly, it ends up sending a packet every 3 seconds anyhow. So if you only send a packet every minute, then UDP should be OK, (assuming you can live with its faultiness) – HAL9000 Aug 11 '20 at 19:07
  • @HAL9000 Yea! Like I said, the information is coordinates / temperature values / fuel values, etc. Of course you would like to have 100% accuracy on the info (without faulty info transmission / packets missing) but it's fine if from time to time those parameters don't refresh! – joaoasilva Aug 11 '20 at 19:28

1 Answers1

0

1 - Google for a simple TCP or UDP server-client application you will find plenty of them,

2 - Get your server running first, then use something like telnet to connect to it, send your first message to your server. (probably use a sample message that your client will send). Try sending messages from another device while it is in the same network and it is not in the same network,

3 - Reshape your server as to parse and evaluate the messages it will receive (NMEA sentences or whatever it receives)

4 - Then go all the way back to the example code and implement your client upon it. Building a client should take less time relatively.

I assumed you can handle listening GPS and other sensors. Good luck.

no more sigsegv
  • 468
  • 5
  • 17
  • 1
    Given @user3386109 UDP server suggestion (besides some other info I found online), I created my server / client code. It all works and I can exchange messages between my client and my server (made a 1 way connection only: client -> server) since I won't be using my server to reply to my GPS devices. Will edit my post and show everyone the code made. I would assume I am on step 2 still. I will now try to run my client code on a different pc than my server code (inside and outside my network). Still figuring out what changes need to be made! – joaoasilva Aug 11 '20 at 18:45