Помогите разобраться в чем проблема
вот код:
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <sys/un.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <mysql.h>
#include <string.h>
#define QUEUE_LENGTH    10
#define BUF_LEN         4096
#define SOCK_NAME       "mysocket"
#define PORT    1081
int main (void)
{
   MYSQL *conn;
   MYSQL_RES *res;
   MYSQL_ROW row;
   char *server = "localhost";
   char *user = "root";
   char *password = ""; /* set me first */
   char *database = "messdb";
        int sock, client_sock;
        char * buf;
        char * buffer[100];
        char * mes;
        int count;
        struct sockaddr_in saddr;
                int i=5;
        sock = socket (PF_INET, SOCK_STREAM, 0);
        if (sock == -1) {
                fprintf (stderr, "socket() error\n");
                return 1;
        }
       buf = (char *) malloc (BUF_LEN);
        if (buf == NULL) {
                fprintf (stderr, "malloc() error\n");
                return 1;
        }
        saddr.sin_family = AF_INET;
        saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
        saddr.sin_port = htons (PORT);
        if (bind (sock, (struct sockaddr *) &saddr,
                        sizeof (saddr)) == -1) {
                fprintf (stderr, "bind() error\n");
                return 1;
        }
        if (listen (sock, QUEUE_LENGTH) == -1) {
                fprintf (stderr, "listen() error\n");
                return 0;
        }
        while (1) {
                client_sock = accept (sock, NULL, NULL);
                if (client_sock == -1) {
                        fprintf (stderr, "accept() error\n");
                        return 1;
                }
                if ((count = read (client_sock,
                        buf, BUF_LEN-1)) == -1) {
                        fprintf (stderr, "read() error\n");
                        return 1;
                }
                buf[count] = '\0';
                //printf ("number for message %s\n", buf);
               //in db mysql
          conn = mysql_init(NULL);
     /* Connect to database */
         if (!mysql_real_connect(conn, server,
         user, password, database, 0, NULL, 0)) {
                 fprintf(stderr, "%s\n", mysql_error(conn));
         exit(1);
                }
          /* send SQL query */
        //strcpy(buf_sql,"select * from message");
        //strcat(buf_sql,buf);
        //strcat(buf_sql,"\")");
        //fprintf(stdout,buf_sql);
        if (mysql_query(conn, "select * from message")) {
                 fprintf(stderr, "%s\n", mysql_error(conn));
                 exit(1);
                 }
        if(!(res = mysql_store_result(conn)) && mysql_num_fields(conn))
        {
                fprintf(stderr, "error mysql_store_result\n");
        }
         /* output table name */
                //printf("mysql result:\n");
                 while ((row = mysql_fetch_row(res)) != NULL)
                {
                write (client_sock, row[0], strlen (row[0]));
                }
                fprintf(stdout,i);
        /* close connection */
                mysql_free_result(res);
                 mysql_close(conn);
                //in db end
                close (client_sock);
                if (!strcmp (buf, "exit")) break;
        }
        free (buf);
        close (sock);
        unlink (SOCK_NAME);
        return 0;
}
Програма компилируеться: gcc -o socket2-server $(mysql_config --cflags) socket2-server.c $(mysql_config --libs)
После первой попытки конектиться клиенту получаем:
[root@centos socket2]# ./socket2-server
Segmentation fault
[root@centos socket2]# 
Если закоментировать переменую "i"(2 строки:int i=5; и fprintf(stdout,i);) все работает чудно(но переменая мне в дальнейшем нужна). не могу понять что с ней и как профиксить.И вообще как от нее зависит умирания всего.
Заранее большое спасибо!


    
      Ответ на:
      
          комментарий
        от kemm 
  
    
      Ответ на:
      
          комментарий
        от Sylvia 
  
    
      Ответ на:
      
          комментарий
        от Cy6erBr4in 
  


    
      Ответ на:
      
          комментарий
        от Cy6erBr4in 
  
    
      Ответ на:
      
          комментарий
        от legolegs 
  
    
      Ответ на:
      
          комментарий
        от kemm 
  
    
      Ответ на:
      
          комментарий
        от Sylvia 
  
    
      Ответ на:
      
          комментарий
        от Sylvia 
  
    
      Ответ на:
      
          комментарий
        от Cy6erBr4in 
  
    
      Ответ на:
      
          комментарий
        от legolegs 
  
    
      Ответ на:
      
          комментарий
        от Cy6erBr4in 
  
    
      Ответ на:
      
          комментарий
        от Cy6erBr4in 
  
    
      Ответ на:
      
          комментарий
        от legolegs 
  
    
      Ответ на:
      
          комментарий
        от legolegs 
  
    
      Ответ на:
      
          комментарий
        от Cy6erBr4in 
  
    
      Ответ на:
      
          комментарий
        от kemm 
  
    
      Ответ на:
      
          комментарий
        от Cy6erBr4in 
  
    
      Ответ на:
      
          комментарий
        от true_admin 
  
    
      Ответ на:
      
          комментарий
        от true_admin 
  
    
        Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.
      
Похожие темы
- Форум socket with function mysql (2009)
- Форум socket error (2009)
- Форум Segmentation fault (C & Mysql) (2007)
- Форум си, mysql, указатель на возвращаемую строку. почему си так себя ведет (2019)
- Форум MYSQL и C API (2007)
- Форум Re: пример на C для MySQL (сам разобрался жаль что вопрос сам удалили) (2007)
- Форум Неправильное выделение памяти (2006)
- Форум non-blocking write (2011)
- Форум pthread не работает (2006)
- Форум Проблемы с серверным приложением (2003)