Hi All,

I've write a code for client and server. It works well so far but the server just acknowledge the client of what it received. How to recode this program to make sure that once server acknowledged the client that the data sent by the client have been append in the file in the server machine so that client program can update the status of that particluar MySQL table? I've write the code but the status didnt update as what I want and I also cant compile this program. The error is: client.c:65: error: incompatible types in assignment

Below are the code:

#include <mysql.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>


void error(char *msg)
{
    perror(msg);
    exit(0);
}


int main(int argc, char *argv[]) 
{


   MYSQL *conn;
   MYSQL_RES *res;
   MYSQL_ROW row;

   char *host = "192.168.2.11";
   char *user = "daily1a";
   char *password = "daily2006";
   char *database = "28882db";

   char string1[500], query[500], Tid[50];

   void senddata(char *SendString);

   conn = mysql_init(NULL);

      
   /* Connect to database */
   if (!mysql_real_connect(conn, host,
         user, password, database, 0, NULL, 0)) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      exit(0);
   }

   /* send SQL query */
   if (mysql_query(conn, "SELECT MobileNumber, StartDate, EndDate FROM Subscriptions where Status=0")) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      exit(0);
   }

   res = mysql_use_result(conn);
   
   /* output fields 0, 1 and 2 of each row */
   while ((row = mysql_fetch_row(res)) != NULL)
   {
      bzero(string1, sizeof(string1));
      sprintf(string1,"%s,%s,%s", row[0], row[1], row[2]);
      printf ("%s\n",string1);
      senddata(string1);
   } 

    if (row[9]==0)
    {
        bzero(query, sizeof(query)); 
        Tid = atoi(row[0]); 
        sprintf(query, "UPDATE Subscriptions SET Status = 1 WHERE MobileNumber = '%s'",Tid); 
    }      

   /* Release memory used to store results and close connection */
   mysql_free_result(res);
   mysql_close(conn);

   return 0;
}

void senddata(char *SendString)
{

  char buf[8192];
  char message[256];
  int socket_descriptor;
  struct sockaddr_in pin;
  struct hostent *server_host_name;

  char * host_name = "127.0.0.1";
  int port = 8000;
  char * str = "A default test string";

  str = SendString;

  if ((server_host_name = gethostbyname(host_name)) == 0) {
  perror("Error resolving local host\n");
  exit(1);
  }

  bzero(&pin, sizeof(pin));
  pin.sin_family = AF_INET;
  pin.sin_addr.s_addr = htonl(INADDR_ANY);
  pin.sin_addr.s_addr = ((struct in_addr *)(server_host_name->h_addr))->s_addr;
  pin.sin_port = htons(port);
 
  if ((socket_descriptor = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
   perror("Error opening socket\n");
   exit(1);
  }

  if (connect(socket_descriptor, (void *)&pin, sizeof(pin)) == -1) {
   perror("Error connecting to socket\n");
   exit(1);
  }

   printf("Sending message %s to server...\n", str);

  if (send(socket_descriptor, str, strlen(str), 0) == -1) {
   perror("Error in send\n");
   exit(1);
  }

   printf("...Sent message.. wait for response...\n");

  if (recv(socket_descriptor, buf, 8192, 0) == -1) {
    perror("Error in receiving response from server\n");
    exit(1);
  }

    printf("\nResponse from server:\n\n%s\n", buf);

   close(socket_descriptor);

}

Need any help from expert here..thanks

Recommended Answers

All 2 Replies

#include <mysql.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>


void error(char *msg)
{
    perror(msg);
    exit(0);
}


int main(int argc, char *argv[]) 
{


   MYSQL *conn;
   MYSQL_RES *res;
   MYSQL_ROW row;

   char *host = "192.168.2.11";
   char *user = "daily1a";
   char *password = "daily2006";
   char *database = "28882db";

   char string1[500], query[500], Tid[50];

   void senddata(char *SendString);

   conn = mysql_init(NULL);

      
   /* Connect to database */
   if (!mysql_real_connect(conn, host,
         user, password, database, 0, NULL, 0)) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      exit(0);
   }

   /* send SQL query */
   if (mysql_query(conn, "SELECT MobileNumber, StartDate, EndDate FROM Subscriptions where Status=0")) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      exit(0);
   }

   res = mysql_use_result(conn);
   
   /* output fields 0, 1 and 2 of each row */
   while ((row = mysql_fetch_row(res)) != NULL)
   {
      bzero(string1, sizeof(string1));
      sprintf(string1,"%s,%s,%s", row[0], row[1], row[2]);
      printf ("%s\n",string1);
      senddata(string1);
   } 

    [I][B]if (row[9]==0)[/B][/I]
    {
        bzero(query, sizeof(query)); 

            // [I][B]Tid = atoi(row[0]); 
     
[/B][/I]         sprintf(query, "UPDATE Subscriptions SET Status = 1 WHERE MobileNumber = '%s'",Tid); 
    }      

   /* Release memory used to store results and close connection */
   mysql_free_result(res);
   mysql_close(conn);

   return 0;
}

