Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux 網路程式設計兩大經典錯誤:ENOTSOCK 與 ECONNREFUSED 成因與修正

編輯:Linux 教學

網路程式開發中常遇到的兩個錯誤碼分析:

錯誤 88:ENOTSOCK — Socket operation on non-socket

成因:socket() 回傳值判斷時少寫括號,導致賦值與比較優先序錯誤。
錯誤碼:

if (sockfd = socket(AF_INET, SOCK_STREAM, 0) < 0) { ... }

< 優先序高於 =,導致 sockfd 被賦值為布林結果 (0/1) 而非 socket fd。
正確碼:

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { ... }

錯誤 111:ECONNREFUSED — Connection refused

成因:監聽埠號設為 13 (daytime 服務),屬於特權埠 (<1024),非 root 執行將被拒絕。
解決:將埠號改為 >1024 的非特權埠(如 8080、9999)。

Copyright © Windows教程網 All Rights Reserved