日期:2014-05-16  浏览次数:20770 次

我写的一个关于线程的程序,为什么编译通不过?
源程序:
[code=C/C++][/code#include"my_head.h"

#define MAXLINE 1024

struct arg//thread arguments
{
FILE *fp;
int sockfd;
};
void *read_sock(void *thread_arg);
int get_opt(int argc,char **argv);
int client_handle(FILE *file,int sock);

char port[10];//global variables
char ip_addr[30];


int main(int argc,char **argv)
{
int sock;
struct sockaddr_in server;
int len = sizeof(server);
FILE *file;
file = stdin;

get_opt(argc,argv);

bzero(&server,len);

sock = Socket(AF_INET,SOCK_STREAM,0);

server.sin_family = AF_INET;
server.sin_port = htons(atoi(port));
server.sin_addr.s_addr = inet_addr(ip_addr);

Connect(sock,(struct sockaddr *)&server,len);
client_handle(file,sock);

return(0);
}

int get_opt(int argc,char **argv)//get arguments
{
int opt;

bzero(port,sizeof(port));
bzero(ip_addr,sizeof(ip_addr));

if( argc < 4 )
{
printf("argc error\n");
exit(1);
}

while(( opt = getopt(argc,argv,"p:i:")) != -1 )
{
if( opt == 'p' )
strcpy(port,optarg);
else if( opt == 'i' )
strcpy(ip_addr,optarg);
else
{
printf("getopt error\n");
exit(1);
}
}
}

int client_handle(FILE *file,int sock)
{
char send_msg[MAXLINE];
pthread_t thread_id;
struct arg thread_arg;

bzero(send_msg,MAXLINE);

thread_arg.fp = file;
thread_arg.sockfd = sock;

printf("debug1\n");//debug
pthread_create(&thread_id,NULL,read_sock,(void *)&thread_arg);//
printf("debug2\n");//debug

while( fgets(send_msg,MAXLINE,file) != NULL )
{
Send(sock,send_msg,strlen(send_msg),0);
bzero(send_msg,MAXLINE);
}
if( ferror(file) )
{
printf("fgets error\n");
exit(1);
}
exit(0);
}

void *read_sock(void *thread_arg)
{
char recv_msg[MAXLINE];
bzero(recv_msg,MAXLINE);

while( Recv((struct arg *)thread_arg->sockfd,recv_msg,MAXLINE,0) > 0 )
{
fputs(recv_msg,(struct arg *)thread_arg->fp);
bzero(recv_msg,MAXLINE);
}
return(NULL);
}]
错误提示:function ‘read_sock’:
client.c:102: warning: dereferencing ‘void *’ pointer
client.c:102: error: request for member ‘sockfd’ in something not a structure or union
client.c:104: warning: dereferencing ‘void *’ pointer
client.c:104: error: request for member ‘fp’ in something not a structure or union

应该是参数的传递有问题。

------解决方案--------------------
(struct arg *)thread_arg 加括号