Unix socket 程式設計API二之輔助函式

2021-09-08 02:14:16 字數 2111 閱讀 1883

一:網路位元組序跟主機位元組序轉換:

#include uint32_t htonl(uint32_t hostlong); //host to network long

uint16_t htons(uint16_t hostshort);//host to network short

uint32_t ntohl(uint32_t netlong); //network to host long

uint16_t ntohs(uint16_t netshort); //network to host short

eg:檢視主機是大端位元組序還是小端位元組序

#include	"unp.h"

int main(int argc, char **ar**)

un;un.s = 0x0102;

printf("%s: ", cpu_vendor_os);

if (sizeof(short) == 2) else

printf("sizeof(short) = %d\n", sizeof(short));

exit(0);

}

二:位址轉換函式

#include #include #include int inet_aton(const char *cp, struct in_addr *inp); //將字串位址cp轉為struct in_addr結構

in_addr_t inet_addr(const char *cp);//將字串位址cp轉為in_addr_t結構

in_addr_t inet_network(const char *cp);//將字串位址cp轉為in_addr_t結構

char *inet_ntoa(struct in_addr in);//將struct in_addr位址結構轉換為字串

#include int inet_pton(int af, const char *src, void *dst);

const char *inet_ntop(int af, const void *src,char *dst, socklen_t size);

#include int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

四:select

/* according to earlier standards */

#include #include #include int select(int nfds, fd_set *readfds, fd_set *writefds,

fd_set *exceptfds, struct timeval *timeout);

void fd_clr(int fd, fd_set *set);

int fd_isset(int fd, fd_set *set);

void fd_set(int fd, fd_set *set);

void fd_zero(fd_set *set);

#include int pselect(int nfds, fd_set *readfds, fd_set *writefds,

fd_set *exceptfds, const struct timespec *timeout,

const sigset_t *sigmask);

五:套接字選項

#include /* see notes */

#include int getsockopt(int sockfd, int level, int optname,void *optval, socklen_t *optlen);

int setsockopt(int sockfd, int level, int optname,const void *optval, socklen_t optlen);

Unix Socket程式設計簡介

閱讀本文需要對tcp有基本的了解,對tcp連線的建立 斷開,以及tcp執行過程中狀態轉換不了解的童鞋可以首先閱讀我的另一篇部落格 unix linux作業系統暴露給應用層的網路程式設計介面是socket,下面我們對socket程式設計進行個簡單的介紹 client server程式設計模型是使用最廣...

程式設計 unix socket 學習的記錄

1gcc ser.c o ser include include include include include include define un sock path home admin zzk tenda unix socket server.socket int main int argc,...

unix Socket程序間通訊

結構體部分介紹 typs.h define std type typedef define u32 type unsigned int std type u32 type socklen t unistd.h typedef socklen t socklen t linux un.h define...