相關鏈接:TCP連接與釋放、網絡編程——C++實現socket通信(TCP)
相關函數:
服務端:
socket()
bind()
listen()
poll() 高併發poll模式
accept()
read() 或 recv()等
write() 或 send()等
close()
客戶端:
socket()
connect()
write() 或 send()等
read() 或 recv()等
close()
着重說明下poll函數用法。
跟select功能類似,可以設置的同時監聽上限會更多,poll效率更高,調用完poll函數之後不會清空監聽的事件集合.
int poll(struct pollfd *fds, nfds_t nfds, int timeout);
-fds: 是一個struct pollfd結構類型的數組,用於存放需要檢測其狀態的socket描述符。結構體類型定義如下:
struct pollfd {
int fd; //文件描述符
short events; //等待的需要監聽的事件類型,常用取值爲POLLIN(監聽讀)/POLLOUT(寫)/POLLERR(異常)。如fds[0].events = POLLIN
short revents; //實際發生了的事件,也就是返回結果。值的範圍同events: POLLIN/POLLOUT/POLLERR
};
-nfds: nfds_t類型的參數,用於標記數組fds中的結構體元素的總數量;
-timeout: 是poll函數調用阻塞的時間,單位:毫秒。傳值-1表示阻塞監聽,0表示不阻塞立即返回,>0表示阻塞等待timeout的時間
返回值:>0:數組fds中準備好讀、寫或出錯狀態的那些socket描述符的總數量,fds數組中有狀態的fd的revents被賦值傳出,可以通過跟POLLIN/POLLOUT/POLLERR等標誌通過位與&來判斷,如if(fds[n].revents & POLLIN)
=0:數組fds中沒有任何socket描述符準備好讀、寫,或出錯,revents會被清空
=-1:poll函數調用失敗,同時會自動設置全局變量errno.
注意:每當服務端連接斷開後,進入TIME_WAIT狀態,等待2msl時間之後才能重新使用IP和端口,否則在bind時就會報錯。要解決這個問題可以在程序開始時調用端口複用函數setsockopt。原型如下:
//int setsockopt(int sockfd, int level, int optname,const void *optval, socklen_t optlen);
/* sockfd:標識一個套接口的描述字。
level:選項定義的層次;支持SOL_SOCKET、IPPROTO_TCP、IPPROTO_IP和IPPROTO_IPV6。
optname:需設置的選項。
optval:指針,指向存放選項值的緩衝區
optlen:optval緩衝區長度。
返回值: 成功返回0,失敗返回 -1. */
實際調用:
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
廢話不多說,上源碼!
實現的功能:客戶端C向服務端S發送一串字符數據,S端會對字符串做轉大寫操作然後回發給C端。直接在咱們Tcp_Server.cpp基礎上修改代碼
服務端Poll_Server.cpp
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <ctype.h>
#include <poll.h> //poll頭文件
#define MAXSIZE 1024
#define IP_ADDR "127.0.0.1"
#define IP_PORT 8888
int main()
{
int i_listenfd, i_connfd;
struct sockaddr_in st_sersock;
char msg[MAXSIZE];
int nrecvSize = 0;
int index = 0; //記錄fd數組中最大fd對應的下標
struct pollfd pofds[MAXSIZE]; //結構體數組
for(n : pofds) //將所有數組中的fd設爲-1,方便以後填充
{
n.fd = -1;
}
if((i_listenfd = socket(AF_INET, SOCK_STREAM, 0) ) < 0) //建立socket套接字
{
printf("socket Error: %s (errno: %d)\n", strerror(errno), errno);
exit(0);
}
memset(&st_sersock, 0, sizeof(st_sersock));
st_sersock.sin_family = AF_INET; //IPv4協議
st_sersock.sin_addr.s_addr = htonl(INADDR_ANY); //INADDR_ANY轉換過來就是0.0.0.0,泛指本機的意思,也就是表示本機的所有IP,因爲有些機子不止一塊網卡,多網卡的情況下,這個就表示所有網卡ip地址的意思。
st_sersock.sin_port = htons(IP_PORT);
if(bind(i_listenfd,(struct sockaddr*)&st_sersock, sizeof(st_sersock)) < 0) //將套接字綁定IP和端口用於監聽
{
printf("bind Error: %s (errno: %d)\n", strerror(errno), errno);
exit(0);
}
if(listen(i_listenfd, 20) < 0) //設定可同時排隊的客戶端最大連接個數
{
printf("listen Error: %s (errno: %d)\n", strerror(errno), errno);
exit(0);
}
printf("listen fd: %d\n", i_listenfd);
pofds[index].fd = i_listenfd; //先賦值
pofds[index].events = POLLIN;
printf("======waiting for client's request======\n");
//準備接受客戶端連接
while(1)
{
int nCount = poll(pofds, index+1, -1); //阻塞監聽
printf("----------poll監聽到可讀事件計數:%d\n",nCount);
for(int i = 0; i < MAXSIZE; i++)
{
if(nCount == 0)
{
break;
}
if(!(pofds[i].revents & POLLIN))
{
continue; //不在監聽事件中則跳過
}
printf("----------即將處理監聽到的 pofds[%d]: %d\n", i, pofds[i].fd);
nCount--; //每處理一次就自減1
if(pofds[i].fd == i_listenfd) //監聽到有客戶端連接
{
if((i_connfd = accept(i_listenfd, (struct sockaddr*)NULL, NULL)) < 0) //阻塞等待客戶端連接
{
printf("accept Error: %s (errno: %d)\n", strerror(errno), errno);
// continue;
}
else
{
printf("Client[%d], welcome!\n", i_connfd);
}
for(int n = 0; n < MAXSIZE; n++)
{
if(pofds[n].fd == -1) //將新客戶端fd加入數組中
{
pofds[n].fd = i_connfd;
pofds[n].events = POLLIN;
index < n ? index = n : true ;
printf("將新客戶端fd加入數組中. fd:%d, index:%d\n", pofds[n].fd, index);
break;
}
}
}
else //監聽到已連接的客戶端發來的數據
{
//接受客戶端發來的消息並作處理(小寫轉大寫)後回寫給客戶端
memset(msg, 0 ,sizeof(msg));
if((nrecvSize = read(pofds[i].fd, msg, MAXSIZE)) < 0)
{
printf("accept Error: %s (errno: %d)\n", strerror(errno), errno);
continue;
}
else if( nrecvSize == 0) //read返回0代表對方已close斷開連接。
{
printf("client has disconnected!\n");
if(index == i) //如果是最大的下標的客戶端退出,則index-1
{
index--;
}
pofds[i].fd = -1; //清除數組中相應位置
close(pofds[i].fd);
continue;
}
else
{
printf("recvMsg:%s", msg);
for(int i=0; msg[i] != '\0'; i++)
{
msg[i] = toupper(msg[i]);
}
if(write(pofds[i].fd, msg, strlen(msg)+1) < 0)
{
printf("accept Error: %s (errno: %d)\n", strerror(errno), errno);
}
}
}
}
}//while
close(i_listenfd);
return 0;
}
客戶端Poll_Client.cpp (直接用咱們Tcp_Client.cpp就可以)
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <signal.h>
#include <arpa/inet.h>
#define MAXSIZE 1024
#define IP_ADDR "127.0.0.1"
#define IP_PORT 8888
int i_sockfd = -1;
void SigCatch(int sigNum) //信號捕捉函數(捕獲Ctrl+C)
{
if(i_sockfd != -1)
{
close(i_sockfd);
}
printf("Bye~! Will Exit...\n");
exit(0);
}
int main()
{
struct sockaddr_in st_clnsock;
char msg[1024];
int nrecvSize = 0;
signal(SIGINT, SigCatch); //註冊信號捕獲函數
if((i_sockfd = socket(AF_INET, SOCK_STREAM, 0) ) < 0) //建立套接字
{
printf("socket Error: %s (errno: %d)\n", strerror(errno), errno);
exit(0);
}
memset(&st_clnsock, 0, sizeof(st_clnsock));
st_clnsock.sin_family = AF_INET; //IPv4協議
//IP地址轉換(直接可以從物理字節序的點分十進制 轉換成網絡字節序)
if(inet_pton(AF_INET, IP_ADDR, &st_clnsock.sin_addr) <= 0)
{
printf("inet_pton Error: %s (errno: %d)\n", strerror(errno), errno);
exit(0);
}
st_clnsock.sin_port = htons(IP_PORT); //端口轉換(物理字節序到網絡字節序)
if(connect(i_sockfd, (struct sockaddr*)&st_clnsock, sizeof(st_clnsock)) < 0) //主動向設置的IP和端口號的服務端發出連接
{
printf("connect Error: %s (errno: %d)\n", strerror(errno), errno);
exit(0);
}
printf("======connect to server, sent data======\n");
while(1) //循環輸入,向服務端發送數據並接受服務端返回的數據
{
fgets(msg, MAXSIZE, stdin);
printf("will send: %s", msg);
if(write(i_sockfd, msg, MAXSIZE) < 0) //發送數據
{
printf("write Error: %s (errno: %d)\n", strerror(errno), errno);
exit(0);
}
memset(msg, 0, sizeof(msg));
if((nrecvSize = read(i_sockfd, msg, MAXSIZE)) < 0) //接受數據
{
printf("read Error: %s (errno: %d)\n", strerror(errno), errno);
}
else if(nrecvSize == 0)
{
printf("Service Close!\n");
}
else
{
printf("Server return: %s\n", msg);
}
}
return 0;
}