#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 4000
#define BACKLOG 2
char bye[]="exit";
struct
{
int sock_fd;
int client_fd;
pthread_t sen_tidp;
pthread_t rev_tidp;
}pid;
void *rev_msg(void*arg)
{
int *sock_fd;
int num,ok;
char buf[1024];
sock_fd=(int*)arg;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
ok=0;
do
{
num=read(*sock_fd,buf,1023);
buf[num]='\0';
printf("it said: %s\n",buf);
if(strncasecmp(buf,bye,4)==0)
ok=1;
}while(!ok);
sleep(2);
pthread_cancel(pid.sen_tidp);
return (void*)0;
}
void *sent_msg(void *arg)
{
int *sock_fd;
int num,ok;
char buf[1024]="Welcome!";
sock_fd=(int*)arg;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
num=strlen(buf);
write(*sock_fd,buf,num);
ok=0;
do
{
scanf("%s",&buf);
num=strlen(buf);
write(*sock_fd,buf,num);
if(strncasecmp(buf,bye,4)==0)
ok=1;
}while(!ok);
sleep(2);
pthread_cancel(pid.rev_tidp);
return (void*)0;
}
int main(int argc, char *argv[])
{
int sock_fd,client_fd;
int num;
struct sockaddr_in serv_addr,client_addr;
pthread_t sen_tidp,rev_tidp;
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(MYPORT);
serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
bzero(&(serv_addr.sin_zero),8);
sock_fd=socket(AF_INET,SOCK_STREAM,0);
bind(sock_fd,(struct sockaddr*)&serv_addr,sizeof(struct sockaddr_in));
listen(sock_fd,BACKLOG);
num=sizeof(struct sockaddr_in);
client_fd=accept(sock_fd,(struct sockaddr*)&client_addr,&num);
pthread_create(&sen_tidp,NULL,sent_msg,(void*)&client_fd);
pthread_create(&rev_tidp,NULL,rev_msg,(void*)&client_fd);
pid.sock_fd=sock_fd;
pid.client_fd=client_fd;
pid.sen_tidp=sen_tidp;
pid.rev_tidp=rev_tidp;
pthread_join(sen_tidp,NULL);
pthread_join(rev_tidp,NULL);
close(client_fd);
close(sock_fd);
exit(0);
}
客户端:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERV_PORT 4000
volatile pthread_t rev_tidp,sen_tidp;
char bye[]="exit";
void *sen_msg(void*arg)
{
int *sock_fd;
int num;
char buf[1024];
sock_fd=(int*)arg;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
sleep(2);
do
{
scanf("%s",&buf);
num=strlen(buf);
write(*sock_fd,buf,num);
}while(strncasecmp(buf,bye,4)!=0);
pthread_cancel(rev_tidp);
pthread_exit((void *)0);
}
void *rev_msg(void * arg)
{
int *sock_fd;
int num;
char buf[1024];
sock_fd=(int *)arg;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
do
{
num=read(*sock_fd,buf,1023);
buf[num]='\0';
printf("it said: %s\n",buf);
}while(strncasecmp(buf,bye,4)!=0);
sleep(2);
pthread_cancel(sen_tidp);
pthread_exit((void *)0);
}
int main(int argc, char *argv[])
{
int sock_fd;
int nbyte,nbyte1,num;
struct sockaddr_in serv_addr;
struct hostent *host;
host=gethostbyname("localhost");
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(SERV_PORT);
serv_addr.sin_addr=*((struct in_addr *)host->h_addr);
bzero(&(serv_addr.sin_zero),8);
sock_fd=socket(AF_INET,SOCK_STREAM,0);
connect(sock_fd,(struct sockaddr*)&serv_addr,sizeof(struct sockaddr_in));
pthread_create(&rev_tidp,NULL,rev_msg,(void*)&sock_fd);
pthread_create(&sen_tidp,NULL,sen_msg,(void*)&sock_fd);
pthread_join(rev_tidp,NULL);
sleep(2);
pthread_join(sen_tidp,NULL);
close(sock_fd);
exit(0);
}
为什么在退出的时候总是有一个程序无法正常退出?
mg271603433 于 2011-01-05 17:44:18发表:
路过,看看
kongrong1 于 2010-12-15 10:32:31发表:
可以用_exit(0);来解决,exit()退出时需要会执行清空缓冲区的行为,但_exit()没有,是强制退出----但我不知道这样是否对整个程序有坏处。
kongrong1 于 2010-12-14 16:05:25发表:
找出原因了,是scanf函数的原因。线程退出后会对SCANF的缓冲区进行处理,而缓冲区没有清理完成造成的等待。但我不知道该怎么更改。望高手指教!
kongrong1 于 2010-12-14 13:26:57发表:
有没有高手帮我解决一下,我怀疑是线程退出方面的问题。
velcbo 于 2010-12-13 20:38:21发表:
不懂,帮顶