請問:使用fdopen轉換socket描述子的問題 |
缺席
|
nady
一般會員 發表:8 回覆:6 積分:2 註冊:2002-11-12 發送簡訊給我 |
各位大大您好
因為我想要寫一個Http Server的程式,
需要經常性處理socket收送的字串,所以想要將socket轉換成FILE檔案描述子。
利用fprintf來處理資料。
我參考了網路上前輩的程式,在linux上的程式是可行的。
可是轉換到Windows(BCB)就不可以了。
出現問題在於fdopen回傳都是NULL。
追查errno,好像是"No Such File or Directory"。
可是accept傳回的newsockfd是可以send和recv的。
只是送入fdopen就會失敗。
可否請高手們幫幫忙,指點一下。
謝謝嚕! 程式碼片段如下:
<textarea class="cpp" rows="10" cols="60" name="code">
while (1)
{
int s;
FILE *fin;
FILE *fout; newsockfd = accept(sockfd,(struct sockaddr *)&addr,&addr_len);
if (newsockfd < 0) break; fin = fdopen(newsockfd , "r");
fout = fdopen(newsockfd , "w"); process(fin,fout); fflush(fout);
fclose(fin);
fclose(fout);
}
</textarea>
|
nady
一般會員 發表:8 回覆:6 積分:2 註冊:2002-11-12 發送簡訊給我 |
不好意思,這個問題後來我覺得應該是因為socket在linux和windows是不一樣的吧!
因為在網路上有位前輩說的: Ah, as far as I know, one of the big limitations of winsock is that the SOCKET type is *not* equivalent to file descriptor unlike that using BSD or POSIX sockets. That's one of the major reasons for using a separate data type, SOCKET, as opposed to int, typical type of a file descriptor. This implies that you cannot mix SOCKETs and stdio, sorry. This is the case when you use -mno-cygwin. 所以我找到另外一個前輩的程式碼,片段如下:代表他也是刻意避開在windows下使用fdopen轉換! <textarea class="cpp" rows="10" cols="60" name="code"> #ifdef WIN32 #include 不好意思浪費網站的資源。 nady |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |