当前位置: 代码迷 >> C语言 >> [求助]关于C语言多线程内存释放的问题
  详细解决方案

[求助]关于C语言多线程内存释放的问题

热度:541   发布时间:2007-10-27 10:05:02.0
[求助]关于C语言多线程内存释放的问题

这是小弟最近编的一个LINUX下的C语言聊天室程序,刚开始运行的时候没问题,但运行后直接关闭程序的时候,下次在执行的时候就不能正常执行了,初步估计是多线程的地方内存没管理好,那位大虾如果看出什么问题,帮帮忙啊!!!
服务器端:
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
/*#include <sys/un.h>
*#include <unistd.h> */
#include <netinet/in.h>
#include <pthread.h>


int n=0,iResult;
int client_sockfd[5];


void send_thread(int c_scoket)
{
char BUFFERREC[100], BUFFERSEND[100];
memset(BUFFERREC,0,100);
memset(BUFFERSEND,0,100);

printf("thread%d\n",n);
n++;
printf("c_scoket = %d\n",c_scoket);

char c_name[10];
read(c_scoket,BUFFERREC,100);
strcpy(c_name,BUFFERREC);
printf("%s is coming\n",c_name);
char say_hello[]="ok,you can chat with others!";
write(c_scoket,say_hello,strlen(say_hello));

memset(BUFFERREC,0,100);

while(1)
{


read(c_scoket,BUFFERREC,100);

if(strcmp(BUFFERREC,"q")==0)
{
printf("%s is leaving\n",c_name);
close(c_scoket);
pthread_exit(NULL);

}
strcat(BUFFERSEND,c_name);
strcat(BUFFERSEND," say :");
strcat(BUFFERSEND,BUFFERREC);

int k=0;
for (;k<5;k++)
{
if (client_sockfd[k]!=0)
{
printf("for xun huan %d\n",k);

iResult = write(client_sockfd[k],BUFFERSEND,100);
if (iResult < 0 )
{ printf("send failed: %d\n");
close(client_sockfd[k]);

}


}
}
memset(BUFFERREC,0,100);
memset(BUFFERSEND,0,100);

}
pthread_exit(NULL);
}

int main()
{
//int server_sockfd,client_sockfd[5];
int server_sockfd;
int server_len,client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
unlink("server_socket");
/*如果存在同名的套接口,则先删除*/
bzero(&server_address,sizeof(server_address));
server_sockfd=socket(AF_INET,SOCK_STREAM,0);
server_address.sin_family=AF_INET;
server_address.sin_port=7000;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
if ( server_address.sin_addr.s_addr == INADDR_NONE )
abort(); /* Failed */

server_len=sizeof(server_address);
bind(server_sockfd,(struct sockaddr *)&server_address,server_len);

listen(server_sockfd,5);
/*以上创建监听队列.等待用户的连接请求*/
pthread_t id_send,id_recv;
int ret;
pthread_attr_t attr;


while(1){


int i=0;
for(;i<5;i++){
if(client_sockfd[i]==0)

{
client_sockfd[i]=accept(server_sockfd,(struct sockaddr *)&client_address,&client_len);


printf("server ok, client %d come\n",i+1);
/*以上接受一个客户的请求*/

printf("client_socket %d = %d\n",i,client_sockfd[i]);


printf("is ok ?\n");

// ret=pthread_create(&id_recv,&attr,(void *) recv_thread,client_sockfd[i]);
// if(ret!=0){
// printf ("Create receive pthread error!\n");
// exit (1);
// }

/*初始化属性值,绑定的和PTHREAD_SCOPE_PROCESS非绑定的*/
pthread_attr_init(&attr);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);

ret=pthread_create(&id_send,&attr,(void *) send_thread,client_sockfd[i]);
if(ret!=0){
printf ("Create send pthread error!\n");
exit (1);
}
}//if end
}//for end
}//while end

pthread_join(id_send,NULL);
return 0;
}



客户端:
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
//#include <sys/un.h>
//#include <unistd.h>
#include <netinet/in.h>
#include <pthread.h>

char ch[100],buffer[100];

void thread(int sockfd)
{
while(1){
read(sockfd,ch,100);
printf("%s\n",ch);

if(strcmp(ch,"q")==0)break;
memset(ch,0,100);
}
pthread_exit(NULL);
}

int main()
{
int sockfd;
int len;
struct sockaddr_in address;
int result;
bzero(&address,sizeof(address));
sockfd=socket(AF_INET,SOCK_STREAM,0);
address.sin_family=AF_INET;
address.sin_port=7000;

char ip_s[50];
printf("shu ru ip:\n");
scanf("%s",ip_s);
address.sin_addr.s_addr=inet_addr(ip_s);


len=sizeof(address);
/*以上创建服务器套接口的地址,其中包括套接口类型,名称*/
result=connect(sockfd,(struct sockaddr *)&address,len);

if(result==-1){
perror("oops:client1");
exit(1);

}
printf("input your chat name :\n");
scanf("%s",buffer);
write(sockfd,buffer,100);
read(sockfd,ch,100);
printf("%s\n",ch);

memset(buffer,0,100);
memset(ch,0,100);


pthread_t id;
int ret;
pthread_attr_t attr;

pthread_attr_init(&attr);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);


ret=pthread_create(&id,&attr,(void *) thread,sockfd);
if(ret!=0){
printf ("Create pthread error!\n");
exit (1);
}

搜索更多相关的解决方案: C语言  内存  线程  include  服务器  

----------------解决方案--------------------------------------------------------
不知道你报的是什么错误啊~
----------------解决方案--------------------------------------------------------
end failed: -1209844156
for xun huan 0
send failed: -1209844156
for xun huan 0
send failed: -1209844156
for xun huan 0
send failed: -1209844156
for xun huan 0
send failed: -1209844156
for xun huan 0
send failed: -1209844156
for xun huan 0
send failed: -1209844156
for xun huan 0
send failed: -1209844156
for xun huan 0
send failed: -1209844156


怎么会出现死循环呢



----------------解决方案--------------------------------------------------------
  相关解决方案