- 论坛徽章:
- 0
|
AIX下面用sybase的ct-lib编程时,开线程connect会报错,但不开线程直接连成功,如下:
环境:
AIX5.3
SYBASE ASE12.5-pcclient
g++ version 4.0.0
编译选项:-lstdc++ -lpthread -D_THREAD_SAFE -g
错误:
Open Client Message: LAYER(5), ORIGIN(3) ,SEVERITY(5) NUMBER(4).
network packet layer: internal net library error: Net-Lib protocol driver call to connect two endpoints failed
代码:
#include <stdio.h>
#include <cspublic.h>
#include <sybfront.h>
#include <sybdb.h>
#include <ctpublic.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/wait.h>
#define LIB_VERSION CS_VERSION_125
CS_RETCODE csmsg_callback(CS_CONTEXT *context, CS_CLIENTMSG *emsgp)
{
printf(
"CS-Library error: severity(%ld) layer(%ld) origin(%ld) number(%ld). \nerrmsg: %s.\n",
(long)CS_SEVERITY(emsgp->msgnumber),
(long)CS_LAYER(emsgp->msgnumber),
(long)CS_ORIGIN(emsgp->msgnumber),
(long)CS_NUMBER(emsgp->msgnumber),
emsgp->msgstring);
return CS_SUCCEED;
}
CS_RETCODE ctmsg_callback(CS_CONTEXT *context,CS_CONNECTION *connection,CS_CLIENTMSG *errmsg)
{
printf(
"\nOpen Client Message: LAYER(%d), ORIGIN(%d) ,SEVERITY(%d) NUMBER(%d).\nerrmsg: %s.\n",
CS_LAYER(errmsg->msgnumber),
CS_ORIGIN(errmsg->msgnumber),
CS_SEVERITY(errmsg->msgnumber),
CS_NUMBER(errmsg->msgnumber),
errmsg->msgstring);
return CS_SUCCEED;
}
CS_RETCODE servermsg_callback(CS_CONTEXT *context,CS_CONNECTION *connection,CS_SERVERMSG *srvmsg)
{
printf(
"\nServer message: Message number(%d), Severity(%d), State(%d), Line(%d).\nerrmsg: %s.\n",
srvmsg->msgnumber,
srvmsg->severity,
srvmsg->state,
srvmsg->line,
srvmsg->text);
return CS_SUCCEED;
}
bool set_callback_handler(CS_CONTEXT* context)
{
CS_RETCODE recode = CS_SUCCEED;
if((recode = cs_config(context, CS_SET, CS_MESSAGE_CB, (CS_VOID*)csmsg_callback, CS_UNUSED, NULL))
!= CS_SUCCEED)
{
printf("Failed to set callback handler for CS_LIB");
return false;
}
if((recode = ct_callback(context, NULL, CS_SET, CS_CLIENTMSG_CB, (CS_VOID*)ctmsg_callback))
!= CS_SUCCEED)
{
printf("Failed to set callback handler for CT_LIB");
return false;
}
if((recode = ct_callback(context, NULL, CS_SET, CS_SERVERMSG_CB, (CS_VOID*)servermsg_callback))
!= CS_SUCCEED)
{
printf("Failed to set callback handler for SRV_LIB");
return false;
}
return true;
}
bool set_param(CS_CONNECTION* connection, const char* username, const char* password, const char* appname)
{
if (ct_con_props(connection, CS_SET, CS_USERNAME,
(CS_VOID*)username, CS_NULLTERM, NULL) != CS_SUCCEED)
{
ct_con_drop(connection);
printf("inner_connect : ct_con_props(username) failed.\n");
return false;
}
if (ct_con_props(connection, CS_SET, CS_PASSWORD,
(CS_VOID*)password, CS_NULLTERM, NULL) != CS_SUCCEED)
{
ct_con_drop(connection);
printf("inner_connect : ct_con_props(password) failed.\n");
return false;
}
if (ct_con_props(connection, CS_SET, CS_APPNAME,
(CS_VOID*)appname, CS_NULLTERM, NULL) != CS_SUCCEED)
{
ct_con_drop(connection);
printf("inner_connect : ct_con_props(appname) failed.\n");
return false;
}
return true;
}
void* test(void*)
{
CS_CONTEXT* context = 0;
if (cs_ctx_alloc(LIB_VERSION, &context) != CS_SUCCEED)
{
printf("ex_init: cs_ctx_alloc() failed.");
return 0;
}
if (ct_init(context, LIB_VERSION) != CS_SUCCEED)
{
cs_ctx_drop(context);
printf("ex_init: ct_init() failed.");
return 0;
}
if(!set_callback_handler(context))
{
cs_ctx_drop(context);
return 0;
}
CS_CONNECTION* connection = 0;
if (ct_con_alloc(context, &connection) != CS_SUCCEED)
{
printf("inner_connect : ct_con_alloc() failed.\n");
return 0;
}
if(!set_param(connection, "lcc", "lcc123", "apptest"))
{
return 0;
}
if (ct_connect(connection, "SYBASE5", CS_NULLTERM) != CS_SUCCEED)
{
printf("inner_connect : ct_connect() failed");
return 0;
}
printf("connected success.\n");
if (ct_close(connection, CS_UNUSED) != CS_SUCCEED)
{
printf("ct_close() error.\n");
return 0;
}
if (ct_con_drop(connection) != CS_SUCCEED)
{
printf("ex_con_cleanup: ct_con_drop() failed");
return 0;
}
if (ct_exit(context, CS_UNUSED ) != CS_SUCCEED)
{
printf("inner_context_cleanup: ct_exit() failed.\n");
return 0;
}
if (cs_ctx_drop(context) != CS_SUCCEED)
{
printf("inner_context_cleanup: cs_ctx_drop() failed.\n");
return 0;
}
return 0;
}
int main()
{
//at this case, connect success.
test(0);
//but this connect fail.
pthread_t pid;
if(pthread_create(&pid, NULL, test, 0) != 0 )
{
printf("pthread_create() failed.\n");
return 1;
}
sleep(1000);
return 0;
} |
|