23,125
社区成员
发帖
与我相关
我的任务
分享
void *thread_server(void *arg)
{
int Readres,Writeres;
bool IsConnection = true;
int sockfd;
sockfd = client_sockfd[(int)arg];
char buf[BUFNUM];
printf("enter subthread: %d -- %d\n",(int)arg,sockfd);
while(IsConnection)
{
memset(buf, '\0', sizeof(buf));
/*读取客户端发送的数据*/
Readres = read(sockfd, buf, sizeof(buf));
if(Readres == 0)
{
printf("read() return 0 ! this thread will exit! --from %d -- %d\n",(int)arg,sockfd);
IsConnection = false;
}
else if(Readres < 0)
{
printf("read() return ERROR:%d ==> %s! this thread will exit!\n",strerror(errno));
IsConnection = false;
}
else
{
printf("The Client say %d char: %s --from %d--%d\n",Readres,buf,(int)arg,sockfd);
/*处理客户端的数据*/
DataProcess(buf);
/*向客户端发送数据*/
//Writeres = write(sockfd, &(buf[BUFNUM]), sizeof(buf[BUFNUM]));
IsConnection = true;
}
}
close(sockfd);
pthread_exit(NULL);
}