TCP/IP网络编程 第十三章:多种IO函数
发布人:shili8
发布时间:2025-01-15 20:35
阅读次数:0
**TCP/IP网络编程**
**第十三章: 多种IO函数**
在前面的章节中,我们已经学习了如何使用TCP/IP协议栈中的各种函数来实现网络通信。然而,在实际的开发过程中,往往需要使用更底层的IO函数来进行数据传输和处理。在本章中,我们将介绍一些常用的IO函数,并提供示例代码。
###1. read() 和 write()
read() 和 write() 是最基本的IO函数,它们用于读取和写入文件或网络套接字中的数据。下面是它们的使用方法:
c#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> int read(int fd, void *buf, size_t count); int write(int fd, const void *buf, size_t count);
示例代码:
cint main() { int sock = socket(AF_INET, SOCK_STREAM,0); if (sock < 0) { perror("socket"); return -1; } char buffer[1024]; int bytes_read = read(sock, buffer, sizeof(buffer)); if (bytes_read >0) { printf("%s ", buffer); } else { perror("read"); } close(sock); return0; }
###2. recv() 和 send()
recv() 和 send() 是用于网络套接字的IO函数,它们用于读取和写入数据包。下面是它们的使用方法:
c#include <sys/socket.h> ssize_t recv(int sockfd, void *buf, size_t len, int flags); ssize_t send(int sockfd, const void *buf, size_t len, int flags);
示例代码:
cint main() { int sock = socket(AF_INET, SOCK_STREAM,0); if (sock < 0) { perror("socket"); return -1; } char buffer[1024]; ssize_t bytes_sent = send(sock, "Hello, world!", strlen("Hello, world!"),0); if (bytes_sent >0) { printf("Sent %ld bytes ", bytes_sent); } else { perror("send"); } close(sock); return0; }
###3. recvfrom() 和 sendto()
recvfrom() 和 sendto() 是用于网络套接字的IO函数,它们用于读取和写入数据包,并返回发送方的地址信息。下面是它们的使用方法:
c#include <sys/socket.h> ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen); ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen);
示例代码:
cint main() { int sock = socket(AF_INET, SOCK_DGRAM,0); if (sock < 0) { perror("socket"); return -1; } char buffer[1024]; struct sockaddr_in src_addr; memset(&src_addr,0, sizeof(src_addr)); src_addr.sin_family = AF_INET; src_addr.sin_port = htons(12345); inet_pton(AF_INET, "127.0.0.1", &src_addr.sin_addr); ssize_t bytes_received = recvfrom(sock, buffer, sizeof(buffer),0, (struct sockaddr *)&src_addr, NULL); if (bytes_received >0) { printf("%s ", buffer); } else { perror("recvfrom"); } close(sock); return0; }
###4. readv() 和 writev()
readv() 和 writev() 是用于网络套接字的IO函数,它们用于读取和写入多个缓冲区中的数据。下面是它们的使用方法:
c#include <sys/socket.h> ssize_t readv(int sockfd, const struct iovec *iov, int iovcnt, void *buf, size_t count); ssize_t writev(int sockfd, const struct iovec *iov, int iovcnt, void *buf, size_t count);
示例代码:
cint main() { int sock = socket(AF_INET, SOCK_STREAM,0); if (sock < 0) { perror("socket"); return -1; } char buffer1[1024]; char buffer2[1024]; struct iovec iov[] = {{buffer1, sizeof(buffer1)}, {buffer2, sizeof(buffer2)}}; int iovcnt =2; ssize_t bytes_read = readv(sock, iov, iovcnt, NULL,0); if (bytes_read >0) { printf("%s ", buffer1); printf("%s ", buffer2); } else { perror("readv"); } close(sock); return0; }
###5. recvmsg() 和 sendmsg()
recvmsg() 和 sendmsg() 是用于网络套接字的IO函数,它们用于读取和写入多个缓冲区中的数据,并返回发送方的地址信息。下面是它们的使用方法:
c#include <sys/socket.h> ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags); ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
示例代码:
cint main() { int sock = socket(AF_INET, SOCK_DGRAM,0); if (sock < 0) { perror("socket"); return -1; } char buffer[1024]; struct sockaddr_in src_addr; memset(&src_addr,0, sizeof(src_addr)); src_addr.sin_family = AF_INET; src_addr.sin_port = htons(12345); inet_pton(AF_INET, "127.0.0.1", &src_addr.sin_addr); struct msghdr msg; memset(&msg,0, sizeof(msg)); char *iov[2] = {buffer, buffer}; struct iovec iov_vec[] = {{iov[0], strlen(iov[0])}, {iov[1], strlen(iov[1])}}; msg.msg_iov = iov_vec; msg.msg_iovlen =2; ssize_t bytes_received = recvmsg(sock, &msg,0); if (bytes_received >0) { printf("%s ", buffer); } else { perror("recvmsg"); } close(sock); return0; }
以上是本章中介绍的IO函数及其示例代码。这些函数可以帮助你更好地理解网络编程中的数据传输和处理过程。