红联Linux门户
Linux帮助

一个libevent多线程的服务器引发的问题?????

发布时间:2010-09-13 11:47:27来源:红联作者:410076766
我自己写了一个简单的libevent建立多线程服务器的代码,希望将数据库查询出来的数据发送到客户端???,编译没出什么问题,但是运行之后数据没传过去???,哪位知道怎么改?谢谢!!!
客户端结束后服务端出现的东西如下:

*** glibc detected *** ./11s: free(): invalid next size (normal): 0x09d710e0 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6(+0x6b591)[0x9c1591]
/lib/tls/i686/cmov/libc.so.6(+0x6cde8)[0x9c2de8]
/lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0x9c5ecd]
/lib/tls/i686/cmov/libc.so.6(fclose+0x14a)[0x9b1aaa]
/lib/tls/i686/cmov/libnss_files.so.2(_nss_files_getservbyname_r+0x12b)[0x47b58b]
/lib/tls/i686/cmov/libc.so.6(getservbyname_r+0xdc)[0xa3c98c]
/lib/tls/i686/cmov/libc.so.6(getservbyname+0x86)[0xa3c7e6]
/usr/lib/libmysqlclient.so.16(mysql_server_init+0xc6)[0x59c996]
/usr/lib/libmysqlclient.so.16(mysql_init+0x39)[0x5cb639]
./11s[0x8048d18]
/usr/local/lib/libevent-1.4.so.2(event_base_loop+0x358)[0x115e08]
/usr/local/lib/libevent-1.4.so.2(event_loop+0x29)[0x115ff9]
/usr/local/lib/libevent-1.4.so.2(event_dispatch+0x1e)[0x11601e]
./11s[0x80496b5]
[0x2]
======= Memory map: ========
00110000-00126000 r-xp 00000000 08:06 3401221 /usr/local/lib/libevent-1.4.so.2.1.2
00126000-00127000 r--p 00015000 08:06 3401221 /usr/local/lib/libevent-1.4.so.2.1.2
00127000-00128000 rw-p 00016000 08:06 3401221 /usr/local/lib/libevent-1.4.so.2.1.2
00128000-0014c000 r-xp 00000000 08:01 7866712 /lib/tls/i686/cmov/libm-2.11.1.so
0014c000-0014d000 r--p 00023000 08:01 7866712 /lib/tls/i686/cmov/libm-2.11.1.so
0014d000-0014e000 rw-p 00024000 08:01 7866712 /lib/tls/i686/cmov/libm-2.11.1.so
0014e000-0016b000 r-xp 00000000 08:01 7866695 /lib/libgcc_s.so.1
0016b000-0016c000 r--p 0001c000 08:01 7866695 /lib/libgcc_s.so.1
0016c000-0016d000 rw-p 0001d000 08:01 7866695 /lib/libgcc_s.so.1
????????





服务端代码如下:

#include
#include
#include
#include
#include
#include
#include
#include
#include // for errno
#include
#include // for sockaddr_in
#include

#include

//#include
#include
//#include
//#include
//#include
//#include

#define IPADDR "127.0.0.1"
#define PORT 11111

void sock_read(int server_socket, short event, void *arg) //
{
printf("start sock_read\n");

struct event ev;
struct sockaddr addr;
char buffer[500];
MYSQL db;/*connector*/
MYSQL_RES * result;/*result buffer*/
MYSQL_ROW row;/*one row of the result*/

if(mysql_init(&db)==NULL)
{
fprintf(stderr,"Fail to initialize the db.\n");
exit(1);
}
char host[32] = "localhost";
char user[32] = "root";
char passwd[38] = "123456";
char dbc[32] = "mysql";
char getDate[1024];
char id[2000];
char s[2000];
memset(getDate,0,1024);
if (NULL == (mysql_real_connect(&db,host,user,passwd,dbc,0,NULL,0)))
{
fprintf(stderr,"Connect to mysql Error:%s!\n",mysql_error(&db));
exit(1);
}
char str[2000];char str1[2000];
char Where_SQL[1024]="SELECT d.user_id,u.email,d.domain,d.status,v.* FROM domain_vip_status AS v, domains AS d, users AS u WHERE d.id = v.domain_id AND u.id = d.user_id AND d.grade != 'Free' ORDER BY vip_ended_at, email ";

if(mysql_query(&db,Where_SQL)!= 0)
{
printf("mysql_query(&db,str)=%d\n",mysql_query(&db,str));
fprintf(stderr,"Fail to query the db for information.\n");
exit(1);
}
else
{
puts("Connect to mysql success!\n");
}

/*获得查询结果*/
if ((result = mysql_store_result(&db)) == NULL)
{
fprintf(stderr,"Fail to get the result.\n");
exit(1);
}
/*fetching each row*/
while((row=mysql_fetch_row(result))!=NULL)
{
puts("================================================");
printf("user_id: %s\n",row[0]);
printf("email: %s\n",row[1]);
printf("domain: %s\n",row[2]);
printf("status: %s\n",row[3]);
printf("domain_id: %s\n",row[4]);
printf("vip_started_at: %s\n",row[5]);
printf("vip_ended_at: %s\n",row[6]);
puts("================================================");

char m1[]="user_id:";char m2[]=" email:";char m3[]=" domain:";char m4[]=" status:";char m5[]=" domain_id:";char m6[]=" vip_started_at:";char m7[]=" vip_ended_at:";

memset(buffer,0,500);
sprintf(buffer,"%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n",m1,row[0],m2,row[1],m3,row[2],m4,row[3],m5,row[4],m6,row[5],m7,row[6]);
strcat(buffer,"\n"); //C语言字符串连接

int ss = send(server_socket,buffer,strlen(buffer),0); //*new_
printf("ss=%d\n",ss);
memset(buffer,0,500);
recv(server_socket,buffer,500,0);
printf("recv[%s]\n",buffer);
}
send(server_socket,"over",strlen("over"),0); //*new_
mysql_free_result(result);
mysql_close(&db);
}

static void sock_accept(int server_socket, short event, void *arg)
{
printf("start sock_accept\n");
struct event *ev = arg;
struct sockaddr addr;
int s;
socklen_t len = sizeof(addr);

struct event *rev = (struct event *)malloc(sizeof(rev));//*re
if((s = accept(server_socket, &addr, &len)) == -1)
{
fprintf(stderr, "Sock Accept Failed!\n");
exit(0);
}
event_set(rev, s, EV_READ, sock_read, rev); //EV_READ,short event
event_add(rev, NULL);
event_add(ev, NULL);
}
int main () //int argc, char **argv
{
struct event *ev; //= arg

int server_socket;
struct sockaddr_in addr;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1)
{
perror("socket error\n");
exit(-1);
}
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr =htonl(INADDR_ANY); //INADDR_ANY;IPADDR
if (bind(server_socket,(struct sockaddr*)&addr, sizeof(addr)) == -1)
{
perror("bind error\n");
exit(-1);
}

if (listen(server_socket, 5) == -1)
{
perror("listen error\n");
exit(-1);
}
event_init();
event_set(&ev,server_socket,EV_READ,sock_accept,&ev); //EV_READ short event
event_add(&ev, NULL);
event_dispatch();
return (0);
}
文章评论

共有 0 条评论