概述
TCP/IP——网络编程模型
小狼@http://blog.csdn.net/xiaolangyangyang
一 socket结构体
struct sockaddr {
unsigned short sa_family; /* address族, AF_xxx */
char sa_data[14]; /* 14 bytes的协议地址 */
};
struct sockaddr_in {
short int sin_family; /* Internet地址族 */
unsigned short int sin_port; /* 端口号 */
struct in_addr sin_addr; /* Internet地址 */
unsigned char sin_zero[8]; /* 添0(和struct sockaddr一样大小)*/
};
struct in_addr {
unsigned long s_addr;
};
二 socket常用函数
int socket(int domain, int type, int protocol);
int bind(int sockfd, struct sockaddr *my_addr, int addrlen);
int connect(int sockfd, struct sockaddr *serv_addr, int addrlen);
int listen(int sockfd, int backlog);
int accept(int sockfd, void *addr, int *addrlen);
int send(int sockfd, const void *msg, int len, int flags);
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);
close(sockfd);
int shutdown(int sockfd, int how);
int setsockopt(int sockfd, int level, int name, char *value, int *optlen);
int getsockopt(int sockfd, int level, int name, char *value, int *optlen);
int getpeername(int sockfd, struct sockaddr *addr, int *addrlen);
retcode = getsockname(socket, name, namelen);
struct hostent *gethostbyname(const char *name);
retcode = gethostbyaddr(addr, len, type);
retcode = getprotobyname(name);
int fcntl(int fd, int cmd, long arg);
三 初级网络编程模型(阻塞I/O模型)
四 Linux下网络编程五种I/O模型
- 阻塞I/O (blocking I/O)
- 非阻塞I/O (nonblocking I/O)
- I/O复用 (select 和poll) (I/O multiplexing)
- 信号驱动I/O (signal driven I/O (SIGIO))
- 异步I/O (asynchronous I/O (the POSIX aio_functions))
usr_socket.sin_addr.s_addr = inet_addr(ip);
usr_socket.sin_port = htons(port);
bzero(&(usr_socket.sin_zero), 8);
fd_set wset;
struct timeval tv;
flags = fcntl(socket_fd, F_GETFL, 0);
fcntl(socket_fd, F_SETFL, flags|O_NONBLOCK);
FD_ZERO(&wset);
FD_SET(socket_fd, &wset);
tv.tv_sec = 2;
tv.tv_usec = 0;
result = select(socket_fd+1, NULL, &wset, NULL, &tv);
最后
以上就是拼搏奇异果为你收集整理的TCP/IP——网络编程模型的全部内容,希望文章能够帮你解决TCP/IP——网络编程模型所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复