| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1074 人关注过本帖
标题:[求助]关于C语言多线程内存释放的问题
只看楼主 加入收藏
计院人
Rank: 1
等 级:新手上路
帖 子:83
专家分:0
注 册:2006-10-15
收藏
 问题点数:0 回复次数:2 
[求助]关于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 服务器 
2007-10-27 10:05
zzjking
Rank: 1
等 级:新手上路
帖 子:69
专家分:0
注 册:2007-7-16
收藏
得分:0 
不知道你报的是什么错误啊~

2007-10-31 13:20
boot2046
Rank: 2
等 级:新手上路
威 望:3
帖 子:168
专家分:0
注 册:2007-7-23
收藏
得分:0 
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


怎么会出现死循环呢



Linux是简单的,你不需要成为天才也能理解这种简单,Windows是复杂的,就算你是天才也不能理解这种复杂
2007-10-31 14:59
快速回复:[求助]关于C语言多线程内存释放的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015818 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved