网络编程之select
網絡編程學習模式,有select、poll、epoll等方式,測試使用三種方式來測試網絡連接。
/*
*?
*?
*/
#ifndef _CCNET_H
#define _CCNET_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
//@file
//@brief 網絡處理函數封裝
/**
* @brief 帶超時(微秒級)的tcp連接函數
* @param host : 連接主機名,不能為NULL
* @param port : 連接主機監聽端口
* @param timeout : 最大超時時間(微妙)的指針,不能為NULL,連接成功timeout將被設置為剩余的時間
* @param name : 連接模塊名
* @return -1 連接失敗或超時
* @return socket 連接成功的socket
*/
int CCConnectO(const char *host, int port, int *timeout, const char *name);
/**
* @brief 帶超時(微秒級)的socket讀操作
* @param sock : 已連接的socket
* @param buf : 讀入的空間
* @param len : 讀入的最大長度
* @param timeout : 最大超時時間(微妙)的指針,不能為NULL,函數返回后timeout將被設置為剩余的時間
* @param name : 連接模塊名
* @return -1 讀失敗
* @return 實際讀到的字符數
*/
int CCReadO(int sock, void *buf, ssize_t len, int *timeout, const char *name);
/**
* @brief 帶超時(微秒級)的socket讀操作,直到讀到的內容中出現了stop串,或者讀到len長度,或超時。
* @param sock : 已連接的socket
* @param buf : 讀入的空間
* @param len : 讀入的最大長度
* @param timeout : 最大超時時間(微妙)的指針,不能為NULL,函數返回后timeout將被設置為剩余的時間
* @param name : 連接模塊名
* @param stop : 當讀到內容中出現stop字串時,停止讀
* @return -1 讀失敗
* @return 實際讀到的字符數
*/
int CCReadOS(int sock, void *buf, ssize_t len, int *timeout, const char *name,
const char *stop);
/**
* @brief 帶超時(微秒級)的socket寫操作
* @param sock : 已連接的socket
* @param buf : 要寫出的空間
* @param len : 要寫出的最大長度
* @param timeout : 最大超時時間(微妙)的指針,不能為NULL,函數返回后timeout將被設置為剩余的時間
* @param name : 連接模塊名
* @return -1 讀失敗
* @return 實際寫出的字符數
*/
int CCWriteO(int sock, void *buf, ssize_t len, int *timeout,
const char *name);
/**
* @brief 關閉socket
* @param fd : 已連接的socket
* @return -1 關閉失敗
* @return 0 成功關閉
*/
int CCClose(int fd);
/**
* @brief 開啟tcp監聽端口
* @param port : 端口
* @param queue : listen等待隊列長度
* @return -1 失敗
* @return 成功返回開啟監聽的fd
*/
int CCTcpListen(const char *host, int port, int queue);
#endif // _CCNET_H
/* vim: create noet: */
轉載于:https://www.cnblogs.com/cyblogs/p/11295568.html
總結
以上是生活随笔為你收集整理的网络编程之select的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何达到自己想要的成功 2011.8.4
- 下一篇: Intellij IDEA运行报Comm