用线程实现的一个C/S通信,总有问题啊。大虾进来看看~~
我在线程函数pthread_fun(...)调用了一个写文件的函数saveClientInfo(...).
void saveClientInfo(struct sockaddr_in cli_addr,int y,int x,time_t now) //save the info to file
{
FILE * fp;
struct tm * tnow; //for local time
char * ip; //client ip
char * port; //client port
char * nbuf;
ip=inet_ntoa(cli_addr.sin_addr);
sprintf(port,"%d",cli_addr.sin_port);
tnow=localtime(&now); // local time
if((fp=fopen("cinfo.txt","w"))==NULL)
{
printf("file open error!\n");
exit(1);
}
fputs(ip,fp); //save the info
fputc('\t',fp);
fputs(port,fp);
fputc('\t',fp);
fputs(asctime(tnow),fp);
fputc('\t',fp);
sprintf(nbuf,"%d",y); ///exit
printf("y.nbuf = %s\n",nbuf);
fputs(nbuf,fp);
fputc('\t',fp);
sprintf(nbuf,"%d",x);
fputs(nbuf,fp);fputc('\n',fp);
fclose(fp); //close the client socket
}
每次server端运行到///exit 时就会退出,client端一直是正常的。
可能是什么原因啊?大虾帮帮忙,搞了好长时间,要哭了~~