当前位置: 技术问答>linux和unix
子线程只运行了一次,不知道怎么给阻塞了
来源: 互联网 发布时间:2015-11-26
本文导语: 这是客户端程序 oid _client(int sockfd,struct sockaddr *pservaddr,socklen_t servlen) { socklen_t len; int n; char sendline[MAXLINE],recvline[MAXLINE+1]; sendline[0]='t'; sendline[0]='e'; sendline[0]='s'; sendline[0]='t'; /*杩炴帴涓绘満*/ if(connect(sockf...
这是客户端程序
oid _client(int sockfd,struct sockaddr *pservaddr,socklen_t servlen)
{ socklen_t len;
int n;
char sendline[MAXLINE],recvline[MAXLINE+1];
sendline[0]='t';
sendline[0]='e';
sendline[0]='s';
sendline[0]='t';
/*杩炴帴涓绘満*/
if(connect(sockfd,(struct sockaddr*)pservaddr,servlen)==-1)
{
perror("connect error!!!!!!!!!!1");
exit(1);
}
while(1)
{
printf("this is the sec thread!!!!!!!n");
len=servlen;
/*鍙戦€佷俊鎭嚦鍙鎴风 */
sendto(sockfd,sendline,MAXLINE,0,pservaddr,&len);
/*绛夊緟瀹㈡埛绔俊鎭?*/
n=recvfrom(sockfd,recvline,MAXLINE+1,0,pservaddr,&len);
printf("this is the sec thread !!!!!!!!!1n");
}
}
int thread()
{
printf("this is the sec thread !!!!!!!!!1n");
int sockfd;
struct sockaddr_in servaddr;
/* check args
if(argc != 2)
{
printf("usage: udpclient n");
exit(1);
}*/
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);
servaddr.sin_addr.s_addr=htonl("127.0.0.1");
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
_client(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
return 0;
}
int main()
{
pthread_t id;
int i ,ret;
ret =pthread_create(&id,NULL,(int*)thread,NULL);
if(ret!=0)
{
printf("error!!!!!!!!!!!!!!!!!!!n");
exit(1);
}
for(;;)
{
printf("this is zhe main client!!!!!!!!!! thread!!!!!!n");
sleep(2);
}
pthread_join(id,NULL);
return 0;
}
这是服务端程序
void _echo(int sockfd,struct sockaddr *pcliaddr,socklen_t clilen)
{
int n;
socklen_t len;
char message[MAXLINE];
while(1)
{
printf("this is the sec thread!!!!!!!n");
len=clilen;
/*绛夊緟瀹㈡埛绔俊鎭?*/
n=recvfrom(sockfd,message,MAXLINE,0,pcliaddr,&len);
/*鍙戦€佷俊鎭嚦鍙鎴风 */
sendto(sockfd,message,n,0,pcliaddr,&len);
}
}
int thread(void)
{
printf("this is the sec thread!!!!!!!n");
int sockfd;
struct sockaddr_in servaddr,cliaddr;
sockfd=socket(AF_INET,SOCK_DGRAM,0);/*create a socket*/
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl("127.0.0.1");
servaddr.sin_port=htons(SERV_PORT);
/*澧炲己鍋ュ.鎬?,鍙?涓嶈 */
if(bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr))==-1)
{
perror("bind error!!!!!");
exit(1);
}
_echo(sockfd,(struct sockaddr *)&cliaddr,sizeof(cliaddr));
return 0;
}
int main()
{
pthread_t id;
int i ,ret;
ret =pthread_create(&id,NULL,(int*)thread,NULL);
if(ret!=0)
{
printf("error!!!!!!!!!!!!!!!!!!!n");
exit(1);
}
for(;;)
{
printf("this is the serv!!!!!!!!! main thread!!!!!!!!!n");
sleep(2);
}
pthread_join(id,NULL);
return 0;
}
不知道为什么,这两边的thread只运行了一次后,就再也不动了,我试了好多次,有一次运行了2次就,想来应该是被阻塞了,可又不知道原因,请高手帮忙看看
oid _client(int sockfd,struct sockaddr *pservaddr,socklen_t servlen)
{ socklen_t len;
int n;
char sendline[MAXLINE],recvline[MAXLINE+1];
sendline[0]='t';
sendline[0]='e';
sendline[0]='s';
sendline[0]='t';
/*杩炴帴涓绘満*/
if(connect(sockfd,(struct sockaddr*)pservaddr,servlen)==-1)
{
perror("connect error!!!!!!!!!!1");
exit(1);
}
while(1)
{
printf("this is the sec thread!!!!!!!n");
len=servlen;
/*鍙戦€佷俊鎭嚦鍙鎴风 */
sendto(sockfd,sendline,MAXLINE,0,pservaddr,&len);
/*绛夊緟瀹㈡埛绔俊鎭?*/
n=recvfrom(sockfd,recvline,MAXLINE+1,0,pservaddr,&len);
printf("this is the sec thread !!!!!!!!!1n");
}
}
int thread()
{
printf("this is the sec thread !!!!!!!!!1n");
int sockfd;
struct sockaddr_in servaddr;
/* check args
if(argc != 2)
{
printf("usage: udpclient n");
exit(1);
}*/
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);
servaddr.sin_addr.s_addr=htonl("127.0.0.1");
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
_client(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
return 0;
}
int main()
{
pthread_t id;
int i ,ret;
ret =pthread_create(&id,NULL,(int*)thread,NULL);
if(ret!=0)
{
printf("error!!!!!!!!!!!!!!!!!!!n");
exit(1);
}
for(;;)
{
printf("this is zhe main client!!!!!!!!!! thread!!!!!!n");
sleep(2);
}
pthread_join(id,NULL);
return 0;
}
这是服务端程序
void _echo(int sockfd,struct sockaddr *pcliaddr,socklen_t clilen)
{
int n;
socklen_t len;
char message[MAXLINE];
while(1)
{
printf("this is the sec thread!!!!!!!n");
len=clilen;
/*绛夊緟瀹㈡埛绔俊鎭?*/
n=recvfrom(sockfd,message,MAXLINE,0,pcliaddr,&len);
/*鍙戦€佷俊鎭嚦鍙鎴风 */
sendto(sockfd,message,n,0,pcliaddr,&len);
}
}
int thread(void)
{
printf("this is the sec thread!!!!!!!n");
int sockfd;
struct sockaddr_in servaddr,cliaddr;
sockfd=socket(AF_INET,SOCK_DGRAM,0);/*create a socket*/
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl("127.0.0.1");
servaddr.sin_port=htons(SERV_PORT);
/*澧炲己鍋ュ.鎬?,鍙?涓嶈 */
if(bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr))==-1)
{
perror("bind error!!!!!");
exit(1);
}
_echo(sockfd,(struct sockaddr *)&cliaddr,sizeof(cliaddr));
return 0;
}
int main()
{
pthread_t id;
int i ,ret;
ret =pthread_create(&id,NULL,(int*)thread,NULL);
if(ret!=0)
{
printf("error!!!!!!!!!!!!!!!!!!!n");
exit(1);
}
for(;;)
{
printf("this is the serv!!!!!!!!! main thread!!!!!!!!!n");
sleep(2);
}
pthread_join(id,NULL);
return 0;
}
不知道为什么,这两边的thread只运行了一次后,就再也不动了,我试了好多次,有一次运行了2次就,想来应该是被阻塞了,可又不知道原因,请高手帮忙看看
|
togudulyn(冰楠)
uint32_t htonl(uint32_t hostlong);
The htonl() function converts the unsigned integer hostlong from host byte order to network byte order.
这是从man copy过来的,建议新人要学会用工具先
uint32_t htonl(uint32_t hostlong);
The htonl() function converts the unsigned integer hostlong from host byte order to network byte order.
这是从man copy过来的,建议新人要学会用工具先
|
兄弟,你的代码应该编译都有问题,
象servaddr.sin_addr.s_addr=htonl("127.0.0.1");
就肯定会有问题吧
象servaddr.sin_addr.s_addr=htonl("127.0.0.1");
就肯定会有问题吧
|
看你的代码,要是thread()能执行2次才是错误的。