void senddata(char *SendString)
{

  char buf[8192];
  char message[256];
  int socket_descriptor;
  struct sockaddr_in pin;
  struct hostent *server_host_name;

  char * host_name = "127.0.0.1";
  int port = 8000;
  char * str = "A default test string";

  str = SendString;

  if ((server_host_name = gethostbyname(host_name)) == 0) {
  perror("Error resolving local host\n");
  exit(1);
  }

  bzero(&pin, sizeof(pin));
  pin.sin_family = AF_INET;
  pin.sin_addr.s_addr = htonl(INADDR_ANY);
  pin.sin_addr.s_addr = ((struct in_addr *)(server_host_name->h_addr))->s_addr;
  pin.sin_port = htons(port);
 
  if ((socket_descriptor = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
   perror("Error opening socket\n");
   exit(1);
  }

  if (connect(socket_descriptor, (void *)&pin, sizeof(pin)) == -1) {
   perror("Error connecting to socket\n");
   exit(1);
  }

   printf("Sending message %s to server...\n", str);

  if (send(socket_descriptor, str, strlen(str), 0) == -1) {
   perror("Error in send\n");
   exit(1);
  }

   printf("...Sent message.. wait for response...\n");

  if (recv(socket_descriptor, buf, 8192, 0) == -1) {
    perror("Error in receiving response from server\n");
    exit(1);
  }

    printf("\nResponse from server:\n\n%s\n", buf);

   close(socket_descriptor);

}

In your code i can see that you have used "Tid" without declaring it before.

Also row is a array of char pointers and in your above code you are comparing row [9] with an integer value 0.

Make it necessary changes and see if it works.

HOpe it helped, bye.

Hi,

I've made some changes to my coding..but unfortunately, there were errors when compiling it..dunno how to solve it..hope anyone can help me...the errors:

client.c: In function senddata:
client.c:147: error: incompatible implicit declaration of function UpdateStatus
client.c:37: error: previous implicit declaration of UpdateStatus was here

The code is as below:

#include <mysql.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>

MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;

char *host = "192.168.2.11";
char *user = "daily1a";
char *password = "daily2006";
char *database = "28882db";

int tbuf;

void error(char *msg)
{
    perror(msg);
    exit(0);
}


int main(int argc, char *argv[]) 
{

   char string1[500], query[500], transid[50];
   int i, j;

   void senddata(char* SendString, char* transID);
   //void senddata(char *SendString, *transID);
   //void UpdateStatus(char *transID);
   void UpdateStatus(char transID[100]);
   void timebuffer();

   conn = mysql_init(NULL);

   j=1;

   while (j=1)
   {
      
   /* Connect to database */
   if (!mysql_real_connect(conn, host,
         user, password, database, 0, NULL, 0)) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      exit(0);
   }

   /* send SQL query */
   if (mysql_query(conn, "SELECT TransID, MobileNumber, StartDate, EndDate FROM Subscriptions where Status=0")) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      exit(0);
   }

   res = mysql_use_result(conn);

   
   /* output fields 0, 1 and 2 of each row */
   while ((row = mysql_fetch_row(res)) != NULL)
   {
      if (i==11)
      { 
    timebuffer();
    i=1;
      
      bzero(string1, sizeof(string1)); }
      else
      {
           //transid = row[0];
          sprintf(string1,"%s,%s,%s,%s", row[0], row[1], row[2], row[3]);
          printf ("%s\n",string1);
          senddata(string1, transid);
    i++;
      }
    
    }
}
   /* Release memory used to store results and close connection */
   mysql_free_result(res);
   mysql_close(conn);

   return 0;
}

void senddata(char* SendString, char* transID)
{

  char buf[8192];
  //char *update;
  char message[256];
  int socket_descriptor;
  struct sockaddr_in pin;
  struct hostent *server_host_name;

  char * host_name = "127.0.0.1";
  int port = 8000;
  char * str = "A default test string";
  char * trans = "123456";

  str = SendString;
  trans = transID;

  if ((server_host_name = gethostbyname(host_name)) == 0) {
  perror("Error resolving local host\n");
  exit(1);
  }

  bzero(&pin, sizeof(pin));
  pin.sin_family = AF_INET;
  pin.sin_addr.s_addr = htonl(INADDR_ANY);
  pin.sin_addr.s_addr = ((struct in_addr *)(server_host_name->h_addr))->s_addr;
  pin.sin_port = htons(port);
 
  if ((socket_descriptor = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
   perror("Error opening socket\n");
   exit(1);
  }

  if (connect(socket_descriptor, (void *)&pin, sizeof(pin)) == -1) {
   perror("Error connecting to socket\n");
   exit(1);
  }

   printf("Sending message %s to server...\n", str);

  if (send(socket_descriptor, str, strlen(str), 0) == -1) {
   perror("Error in send\n");
   exit(1);
  }

   printf("...Sent message.. wait for response...\n");

  if (recv(socket_descriptor, buf, 8192, 0) == -1) {
    perror("Error in receiving response from server\n");
    exit(1);
  }

    printf("\nResponse from server:\n\n%s\n", buf);

    UpdateStatus(TransID);
    //update=buf;
    //UpdateStatus(update);

   close(socket_descriptor);

}

void UpdateStatus(char transID[100])
{
    char query[2000];
          char query1[2000];
          char id;
        char TransID;

    conn = mysql_init(NULL);
 
       /* Connect to database */
       if (!mysql_real_connect(conn, host, user, password, database, 0, NULL, 0)) {
          fprintf(stderr, "%s\n", mysql_error(conn));
          exit(0);
       }

    bzero(query1, sizeof(query1));
        id = atoi(TransID);
     sprintf(query1, "UPDATE Subscriptions SET Status = 1 WHERE TransID = '%s'",id);

    #ifdef DEBUG
    printf("%s\n",query1);
    #endif


     /* Release memory used to store results and close connection */
       mysql_free_result(res);
       mysql_close(conn);

}


void timebuffer()
{
    struct timeval tv;

       tv.tv_sec=tbuf;
    tv.tv_usec=0;

    #ifdef TRACE
    printf ("time buffer: %d\n", tv.tv_sec);
    #endif

    select (0,0,0,0,&tv);
}

Supposedly, this program..after receiving response from server, this client program should update MySQL table.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.