[問題] 關於socket的binding()
不好意思 PO了一篇很蠢的文
可是找了一陣子,目前找不到問題@@
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
我在linux平台底下用GCC compile
問題(Question):
我在建立完socket後,要把socket bind起來的時候,發現好像執行失敗,
然後參考網路上的code好像差不多@@,而且compile有通過了,但輸出結果是
bind: Socket operation on non-socket,我個人覺得socket應該有建立成功吧?!
好像就bind產生問題了,但一直不知道是什麼問題,然後剛剛爬文看到可能是port
使用者無法開啟,也用root執行過,想問程式的問題在哪裡,非常感謝
底下是我的code (想說沒很長,就放上來了,希望不會違反版規)
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
int main ()
{
int fd;
if (fd= socket (AF_INET,SOCK_STREAM,0 )<0) {
perror("socket");
exit(1);
}
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port=htons(9780);
server.sin_addr.s_addr =htonl (INADDR_ANY);
if ( bind(fd,(struct sockaddr*)&server, sizeof(server)) < 0)
{
perror ("bind");
exit (1);
}
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
※ 編輯: P568912 來自: 140.112.94.90 (04/24 17:23)
推
04/24 17:48, , 1F
04/24 17:48, 1F
※ 編輯: P568912 來自: 140.112.94.90 (04/24 19:22)
→
04/24 19:24, , 2F
04/24 19:24, 2F
推
04/24 19:35, , 3F
04/24 19:35, 3F
※ 編輯: P568912 來自: 140.112.94.90 (04/24 19:40)
→
04/24 19:40, , 4F
04/24 19:40, 4F
→
04/24 19:52, , 5F
04/24 19:52, 5F
→
04/24 19:56, , 6F
04/24 19:56, 6F
→
04/24 20:32, , 7F
04/24 20:32, 7F