server notify client to update MySQL table

Reply

Join Date: Jul 2006
Posts: 32
Reputation: whitemoss is an unknown quantity at this point 
Solved Threads: 0
whitemoss whitemoss is offline Offline
Light Poster

server notify client to update MySQL table

 
1
  #1
Sep 8th, 2006
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:
  1. #include <mysql.h>
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/socket.h>
  5. #include <netinet/in.h>
  6. #include <netdb.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9.  
  10.  
  11. void error(char *msg)
  12. {
  13. perror(msg);
  14. exit(0);
  15. }
  16.  
  17.  
  18. int main(int argc, char *argv[])
  19. {
  20.  
  21.  
  22. MYSQL *conn;
  23. MYSQL_RES *res;
  24. MYSQL_ROW row;
  25.  
  26. char *host = "192.168.2.11";
  27. char *user = "daily1a";
  28. char *password = "daily2006";
  29. char *database = "28882db";
  30.  
  31. char string1[500], query[500], Tid[50];
  32.  
  33. void senddata(char *SendString);
  34.  
  35. conn = mysql_init(NULL);
  36.  
  37.  
  38. /* Connect to database */
  39. if (!mysql_real_connect(conn, host,
  40. user, password, database, 0, NULL, 0)) {
  41. fprintf(stderr, "%s\n", mysql_error(conn));
  42. exit(0);
  43. }
  44.  
  45. /* send SQL query */
  46. if (mysql_query(conn, "SELECT MobileNumber, StartDate, EndDate FROM Subscriptions where Status=0")) {
  47. fprintf(stderr, "%s\n", mysql_error(conn));
  48. exit(0);
  49. }
  50.  
  51. res = mysql_use_result(conn);
  52.  
  53. /* output fields 0, 1 and 2 of each row */
  54. while ((row = mysql_fetch_row(res)) != NULL)
  55. {
  56. bzero(string1, sizeof(string1));
  57. sprintf(string1,"%s,%s,%s", row[0], row[1], row[2]);
  58. printf ("%s\n",string1);
  59. senddata(string1);
  60. }
  61.  
  62. if (row[9]==0)
  63. {
  64. bzero(query, sizeof(query));
  65. Tid = atoi(row[0]);
  66. sprintf(query, "UPDATE Subscriptions SET Status = 1 WHERE MobileNumber = '%s'",Tid);
  67. }
  68.  
  69. /* Release memory used to store results and close connection */
  70. mysql_free_result(res);
  71. mysql_close(conn);
  72.  
  73. return 0;
  74. }
  75.  
  76. void senddata(char *SendString)
  77. {
  78.  
  79. char buf[8192];
  80. char message[256];
  81. int socket_descriptor;
  82. struct sockaddr_in pin;
  83. struct hostent *server_host_name;
  84.  
  85. char * host_name = "127.0.0.1";
  86. int port = 8000;
  87. char * str = "A default test string";
  88.  
  89. str = SendString;
  90.  
  91. if ((server_host_name = gethostbyname(host_name)) == 0) {
  92. perror("Error resolving local host\n");
  93. exit(1);
  94. }
  95.  
  96. bzero(&pin, sizeof(pin));
  97. pin.sin_family = AF_INET;
  98. pin.sin_addr.s_addr = htonl(INADDR_ANY);
  99. pin.sin_addr.s_addr = ((struct in_addr *)(server_host_name->h_addr))->s_addr;
  100. pin.sin_port = htons(port);
  101.  
  102. if ((socket_descriptor = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  103. perror("Error opening socket\n");
  104. exit(1);
  105. }
  106.  
  107. if (connect(socket_descriptor, (void *)&pin, sizeof(pin)) == -1) {
  108. perror("Error connecting to socket\n");
  109. exit(1);
  110. }
  111.  
  112. printf("Sending message %s to server...\n", str);
  113.  
  114. if (send(socket_descriptor, str, strlen(str), 0) == -1) {
  115. perror("Error in send\n");
  116. exit(1);
  117. }
  118.  
  119. printf("...Sent message.. wait for response...\n");
  120.  
  121. if (recv(socket_descriptor, buf, 8192, 0) == -1) {
  122. perror("Error in receiving response from server\n");
  123. exit(1);
  124. }
  125.  
  126. printf("\nResponse from server:\n\n%s\n", buf);
  127.  
  128. close(socket_descriptor);
  129.  
  130. }

Need any help from expert here..thanks
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: server notify client to update MySQL table

 
1
  #2
Sep 8th, 2006
Originally Posted by whitemoss View Post
#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);

}
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 32
Reputation: whitemoss is an unknown quantity at this point 
Solved Threads: 0
whitemoss whitemoss is offline Offline
Light Poster

Re: server notify client to update MySQL table

 
1
  #3
