URL: https://www.opennet.me/cgi-bin/openforum/vsluhboard.cgi
Форум: vsluhforumID9
Нить номер: 8403
[ Назад ]

Исходное сообщение
"socket with function mysql"

Отправлено virvdova , 19-Авг-09 18:11 
Доброго времени суток.
помогите пожалуйста разобраться с ошибкой.
вот код:
// тут сокет tcp висит на 80 порту. При запросе клиента server выводет(strout) то что клиет вводит + show tables;из mysql
//код компилируется
//#gcc -o server $(mysql_config --cflags) server.c $(mysql_config --libs)
#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>

#define QUEUE_LENGTH    10
#define BUF_LEN         4096
#define SOCK_NAME       "mysocket"
#define PORT    80

int main (void)
{
   MYSQL *conn;
   MYSQL_RES *res;
   MYSQL_ROW row;

   char *server = "localhost";
   char *user = "root";
   char *password = ""; /* set me first */
   char *database = "dbsend";

   conn = mysql_init(NULL);
//      conn = malloc(sizeof(MYSQL));

        int sock, client_sock;
        char * buf;
        char * mes;
        int count;
        struct sockaddr_in saddr;

        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 (">> %s\n", buf);
               //in db mysql

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

                printf("test \n");

          /* send SQL query */
        if (mysql_query(conn, "show tables")) {
                 fprintf(stderr, "%s\n", mysql_error(conn));
                 exit(1);
                 }

        //res = mysql_use_result(conn);
        if(!(res = mysql_store_result(conn)) && mysql_num_fields(conn))
        {
                fprintf(stderr, "error mysql_store_result\n");
        }
         /* output table name */
                printf("MySQL Tables in mysql database:\n");
                 while ((row = mysql_fetch_row(res)) != NULL)
                printf("%s \n", row[0]);
        /* close connection */
                mysql_free_result(res);
                 mysql_close(conn);
                //in db end
        //      strcpy(mes,"message \n");
        //      write (client_sock, mes, strlen (mes));

                close (client_sock);

        //      if (!strcmp (buf, "exit")) break;
        }

        free (buf);
        close (sock);
        unlink (SOCK_NAME);
        return 0;
}
проблема в том что после первого конекта все работает, а если повторить то пишет большой error://не может второй раз оброботать mysql_real_connect()
[root@centos socket2]# ./server
>> GGG

test
MySQL Tables in mysql database:
MESSAGE
>> GGG

*** glibc detected *** ./t1: corrupted double-linked list: 0x08cd6570 ***
======= Backtrace: =========
/lib/libc.so.6[0xb0e4f7]
/lib/libc.so.6[0xb103bd]
/lib/libc.so.6(__libc_malloc+0x7b)[0xb123ab]
/usr/lib/mysql/libmysqlclient.so.15(my_malloc+0x2d)[0x480bdd]
/usr/lib/mysql/libmysqlclient.so.15[0x4aaf34]
/usr/lib/mysql/libmysqlclient.so.15(vio_new+0x51)[0x4ab021]
/usr/lib/mysql/libmysqlclient.so.15(mysql_real_connect+0xac5)[0x4a7f65]
./t1[0x8048c7a]
/lib/libc.so.6(__libc_start_main+0xdc)[0xabce8c]
./t1[0x8048921]
======= Memory map: ========
00110000-00123000 r-xp 00000000 fd:00 165206     /lib/libnsl-2.5.so
00123000-00124000 r-xp 00012000 fd:00 165206     /lib/libnsl-2.5.so
00124000-00125000 rwxp 00013000 fd:00 165206     /lib/libnsl-2.5.so
00125000-00127000 rwxp 00125000 00:00 0
0012e000-0012f000 r-xp 0012e000 00:00 0          [vdso]
0012f000-00258000 r-xp 00000000 fd:00 165319     /lib/libcrypto.so.0.9.8e
00258000-0026b000 rwxp 00129000 fd:00 165319     /lib/libcrypto.so.0.9.8e
0026b000-0026f000 rwxp 0026b000 00:00 0
0026f000-0029c000 r-xp 00000000 fd:00 143476     /usr/lib/libgssapi_krb5.so.2.2
0029c000-0029d000 rwxp 0002d000 fd:00 143476     /usr/lib/libgssapi_krb5.so.2.2
002a3000-002b2000 r-xp 00000000 fd:00 165224     /lib/libresolv-2.5.so
002b2000-002b3000 r-xp 0000e000 fd:00 165224     /lib/libresolv-2.5.so
002b3000-002b4000 rwxp 0000f000 fd:00 165224     /lib/libresolv-2.5.so
002b4000-002b6000 rwxp 002b4000 00:00 0
003a5000-003ca000 r-xp 00000000 fd:00 143461     /usr/lib/libk5crypto.so.3.1
003ca000-003cb000 rwxp 00025000 fd:00 143461     /usr/lib/libk5crypto.so.3.1
003cd000-00460000 r-xp 00000000 fd:00 141984     /usr/lib/libkrb5.so.3.3
00460000-00463000 rwxp 00092000 fd:00 141984     /usr/lib/libkrb5.so.3.3
00465000-00586000 r-xp 00000000 fd:00 426848     /usr/lib/mysql/libmysqlclient.so.15.0.0
00586000-005c8000 rwxp 00120000 fd:00 426848     /usr/lib/mysql/libmysqlclient.so.15.0.0
005c8000-005c9000 rwxp 005c8000 00:00 0
00a51000-00a5a000 r-xp 00000000 fd:00 165179     /lib/libnss_files-2.5.so
00a5a000-00a5b000 r-xp 00008000 fd:00 165179     /lib/libnss_files-2.5.so
00a5b000-00a5c000 rwxp 00009000 fd:00 165179     /lib/libnss_files-2.5.so
00a89000-00aa3000 r-xp 00000000 fd:00 361617     /lib/ld-2.5.so
00aa3000-00aa4000 r-xp 00019000 fd:00 361617     /lib/ld-2.5.so
00aa4000-00aa5000 rwxp 0001a000 fd:00 361617     /lib/ld-2.5.so
00aa7000-00be5000 r-xp 00000000 fd:00 361618     /lib/libc-2.5.so
00be5000-00be7000 r-xp 0013e000 fd:00 361618     /lib/libc-2.5.so
00be7000-00be8000 rwxp 00140000 fd:00 361618     /lib/libc-2.5.so
00be8000-00beb000 rwxp 00be8000 00:00 0
00bed000-00bef000 r-xp 00000000 fd:00 165226     /lib/libdl-2.5.so
00bef000-00bf0000 r-xp 00001000 fd:00 165226     /lib/libdl-2.5.so
00bf0000-00bf1000 rwxp 00002000 fd:00 165226     /lib/libdl-2.5.so
00c0c000-00c31000 r-xp 00000000 fd:00 361619     /lib/libm-2.5.so
00c31000-00c32000 r-xp 00024000 fd:00 361619     /lib/libm-2.5.so
08048000-08049000 r-xp 00000000 fd:00 721404     /root/dev/ch25/socket2/t1
08049000-0804a00Aborted
[root@centos socket2]#
на клиенте:
[root@centos socket2]# telnet localhost 80
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
GGG
Connection closed by foreign host.
[root@centos socket2]# telnet localhost 80
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
GGG
Connection closed by foreign host.
[root@centos socket2]#

