网络编程之select
網(wǎng)絡(luò)編程學(xué)習(xí)模式,有select、poll、epoll等方式,測(cè)試使用三種方式來(lái)測(cè)試網(wǎng)絡(luò)連接。
/*
*?
*?
*/
#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 網(wǎng)絡(luò)處理函數(shù)封裝
/**
* @brief 帶超時(shí)(微秒級(jí))的tcp連接函數(shù)
* @param host : 連接主機(jī)名,不能為NULL
* @param port : 連接主機(jī)監(jiān)聽(tīng)端口
* @param timeout : 最大超時(shí)時(shí)間(微妙)的指針,不能為NULL,連接成功timeout將被設(shè)置為剩余的時(shí)間
* @param name : 連接模塊名
* @return -1 連接失敗或超時(shí)
* @return socket 連接成功的socket
*/
int CCConnectO(const char *host, int port, int *timeout, const char *name);
/**
* @brief 帶超時(shí)(微秒級(jí))的socket讀操作
* @param sock : 已連接的socket
* @param buf : 讀入的空間
* @param len : 讀入的最大長(zhǎng)度
* @param timeout : 最大超時(shí)時(shí)間(微妙)的指針,不能為NULL,函數(shù)返回后timeout將被設(shè)置為剩余的時(shí)間
* @param name : 連接模塊名
* @return -1 讀失敗
* @return 實(shí)際讀到的字符數(shù)
*/
int CCReadO(int sock, void *buf, ssize_t len, int *timeout, const char *name);
/**
* @brief 帶超時(shí)(微秒級(jí))的socket讀操作,直到讀到的內(nèi)容中出現(xiàn)了stop串,或者讀到len長(zhǎng)度,或超時(shí)。
* @param sock : 已連接的socket
* @param buf : 讀入的空間
* @param len : 讀入的最大長(zhǎng)度
* @param timeout : 最大超時(shí)時(shí)間(微妙)的指針,不能為NULL,函數(shù)返回后timeout將被設(shè)置為剩余的時(shí)間
* @param name : 連接模塊名
* @param stop : 當(dāng)讀到內(nèi)容中出現(xiàn)stop字串時(shí),停止讀
* @return -1 讀失敗
* @return 實(shí)際讀到的字符數(shù)
*/
int CCReadOS(int sock, void *buf, ssize_t len, int *timeout, const char *name,
const char *stop);
/**
* @brief 帶超時(shí)(微秒級(jí))的socket寫(xiě)操作
* @param sock : 已連接的socket
* @param buf : 要寫(xiě)出的空間
* @param len : 要寫(xiě)出的最大長(zhǎng)度
* @param timeout : 最大超時(shí)時(shí)間(微妙)的指針,不能為NULL,函數(shù)返回后timeout將被設(shè)置為剩余的時(shí)間
* @param name : 連接模塊名
* @return -1 讀失敗
* @return 實(shí)際寫(xiě)出的字符數(shù)
*/
int CCWriteO(int sock, void *buf, ssize_t len, int *timeout,
const char *name);
/**
* @brief 關(guān)閉socket
* @param fd : 已連接的socket
* @return -1 關(guān)閉失敗
* @return 0 成功關(guān)閉
*/
int CCClose(int fd);
/**
* @brief 開(kāi)啟tcp監(jiān)聽(tīng)端口
* @param port : 端口
* @param queue : listen等待隊(duì)列長(zhǎng)度
* @return -1 失敗
* @return 成功返回開(kāi)啟監(jiān)聽(tīng)的fd
*/
int CCTcpListen(const char *host, int port, int queue);
#endif // _CCNET_H
/* vim: create noet: */
轉(zhuǎn)載于:https://www.cnblogs.com/cyblogs/p/11295568.html
總結(jié)
以上是生活随笔為你收集整理的网络编程之select的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 如何达到自己想要的成功 2011.8.4
- 下一篇: Intellij IDEA运行报Comm