- 论坛徽章:
- 0
|
- typedef struct Task_Param_s
- {
- int iSock; /* 客户端socket */
- struct sockaddr_in server_addr; /* 服务器地址 */
- }Task_Param_t;
- void TaskConnect(void *arg)
- {
- Task_Param_t *pstParam;
- if(NULL == arg)
- {
- ASSERT(0);
- return;
- }
- pstParam = (Task_Param_t *)arg;
- if(connect(pstParam->iSock,&pstParam->server_addr,sizeof(struct sockaddr_in)) == -1)
- {
- printf("[%s:%d]connect %s:%d error\r\n",
- __FILE__, __LINE__, inet_ntoa(pstParam->server_addr.sin_addr), ntohs(pstParam->server_addr.sin_port));
- return;
- }
- }
- int test()
- {
- pthread_t id;
- Task_Param_t stConnectParam;
- int iClientSock = -1;
- iClientSock = socket(AF_INET, SOCK_STREAM, 0);
- stConnectParam.server_addr.sin_family = AF_INET;
- stConnectParam.server_addr.sin_port = htons(POST);
- stConnectParam.server_addr.sin_addr.s_addr = inet_addr(ADDR);
- printf("[%s:%d]connect %s:%d\r\n",
- __FILE__, __LINE__, inet_ntoa(stConnectParam.server_addr.sin_addr), ntohs(stConnectParam.server_addr.sin_port));
- if(pthread_create(&id, NULL, TaskConnect, &stConnectParam))
- {
- printf("create connect task failed\r\n");
- return ERROR;
- }
- }
复制代码
[Client.c:395]connect 192.168.2.38:3333
[Client.c:341]connect 114.3.204.0:36352 error
在网上找了一下,线程参数要么用全局变量,要么malloc分配空间,这样的话就没有上面的问题
问题
1、为什么这里局部变量地址不能这样用,我平时在普通的函数调用都是这样用?
2、我在test函数和TaskConnect函数中将传入参数&stConnectParam和pstParam的地址打印出来了
是一样的,难道pthread_create时将该局部变量的地址篡改了?
[ 本帖最后由 天冷就回来 于 2007-4-27 13:03 编辑 ] |
|