Codice PHP:
#include <stdio.h>
#include <mysql/my_global.h>
#include <mysql/mysql.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define QUEUE_LEN 20
#define BUFF_SIZE 20
// per c++ #include <cstdlib>
// per c++ #include <climits>
// per c++ #include <cstring>
// per c++ #include <memory>
int main (){
MYSQL *conn; /*comandi per inizializzare mysql */
MYSQL_RES *result;
MYSQL_ROW row;
MYSQL db;
MYSQL_RES *res;
MYSQL_FIELD *f;
unsigned int n;
int sk;
int x;
int y;
int i;
int client; /* Socket per la comunicazione con il client */
int ret; /* Ritorno delle funzioni */
int len; /* Dimensione strutture dati */
struct sockaddr_in s_addr; /* Indirizzo Server */
struct sockaddr_in c_addr; /* Indirizzo Client */
int buffer[BUFF_SIZE]; /* definisco l'arrey Buffer di Ricezione dati */
int port = 2001; /* Porta del Server */
char address[] = "127.0.0.1"; /* Indirizzo IP del Server */
const char *query =
"select * "
"from data ";
const char *db_host="localhost";
const char *db_user="root";
const char *db_pass="";
const char *db_name="";
/* Creazione del socket */
sk = socket(AF_INET, SOCK_STREAM, 0);
while (1) {
/* Inizializzazione indirizzo del Server */
bzero(&s_addr, sizeof(s_addr));
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(port);
s_addr.sin_addr.s_addr = htonl(INADDR_ANY);
/* Bind del socket */
ret = bind(sk, (struct sockaddr*) &s_addr, sizeof(s_addr) );
/* Creazione coda di richieste di connessione */
ret = listen(sk, QUEUE_LEN);
/* Inizializzazione indirizzo del Client */
bzero(&c_addr, sizeof(c_addr));
len = sizeof(c_addr);
/* Accept connessioni con i client */
client = accept(sk, (struct sockaddr*)&c_addr, &len );
/* Ricezione Dati */
ret = recv(client, buffer, BUFF_SIZE, 0);
/* scomposizione array in variabili indipendenti */
int receivedx = buffer[0];
int receivedy = buffer[1];
int receivedpos = buffer[2];
printf("X: %d\n", receivedx);
printf("Y: %d\n", receivedy);
printf("POS: %d\n", receivedpos);
if (mysql_init(&db)==NULL) {
printf ("Non sono riuscito a inizializzare. \n");
exit(1);
}
if (!mysql_real_connect(&db, db_host, db_user, db_pass, NULL, 0, NULL, 0))
printf ("Errore di connessione al database su %s\n",db_host);
else
printf ("Connessione avvenuta con successo al database su %s\n",db_host);
if (mysql_select_db(&db,db_name))
printf ("Errore di connessione al database %s\n",db_name);
else
printf ("Connessione avvenuta con successo al database %s\n",db_name);
if ( mysql_real_query (&db, query, (unsigned int) strlen(query)) ) {
printf ("Errore nell'esecuzione della query %s\n",query);
exit(2);
}
res = mysql_store_result(&db);
n = mysql_num_fields(res);
f = mysql_fetch_fields(res);
for (i=0; i<n; i++){
printf ("%s\t",f[i].name);
printf ("\n");
}
while ((row=mysql_fetch_row(res))) {
for (i=0; i<n; i++){ printf ("[%s]\t", row[i]); }
row[1] = x;
row[2] = y;
printf ("\n");
//PARTE INSERIMENTO/UPDATE dati nel database
char str[79];
if(row[1] == receivedx && row[2] == receivedy){
sprintf(str, "UPDATE data SET pos (%d)", receivedpos);
mysql_query(conn, str);
printf("Elemento modificato con successo");
}
else {
sprintf(str, "INSERT INTO data (x, y, pos) VALUES ( %d, %d, %d)", receivedx, receivedy, receivedpos);
mysql_query(conn, str);
printf("Elemento inserito con successo");
}
}
mysql_free_result (res);
mysql_close(&db);
/* Chiusura del socket di comunicazione con il Client */
close(client);
}
}