当前位置: 技术问答>linux和unix
建立多线程的服务器···
来源: 互联网 发布时间:2016-09-22
本文导语: 不废话,直接上代码: void * talk_to_client(void *data) { int new_server_socket = (int)data; char buffer[BUFFER_SIZE]; bzero(buffer, BUFFER_SIZE); MYSQL db;/*connector*/ MYSQL_RES * result;/*result buffer*/ MYS...
不废话,直接上代码:
void * talk_to_client(void *data)
{
int new_server_socket = (int)data;
char buffer[BUFFER_SIZE];
bzero(buffer, BUFFER_SIZE);
MYSQL db;/*connector*/
MYSQL_RES * result;/*result buffer*/
MYSQL_ROW row;/*one row of the result*/
int i;
/*初始化mysql*/
if(mysql_init(&db)==NULL)
{
fprintf(stderr,"Fail to initialize the db.n");
return -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!",mysql_error(&db));
return -1;
}
char str[2000];char str1[2000];
char Where_SQL[]="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";
//scanf("%s",str1);
strcat(str,Where_SQL);
//strcat(str,str1);
if(mysql_query(&db,str)!= 0)
{
fprintf(stderr,"Fail to query the db for information.n");
return -1;
}
/*获得查询结果*/
if ((result = mysql_store_result(&db)) == NULL)
{
fprintf(stderr,"Fail to get the result.n");
return -1;
}
/*fetching each row*/
while((row=mysql_fetch_row(result))!=NULL)
{
puts("================================================");
printf("user_id: %sn",row[0]);
printf("email: %sn",row[1]);
printf("domain: %sn",row[2]);
printf("status: %sn",row[3]);
printf("domain_id: %sn",row[4]);
printf("vip_started_at: %sn",row[5]);
printf("vip_ended_at: %sn",row[6]);
puts("================================================");
}
// mysql_free_result(result);
// mysql_close(&db);
// return 0;
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%sn%s%sn%s%sn%s%sn%s%sn%s%sn%s%sn",m1,row[0],m2,row[1],m3,row[2],m4,row[3],m5,row[4],m6,row[5],m7,row[6]);
printf("%sn",buffer);
}
send(new_server_socket,buffer,strlen(buffer),0);
bzero(buffer,BUFFER_SIZE);
//接收客户端发送来的信息到buffer中
int length = recv(new_server_socket,buffer,BUFFER_SIZE,0);
if (length
void * talk_to_client(void *data)
{
int new_server_socket = (int)data;
char buffer[BUFFER_SIZE];
bzero(buffer, BUFFER_SIZE);
MYSQL db;/*connector*/
MYSQL_RES * result;/*result buffer*/
MYSQL_ROW row;/*one row of the result*/
int i;
/*初始化mysql*/
if(mysql_init(&db)==NULL)
{
fprintf(stderr,"Fail to initialize the db.n");
return -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!",mysql_error(&db));
return -1;
}
char str[2000];char str1[2000];
char Where_SQL[]="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";
//scanf("%s",str1);
strcat(str,Where_SQL);
//strcat(str,str1);
if(mysql_query(&db,str)!= 0)
{
fprintf(stderr,"Fail to query the db for information.n");
return -1;
}
/*获得查询结果*/
if ((result = mysql_store_result(&db)) == NULL)
{
fprintf(stderr,"Fail to get the result.n");
return -1;
}
/*fetching each row*/
while((row=mysql_fetch_row(result))!=NULL)
{
puts("================================================");
printf("user_id: %sn",row[0]);
printf("email: %sn",row[1]);
printf("domain: %sn",row[2]);
printf("status: %sn",row[3]);
printf("domain_id: %sn",row[4]);
printf("vip_started_at: %sn",row[5]);
printf("vip_ended_at: %sn",row[6]);
puts("================================================");
}
// mysql_free_result(result);
// mysql_close(&db);
// return 0;
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%sn%s%sn%s%sn%s%sn%s%sn%s%sn%s%sn",m1,row[0],m2,row[1],m3,row[2],m4,row[3],m5,row[4],m6,row[5],m7,row[6]);
printf("%sn",buffer);
}
send(new_server_socket,buffer,strlen(buffer),0);
bzero(buffer,BUFFER_SIZE);
//接收客户端发送来的信息到buffer中
int length = recv(new_server_socket,buffer,BUFFER_SIZE,0);
if (length