- 论坛徽章:
- 0
|
省略头文件和宏
void thread()
{
int recvbytes,client_fd;
char buf[MAXDATASIZE];
if((recvbytes=recv(client_fd,buf,MAXDATASIZE,0))<0){
perror("recv" ;
exit(1);
}
printf("recveived a connection:%s\n",buf);
pthread_exit(0);
}
int main()
{
struct sockaddr_in server_sockaddr,client_sockaddr;
int sin_size,recvbytes;
int sockfd,client_fd;
char buf[MAXDATASIZE];
int m,i;
pthread_t l;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0){
perror("error" ;
exit(1);
}
printf("socket success=%d\n",sockfd);
server_sockaddr.sin_family=AF_INET;
server_sockaddr.sin_port=htons(SERVPORT);
server_sockaddr.sin_addr.s_addr=INADDR_ANY;
bzero(&(server_sockaddr.sin_zero), ;
if(bind(sockfd,(struct sockaddr *)&server_sockaddr,sizeof(struct sockaddr))<0){
perror("error" ;
exit(1);
}
printf("bin success\n" ;
if(listen(sockfd,BACKLOG)<0){
perror("error" ;
exit(1);
}
printf("listening....\n" ;
loop: if((client_fd=accept(sockfd,(struct sockaddr *)&client_sockaddr,&sin_size))<0){
perror(" eroor" ;
exit(1);
}
if(i<11){
if(pthread_create(&l,NULL,(void*)thread,NULL)>0){
printf("create success\n" ;
exit(0);
i++;
}
}
else{
printf("error" ;
exit(1);
}
goto loop;
close(sockfd);
}
能通过编译,好像pthread_create用的不对,指点一下吧 |
|