Linux下怎么在send的时候,Bad file descriptor
Linux\u4e0b\u600e\u4e48\u5728send\u7684\u65f6\u5019\uff0cBad file descriptor\u56e0\u4e3a\u662f\u4ecewindows \u79fb\u690d\u5230 Linux\uff0c\u800c\u4e14\u7528\u5230libevent\u5e93\uff0c\u5f88\u591a\u4e1c\u897f\u90fd\u8981\u6539\u5176\u4e2d\u6709\u8fd9\u4e48\u4e00\u6761\uff1aevutil_socketpair(AF_INET,SOCK_STREAM,0,fd);\u5728Linux\u4e0b\u5982\u679c\u8fd0\u884c send(fd[0],"",1,0)\u603b\u662f\u8fd4\u56de-1\uff0cperror()\uff0c\u8fd4\u56deBad filedescriptor\u7f51\u4e0a\u67e5\u4e86\u4e00\u4e0b\uff1a\u53ef\u80fd\u662f\u56e0\u4e3a\u6587\u4ef6\u63cf\u8ff0\u7b26\u9519\u8bef\uff0c\u6216\u8005\u5173\u95ed\u7b49\u539f\u56e0\u4e8e\u662f\u53bb\u67e5libevent\u7684utils.h\u5934\u6587\u4ef6\u5728Linux\u4e0bevutil_sockepair\uff08\uff09\u5b9e\u9645\u8c03\u7528\u7684\u662fLinux\u7cfb\u7edf\u51fd\u6570socketpair\uff08\uff09\u53c8\u53bbLinux man socketpair\u4eba\u5bb6\u8bf4\u4e86\uff0c\u5728Linux\u4e0bsocketpair\u4f20\u5165\u7684\u53ea\u80fd\u662fAF_UNIX\uff0cOK\uff0c\u627e\u5230\u4e86\u95ee\u9898\u628a\u539f\u6765\u7684evutil_socketpair(AF_INET,SOCK_STREAM,0,fd)\u6539\u4e3aevutil_socketpair(AF_UNIX,SOCK_STREAM,0,fd)\u6b63\u5e38\u8fd0\u884c\u6240\u4ee5\u6392\u9519\u601d\u8def\uff0c\u548c\u67e5\u627e\u8d44\u6599\u662f\u5f88\u91cd\u8981\u7684\u80fd\u529b\u554a\uff01\uff01\uff01
\u4f60\u7684\u9519\u8bef\u662f\uff1a
write file text.txt error !
msg:Bad file descriptor
\u663e\u7136\uff0c\u662f\u6ca1\u5199\u6743\u9650\uff0c\u6539fd = open(fName, O_CREAT | O_APPEND)
\u4e3afd = open(fName, O_CREAT | O_APPEND | O_RDWR) \u5c31\u597d\u4e86
其中有这么一条:
evutil_socketpair(AF_INET,SOCK_STREAM,0,fd);
在Linux下如果运行 send(fd[0],"",1,0)总是返回-1,perror(),返回Bad file descriptor
网上查了一下:可能是因为文件描述符错误,或者关闭等原因
于是去查libevent的utils.h头文件
在Linux下evutil_sockepair()实际调用的是Linux系统函数socketpair()
又去Linux man socketpair
人家说了,在Linux下socketpair传入的只能是AF_UNIX,OK,找到了问题
把原来的evutil_socketpair(AF_INET,SOCK_STREAM,0,fd)改为evutil_socketpair(AF_UNIX,SOCK_STREAM,0,fd)正常运行
所以排错思路,和查找资料是很重要的能力啊!!!
绛旓細SOCK_STREAM,0,fd);鍦Linux涓濡傛灉杩愯 send(fd[0],"",1,0)鎬绘槸杩斿洖-1锛宲error()锛岃繑鍥濨ad filedescriptor缃戜笂鏌ヤ簡涓涓嬶細鍙兘鏄洜涓烘枃浠舵弿杩扮閿欒锛屾垨鑰呭叧闂瓑鍘熷洜浜庢槸鍘绘煡libevent鐨剈tils.h澶存枃浠跺湪Linux涓媏vutil_sockepair锛堬級瀹為檯璋冪敤鐨勬槸Linux绯荤粺鍑芥暟socketpair锛堬級鍙堝幓Linux man socketpair...
绛旓細鍦ㄥ绾跨▼缂栫▼涓鍏朵腑浣跨敤涓涓嚎绋嬫潵accept瑕佽繛鎺ョ殑瀹㈡埛绔傚悓鏃跺湪鎺ュ彈client鐨勮姹備箣鍚庢柊寤轰竴涓嚎绋嬫潵杩涜鍏蜂綋鐨勬搷浣溿傚叾鎿嶄綔鍖呮嫭鍚慶lient绔彂閫佷竴瀹氬瓧鑺傜殑鏁版嵁锛屼娇鐢send()鍑芥暟鏉ヨ繘琛屾搷浣溿傚鏋滃湪鍙戦佽繃绋嬩腑鍑虹幇浠讳綍涓涓猚lient绔殑鏂嚎锛屽垯鏁翠釜绋嬪簭閮戒細閫鍑恒俿size_t send(int sockfd, const void *buff, si...
绛旓細鍑芥暟璇存槑send() 鐢ㄦ潵灏嗘暟鎹敱鎸囧畾鐨 socket 浼犵粰瀵规柟涓绘満銆備娇鐢 send 鏃跺鎺ュ瓧蹇呴』宸茬粡杩炴帴銆俿end 涓嶅寘鍚紶閫佸け璐ョ殑鎻愮ず淇℃伅锛屽鏋滄娴嬪埌鏈湴閿欒灏嗚繑鍥-1銆傚洜姝わ紝濡傛灉send 鎴愬姛杩斿洖锛屽苟涓嶅繀鐒惰〃绀鸿繛鎺ュ彟涓绔殑杩涚▼鎺ユ敹鏁版嵁銆傛墍淇濊瘉鐨勪粎鏄綋send 鎴愬姛杩斿洖鏃讹紝鏁版嵁宸茬粡鏃犻敊璇湴鍙戦佸埌缃戠粶涓娿傚浜庢敮鎸佷负...
绛旓細姝g‘鍐欐硶锛struct sockaddr_in address;
绛旓細1銆乺ecv銆send鍙敤浜庡鎺ュ瓧閫氫俊锛2銆乺ead銆亀rite鏄簳灞傜郴缁熻皟鐢紝鍙鏄枃浠舵搷浣滃氨閮藉彲浠ョ敤锛 姣斿濂楁帴瀛楁搷浣滐紝濂楁帴瀛楁弿杩扮灞炰簬鏄枃浠舵弿杩扮鐨勪竴绉嶏紝濂楁帴瀛楁湰韬湪Linux涓婂氨鍙仛濂楁帴瀛楁枃浠躲傛墍浠ead銆亀rite鍑芥暟涓嶅厜鍙互鐢ㄤ簬濂楁帴瀛楃紪绋嬶紝涔熷彲浠ョ敤浜庤鍙栧叾浠栧悇绉嶆枃浠讹紝姣斿鐢ㄤ簬鏂囦欢缂栫▼璇诲啓鏅氭枃浠躲
绛旓細绯荤粺璋冪敤 send()銆乻endto() 鍜 sendmsg() 鐢ㄤ簬灏嗘秷鎭紶杈撳埌鍙︿竴涓鎺ュ瓧銆備粎褰撳鎺ュ瓧澶勪簬杩炴帴鐘舵佹椂鎵嶅彲浠ヤ娇鐢 send() 璋冪敤锛堜互渚跨煡閬撻鏈熺殑鎺ユ敹鑰咃紝 涔熷氨鏄send()浠呬粎鐢ㄤ簬鏁版嵁娴佺被鍨嬬殑鏁版嵁鍙戦 锛屽浜嶵CP锛屾湇鍔$鍜屽鎴风閮藉彲浠ヤ娇鐢╯end/recv锛涗絾鏄浜嶶DP锛屽彧鑳芥槸瀹㈡埛绔娇鐢╯end/recv锛屾湇鍔$...
绛旓細MSG_DONTWAIT锛氭搷浣滀笉浼氳闃诲銆侻SG_EOR锛氱粓姝竴涓褰曘侻SG_MORE锛氳皟鐢ㄨ呮湁鏇村鐨勬暟鎹渶瑕佸彂閫併侻SG_NOSIGNAL锛氬綋鍙︿竴绔粓姝㈣繛鎺ユ椂锛岃姹傚湪鍩轰簬娴佺殑閿欒濂楁帴瀛椾笂涓嶈鍙戦丼IGPIPE淇″彿銆侻SG_OOB锛氬彂閫乷ut-of-band鏁版嵁(闇瑕佷紭鍏堝鐞嗙殑鏁版嵁)锛屽悓鏃剁幇琛屽崗璁繀椤绘敮鎸佹绉嶆搷浣溿倀o锛氭寚鍚戝瓨鏀炬帴鏀剁鍦板潃鐨勫尯鍩燂紝...
绛旓細Linux鐨recv銆send鍑芥暟鍜宺ead銆亀rite鍑芥暟閮藉彲浠ョ敤浜庡鎺ュ瓧缂栫▼銆備笉鍚岀殑鏄痳ecv銆乻end鍙敤浜庡鎺ュ瓧閫氫俊锛涜宺ead銆亀rite鏄簳灞傜郴缁熻皟鐢紝鍙鏄枃浠舵搷浣滃氨閮藉彲浠ョ敤锛 姣斿濂楁帴瀛楁搷浣滐紝濂楁帴瀛楁弿杩扮灞炰簬鏄枃浠舵弿杩扮鐨勪竴绉嶏紝
绛旓細1) send鍏堟瘮杈冨彂閫佹暟鎹殑闀垮害nbytes鍜屽鎺ュ瓧sockfd鐨勫彂閫佺紦鍐插尯鐨勯暱搴︼紝濡傛灉nbytes > 濂楁帴瀛梥ockfd鐨勫彂閫佺紦鍐插尯鐨勯暱搴, 璇ュ嚱鏁拌繑鍥濻OCKET_ERROR;2) 濡傛灉nbtyes <= 濂楁帴瀛梥ockfd鐨勫彂閫佺紦鍐插尯鐨勯暱搴︼紝閭d箞send鍏堟鏌ュ崗璁槸鍚︽鍦ㄥ彂閫乻ockfd鐨勫彂閫佺紦鍐插尯涓殑鏁版嵁锛屽鏋滄槸灏辩瓑寰呭崗璁妸鏁版嵁鍙戦佸畬锛屽鏋滃崗璁繕...
绛旓細鍦ㄩ潪闃诲妯″紡涓鎬庝箞鐭ラ亾recv鎺ユ敹鏁版嵁瀹屾垚锛熶互linux涓tcpsocket缂栫▼涓轰緥锛氶樆濉炲氨鏄痳ecv/read鐨勬椂鍊socket鎺ユ敹缂撳啿鍖鸿鏄湁鏁版嵁灏辫锛屾病鏁版嵁鎴戝氨涓鐩寸潯瑙夎禆鐫涓嶈蛋锛岀洿鍒版湁鏁版嵁鏉ヤ簡璇诲畬鎴戞墠璧般send/write鐨勬椂鍊欙紝瑕佹槸鍙戦佺紦鍐插尯婊′簡锛屾病鏈夌┖闂寸户缁彂閫佷簡鎴戜篃涓鐩寸潯瑙夎禆鐫涓嶈蛋锛岀洿鍒板彂閫佺紦鍐插尯鑵惧嚭瓒冲鐨勭┖闂...