LINUX網路程式設計之套接字

2021-06-08 07:03:20 字數 861 閱讀 4821

套接字可以讓linux在網路上通訊,用起來就像管道一樣,當然管道都是單向的,套接字既能寫也能收!

以下是多客戶伺服器**:

#include #include #include #include #include #include #include #include #define port 6000

#define size 256

static void handler()//**子程序函式

int main(int argc, char **argv)

ret = listen(server_socketfd, 5);//建立乙個監聽5個請求的佇列,也就是說這個伺服器允許5個人同時登入

if ( ret == -1 )

while (1)

else if (pid > 0)//父程序把分配的客戶套接字關閉,用來供給下個客戶

else

}return 0;

}

客戶端**:

#include #include #include #include #include #include #include #include #define size 256

#define port 6000

int main()

printf("send:");

fgets(buffer,sizeof(buffer),stdin);

write(socketfd,buffer,sizeof(buffer));//把資料傳給伺服器

close(socketfd);//關閉套接字

return 0;

}

Linux 之 網路程式設計之套接字選項

獲取和設定套接字選項 so keepalive選項 so linger選項 so rcvbuf和so sndbuf選項 so rcvlowat和so sndlowat選項 so rcvtimeo和so sndtimeo選項 so reuseaddr選項getsockopt 函式和setsockopt...

套接字程式設計之localsocket

unix domain socket ipc 使用unix domain socket的過程和網路socket十分相似,也要先呼叫socket 建立乙個socket檔案描述符,address family指定為af unix,type可以選擇sock dgram或sock stream,protoc...

Linux網路程式設計 套接字結構

1,結構體 struct sockaddr,struct sockaddr in,struct in addr 通用套接字資料結構 struct sockaddr實際使用的套接字資料結構,二者可以進行型別轉換 struct sockaddr in struct in addr 2,主機位元組序和網路...