两个结构体
struct sockaddr {
sa_family_t sa_family; /* address family, AF_xxx */
char sa_data[14]; /* 14 bytes of protocol address */
};
/* Structure describing an Internet socket address. */
struct sockaddr_in
{
__SOCKADDR_COMMON (sin_);
in_port_t sin_port; /* Port number. */
struct in_addr sin_addr; /* Internet address. */
/* Pad to size of `struct sockaddr'. */
unsigned char sin_zero[sizeof (struct sockaddr) -
__SOCKADDR_COMMON_SIZE -
sizeof (in_port_t) -
sizeof (struct in_addr)];
};
主要函数
int socket(int domain, int type, int protocol);
int bind(int sockfd,struct sockaddr *my_addr, int addrlen);
int send(int sockfd, const void *msg, int len, int flags);
int accept(int sockfd, void *addr, int *addrlen);
int listen(int sockfd, int backlog);
int recv(int sockfd,void *buf,int len,unsigned int flags);
int sendto(int sockfd, const void *msg,int len,unsigned int flags,const struct sockaddr *to, int tolen);
int recvfrom(int sockfd,void *buf,int len,unsigned int flags,struct sockaddr *from,int *fromlen);
from是一个struct sockaddr类型的变量,该变量保存源机的IP地址及端口号。fromlen常置为sizeof (struct sockaddr)。
一点解释
Send()和recv()这两个函数用于面向连接的socket上进行数据传输
Sendto()和recvfrom()用于在无连接的数据报socket方式下进行数据传输
调用close()函数来释放该socket
调用shutdown()函数来关闭该socket
int shutdown(int sockfd,int how);
Sockfd是需要关闭的socket的描述符。参数 how允许为shutdown操作选择以下几种方式:
·0-------不允许继续接收数据
·1-------不允许继续发送数据
·2-------不允许继续发送和接收数据
/* Description of data base entry for a single host. *//usr/include 下
struct hostent
{
char *h_name; /* Official name of host. */
char **h_aliases; /* Alias list. */
int h_addrtype; /* Host address type. */
int h_length; /* Length of address. */
char **h_addr_list; /* List of addresses from name server. */
#define h_addr h_addr_list[0] /* Address, for backward compatibility. */
};
struct hostent {
char *h_name; //h_name 为地址名称
char **h_aliases; // 一般是个空字节-它是地址的预备名称的指针,也可以这样说 它是一个以空指针结尾的可选主机名队列。
int h_addrtype; //地址类型; 通常是AF_INET(代表是ipv4)
int h_length; //地址的比特长度
char **h_addr_list; //主机网络地址指针 ,零字节-主机网络地址指针( 空指针结尾的主机地址的列表,返回的地址是以网络顺序排列的)。
}
gethostbyname() 成功时返回一个指向结构体 hostent 的指针,失败时是个空 (NULL) 指针
gethostbyaddr()
htonl():把32位值从主机字节序转换成网络字节序·
htons():把16位值从主机字节序转换成网络字节序·
ntohl():把32位值从网络字节序转换成主机字节序·
ntohs():把16位值从网络字节序转换成主机字节序
int (*release)(struct socket *sock);
int (*bind)(struct socket *sock, struct sockaddr *myaddr, int sockaddr_len);
int (*connect)(struct socket *sock, struct sockaddr *vaddr, int sockaddr_len, int flags);
int (*accept)(struct socket *sock,struct socket *newsock, int flags);
unsigned int (*poll)(struct file *file, struct poll_table_struct *wait);
int (*ioctl)(struct socket *sock, unsigned int cmd, unsigned long arg);
int (*listen)(struct socket *sock, int len);
int (*shutdown)(struct socket *sock, int flags);
int (*setsockopt)(struct socket *sock, int level, int optname, char __user *optval, int optlen);
int (*getsockopt)(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen);
int (*sendmsg)(struct kiocb *iocb, struct socket *sock,struct msghdr *m, size_t total_len);
int (*recvmsg)(struct kiocb *iocb, struct socket *sock,struct msghdr *m, size_t total_len, int flags);
int (*mmap)(struct file *file, struct socket *sock,struct vm_area_struct * vma);
ssize_t (*sendpage)(struct socket *sock, struct page *page,int offset, size_t size, int flags);