Sep 13th, 2006
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:
  1. #include <mysql.h>
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/socket.h>
  5. #include <netinet/in.h>
  6. #include <netdb.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9.  
  10. MYSQL *conn;
  11. MYSQL_RES *res;
  12. MYSQL_ROW row;
  13.  
  14. char *host = "192.168.2.11";
  15. char *user = "daily1a";
  16. char *password = "daily2006";
  17. char *database = "28882db";
  18.  
  19. int tbuf;
  20.  
  21. void error(char *msg)
  22. {
  23. perror(msg);
  24. exit(0);
  25. }
  26.  
  27.  
  28. int main(int argc, char *argv[])
  29. {
  30.  
  31. char string1[500], query[500], transid[50];
  32. int i, j;
  33.  
  34. void senddata(char* SendString, char* transID);
  35. //void senddata(char *SendString, *transID);
  36. //void UpdateStatus(char *transID);
  37. void UpdateStatus(char transID[100]);
  38. void timebuffer();
  39.  
  40. conn = mysql_init(NULL);
  41.  
  42. j=1;
  43.  
  44. while (j=1)
  45. {
  46.  
  47. /* Connect to database */
  48. if (!mysql_real_connect(conn, host,
  49. user, password, database, 0, NULL, 0)) {
  50. fprintf(stderr, "%s\n", mysql_error(conn));
  51. exit(0);
  52. }
  53.  
  54. /* send SQL query */
  55. if (mysql_query(conn, "SELECT TransID, MobileNumber, StartDate, EndDate FROM Subscriptions where Status=0")) {
  56. fprintf(stderr, "%s\n", mysql_error(conn));
  57. exit(0);
  58. }
  59.  
  60. res = mysql_use_result(conn);
  61.  
  62.  
  63. /* output fields 0, 1 and 2 of each row */
  64. while ((row = mysql_fetch_row(res)) != NULL)
  65. {
  66. if (i==11)
  67. {
  68. timebuffer();
  69. i=1;
  70.  
  71. bzero(string1, sizeof(string1)); }
  72. else
  73. {
  74. //transid = row[0];
  75. sprintf(string1,"%s,%s,%s,%s", row[0], row[1], row[2], row[3]);
  76. printf ("%s\n",string1);
  77. senddata(string1, transid);
  78. i++;
  79. }
  80.  
  81. }
  82. }
  83. /* Release memory used to store results and close connection */
  84. mysql_free_result(res);
  85. mysql_close(conn);
  86.  
  87. return 0;
  88. }
  89.  
  90. void senddata(char* SendString, char* transID)
  91. {
  92.  
  93. char buf[8192];
  94. //char *update;
  95. char message[256];
  96. int socket_descriptor;
  97. struct sockaddr_in pin;
  98. struct hostent *server_host_name;
  99.  
  100. char * host_name = "127.0.0.1";
  101. int port = 8000;
  102. char * str = "A default test string";
  103. char * trans = "123456";
  104.  
  105. str = SendString;
  106. trans = transID;
  107.  
  108. if ((server_host_name = gethostbyname(host_name)) == 0) {
  109. perror("Error resolving local host\n");
  110. exit(1);
  111. }
  112.  
  113. bzero(&pin, sizeof(pin));
  114. pin.sin_family = AF_INET;
  115. pin.sin_addr.s_addr = htonl(INADDR_ANY);
  116. pin.sin_addr.s_addr = ((struct in_addr *)(server_host_name->h_addr))->s_addr;
  117. pin.sin_port = htons(port);
  118.  
  119. if ((socket_descriptor = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  120. perror("Error opening socket\n");
  121. exit(1);
  122. }
  123.  
  124. if (connect(socket_descriptor, (void *)&pin, sizeof(pin)) == -1) {
  125. perror("Error connecting to socket\n");
  126. exit(1);
  127. }
  128.  
  129. printf("Sending message %s to server...\n", str);
  130.  
  131. if (send(socket_descriptor, str, strlen(str), 0) == -1) {
  132. perror("Error in send\n");
  133. exit(1);
  134. }
  135.  
  136. printf("...Sent message.. wait for response...\n");
  137.  
  138. if (recv(socket_descriptor, buf, 8192, 0) == -1) {
  139. perror("Error in receiving response from server\n");
  140. exit(1);
  141. }
  142.  
  143. printf("\nResponse from server:\n\n%s\n", buf);
  144.  
  145. UpdateStatus(TransID);
  146. //update=buf;
  147. //UpdateStatus(update);
  148.  
  149. close(socket_descriptor);
  150.  
  151. }
  152.  
  153. void UpdateStatus(char transID[100])
  154. {
  155. char query[2000];
  156. char query1[2000];
  157. char id;
  158. char TransID;
  159.  
  160. conn = mysql_init(NULL);
  161.  
  162. /* Connect to database */
  163. if (!mysql_real_connect(conn, host, user, password, database, 0, NULL, 0)) {
  164. fprintf(stderr, "%s\n", mysql_error(conn));
  165. exit(0);
  166. }
  167.  
  168. bzero(query1, sizeof(query1));
  169. id = atoi(TransID);
  170. sprintf(query1, "UPDATE Subscriptions SET Status = 1 WHERE TransID = '%s'",id);
  171.  
  172. #ifdef DEBUG
  173. printf("%s\n",query1);
  174. #endif
  175.  
  176.  
  177. /* Release memory used to store results and close connection */
  178. mysql_free_result(res);
  179. mysql_close(conn);
  180.  
  181. }
  182.  
  183.  
  184. void timebuffer()
  185. {
  186. struct timeval tv;
  187.  
  188. tv.tv_sec=tbuf;
  189. tv.tv_usec=0;
  190.  
  191. #ifdef TRACE
  192. printf ("time buffer: %d\n", tv.tv_sec);
  193. #endif
  194.  
  195. select (0,0,0,0,&tv);
  196. }

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

Thanks
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC