closedir에관한 질문입니다.
글쓴이: liebeym / 작성시간: 화, 2004/11/09 - 2:19오후
pthread를 이용해서 네트워크 프로그램을 만들고 있습니다.
아래 코드는 내부에 지정한 directory 를 읽어서 vectror contatiner로 저장하고 있습니다.
Receive에 관한건 callback형식으로 만들었습니다.
문제는 "" closedir()""을 부르면 Receive함수를 부르지 못하고 그냥 끝나 버립니다.
그래서 "" closedir()"을 제거하니 Receive함수가 불립니다.
이것이 어떤 signal을 발생시켜 막는걸까요..
여러분의 조언을 구합니다.
1. 내부에 지정한 디렉토리를 읽어서 Vector container로 저장
void CFileManager::getSharedFileInfo() 39 { 40 struct dirent *dirp; 41 int nsize = 0; 42 DIR *dp; 43 44 // 공유 디렉터리를 연다 45 if( (dp = opendir(m_strshareddir)) == NULL) 46 { 47 fputs("shared dir open error\n", stderr); 48 return ; 49 } 50 51 // 디렉터리에 있는 내용을 읽어서 vector에 집어 넣는다. 52 while((dirp = readdir(dp)) != NULL) 53 { 54 nsize = 0; 55 56 // 디렉터리가 아니고 일반 파일이 인지 검사, 파일 용량도 가지고 옴 57 if(checkFile(1, dirp->d_name, &nsize) < 0) 58 { 59 continue; 60 } 58 { 59 continue; 60 } 61 62 // File 정보를 저장할 구조체를 선언하여 해당 값을 셋팅 한다 63 struct file_info *file = (struct file_info*)malloc(sizeof(file_info)); 64 file->strfname = strdup(dirp->d_name); 65 file->strexpand = tokenfile(dirp->d_name); 66 file->strdir = strdup(m_strshareddir); 67 file->nfsize = nsize; 68 69 m_asharedfiles.push_back(file); 70 } // end while 71 //여기서 Closedir을 부르면 문제가 있습니다. 72 if(closedir(dp) <0 ) 73 fprintf(stdout,"Can'tclosedirecotry%s\n",m_strshareddir); 75 76 77 78 }
위에 코드를 부르는곳
void CkMuleClient::DirectoryInfo(void) 146 { 147 fileinfo->getSharedFileInfo(); /* shared directory 정보를 읽어옮 */ 148 149 /* 현재 Client의 공유파일 개수를 저장 */ 150 m_CurShareFilecount = fileinfo->m_asharedfiles.size(); 151 }
실제 메인에서 쓰이는 코드
CkMuleClient theApp; theApp.fileinfo = new CFileManager(theApp.prefer->M_SHAREDDIR, theApp.prefer->M_DOWNDIR,NULL); theApp.DirectoryInfo(); theApp.Socketinit(port); /* Socket 초기화 */ theApp.SendPacket(..........); /*Packet 을 보냄 */ /* Pakcet을 받길 기다림 */
2. 이함수가 호출됨으로서 callback함수를 부른다.
124 int CkMuleClient::SocketInit( uint32 UDPport) 125 { 126 //UDP server socket생성 127 m_udp_sock= new CUDPSocket( UDPport ); 128 m_udp_sock->InitClient(); 129 m_udp_sock->SetCallBack(this); 130 return 0; 131 } void CUDPSocket:: SetCallBack(CAbstractApp *call) 134 { 135 m_theApp= call; 136 } 137
pthread에서 create할때 넣어주는 function pointer
140 { ......... 141 str_len= recvfrom(sock, BUFF, BUFFSIZE, 0, (struct sockaddr *)&(temp_addr), &clnt_addr_size); 142 143 if( str_len> 0){ 144 145 fprintf(stderr, "패킷 수신!!!n"); 146 //패킷이 수신된 경우이므로 할일을 하면된다. 147 148 fprintf(stderr, "콜백 호출전!!!n"); [color=red] //여기서 callback함수를 부른다. 149 sock_obj->m_theApp->OnReceive(str_len, BUFF, temp_addr); 150 ............. [/color] }
위서서 호출되어지는 main class에 있는 callback 함수 139 virtual bool OnReceive(unsigned int size, char *rdata, struct sockaddr_in);
Forums:
댓글 달기