家伙,我要面对这个代码的一个问题,这个问题是超时我指的是MySQL的超时 - C/C++
花费的时间程序来告诉我,如果服务器连接或不超时。如果用我
本地主机我得到的答案快,但是当我连接到我的本地主机之外需要50sc -
1.5分钟响应和程序frezz,直到它完成。我可以修复frezzing,或使我自己的超时,如果仍然等待50sc后,告诉我连接失败并停止?
请使用电码作为帮助,becouse我会更好地理解它,感谢所有帮助我得到
PS:使用Mac
#include "mysql.h"
#include <stdio.h>
#include <stdlib.h>
// Other Linker Flags: -lmysqlclient -lm -lz
// just going to input the general details and not the port numbers
struct connection_details
{
char *server;
char *user;
char *password;
char *database;
};
MYSQL* mysql_connection_setup(struct connection_details mysql_details)
{
// first of all create a mysql instance and initialize the variables within
MYSQL *connection = mysql_init(NULL);
// connect to the database with the details attached.
if (!mysql_real_connect(connection,mysql_details.server, mysql_details.user, mysql_details.password, mysql_details.database, 0, NULL, 0)) {
printf("Conection error : %s\n", mysql_error(connection));
exit(1);
}
return connection;
}
MYSQL_RES* mysql_perform_query(MYSQL *connection, char *sql_query)
{
// send the query to the database
if (mysql_query(connection, sql_query))
{
printf("MySQL query error : %s\n", mysql_error(connection));
exit(1);
}
return mysql_use_result(connection);
}
int main()
{
MYSQL *conn; // the connection
MYSQL_RES *res; // the results
MYSQL_ROW row; // the results row (line by line)
struct connection_details mysqlD;
mysqlD.server = (char*)"Localhost"; // where the mysql database is
mysqlD.user = (char*)"root"; // the root user of mysql
mysqlD.password = (char*)"123456"; // the password of the root user in mysql
mysqlD.database = (char*)"test"; // the databse to pick
// connect to the mysql database
conn = mysql_connection_setup(mysqlD);
// assign the results return to the MYSQL_RES pointer
res = mysql_perform_query(conn, (char*) "SELECT * FROM me");
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) !=NULL)
printf("%s - %s\n", row[0], row[1], row[2]); // <-- Rows
/* clean up the database result set */
mysql_free_result(res);
/* clean up the database link */
mysql_close(conn);
return 0;
}
超时在TCP协议栈内 - 您可能需要做一些调整,但是直到您告诉平台。 – trojanfoe 2012-03-18 17:51:29
使用Mac作为平台的进出口 – user1262876 2012-03-18 17:52:35
当您在本地主机之外实现代码时,您确定更改了代码的基础知识吗? 'mysqlD.server =(char *)“Localhost”;'在非本地主机服务器上没有意义 此外,当它在另一个主机上实现时,它会连接吗?我的意思是你说它冻结了一段时间,但它连接? – TheNavigat 2012-03-18 17:53:14