2015-10-28 58 views
-1

我有一個UDP服務器實現,我在哪裏得到分段錯誤recvfrom調用。recvfrom調用分段錯誤

#define SIZEOF  sizeof 
#define PKTSIZE 65535 

char tmp_buf[PKTSIZE]; 
struct sockaddr_storage tmp_from; 
int tmp_fromlen = 0; 
int tmp_bytes; 
tmp_bytes = truncate_size_t_to_int(recvfrom(fd, tmp_buf, 
         SIZEOF(tmp_buf), 0, 
         (struct sockaddr *) &tmp_from, 
         (socklen_t *) &tmp_fromlen)); 

在此之前,綁定和連接都成功。它是一個單線程進程。 這個recvfrom調用有什麼問題。 我需要爲recvfromSIGALRM信號處理嗎? truncate_size_t_to_int用於size_t爲int。

+1

請分享一些更多的代碼。 – Haris

+1

什麼是'tmp_buf'? 「SIZEOF」做什麼? –

+0

char tmp_buf [PKTSIZE]; #define SIZEOF sizeof – agnel

回答

0

recvfrom()可以返回-1。如果不將recv()/recvfrom()/recvmsg()的結果存儲到一個變量中,您不能寫出正確的網絡代碼,測試它爲-1,在TCP中測試它爲零,並且只有在正確的情況下才能繼續執行假設它的代碼。