Заранее огромное спасибо.


Содержание

Сообщения в этом обсуждении
"socket with function mysql"
Отправлено primus , 20-Авг-09 18:21 
>   conn = mysql_init(NULL);

Здесь инициализируется conn
>        while (1) {

.......
>         if (!mysql_real_connect(conn, server,

Здесь conn используется
..........
>            
>     mysql_close(conn);

Здесь освобождается            
..............

>}

Здесь конец while

>проблема в том что после первого конекта все работает, а если повторить
>то пишет большой error://не может второй раз оброботать mysql_real_connect()

Для mysql_real_connect нужен указатель полученный от mysql_init.


"socket with function mysql"
Отправлено souha , 17-Ноя-09 16:51 
>[оверквотинг удален]
>..............
>
>>}
>
>Здесь конец while
>
>>проблема в том что после первого конекта все работает, а если повторить
>>то пишет большой error://не может второй раз оброботать mysql_real_connect()
>
>Для mysql_real_connect нужен указатель полученный от mysql_init.

Hello,
I have the same problem, the application is blocked in the second call to mysql_init and i got the following error:

*** glibc detected *** ./test: corrupted double-linked list: 0x08c954e8 ***
Aborted

looking for your help,
thank you in advance,
Souha
PS: I do not speak Russian, so kindly i need your reply in english.


"socket with function mysql"
Отправлено Артур , 18-Ноя-09 00:46 
>PS: I do not speak Russian, so kindly i need your reply
>in english.

in his answer, primus noted that "conn" is finalized at end of first iteration, so second iteration (obviously) fails.

so, either:
1. don't mysql_close(conn);
2. move mysql_init() call into loop. (--seems likely correct)


"socket with function mysql"
Отправлено souha , 18-Ноя-09 11:59 
>>PS: I do not speak Russian, so kindly i need your reply
>>in english.
>
>in his answer, primus noted that "conn" is finalized at end of
>first iteration, so second iteration (obviously) fails.
>
>so, either:
>1. don't mysql_close(conn);
>2. move mysql_init() call into loop. (--seems likely correct)

Hi,
Thx for your reply, but if i took the second option(move mysql_init() call into loop.), what should be the parameters of the mysql_init() function?

thank you in advance,
souha


"socket with function mysql"
Отправлено Артур , 19-Ноя-09 04:48 
>Hi,
>Thx for your reply, but if i took the second option(move mysql_init()
>call into loop.), what should be the parameters of the mysql_init()
>function?

sorry for late answer,

it depends. in the above code, "conn" is dynamically allocated, so: "while (1) { conn = mysql_init(NULL); ... }", i think.

p.s. i'm not familiar with mysql API, here is what it really does http://dev.mysql.com/doc/refman/5.0/en/mysql-init.html