这个类专门用于数据的通信 :控制命令 、 图像视频等
TCP类定义
#ifndef QTCP_H
#define QTCP_H
#include <netdb.h>
/**********Tcp client*****************/
class MyTcpClient
{
public:
MyTcpClient();
~MyTcpClient();
public:
void ClientCloseSockfd();
int GetClientFd()const;
void ClientControl(const char *cpHostName,const unsigned short int &iPort);
int ClientSend(unsigned const char *buffer ,const int &iSize);
int ClientReceive(char *pBuffer,const int &iBufferSize,const int &iWantByte);
private:
void ClientGetHostByName(const char *cpHostName);
void ClientSocket();
void ClientSocketAddresInit(const unsigned short int &iPort);
void ClientConnect();
private:
unsigned int m_iMaxQueConnNum; //表示最大连接数
int m_iSockfd;
int m_iSendbytes;
int m_iRecvbytes;
private:
typedef struct
{
struct hostent * host;
struct sockaddr_in serv_addr;
}MyClientStruct;
MyClientStruct * m_stpMyClientControl;
};
/*****************tcp server****************/
class MyTcpServer
{
public:
MyTcpServer();
~MyTcpServer();
public:
void ServerSocket();
void ServerSocketAddresInit(const unsigned short int &iPort);
void ServerSetSocketOpt();
void ServerBindListen();
void ServerAccept();
void ServerDisplay();
void ServerCloseSockfd();
public:
void ServerControl(const unsigned short int &iPort);
public:
// int ServerReceive(unsigned char *buffer);
int ServerSend(const char *buffer, const int &iSize);
private:
typedef struct
{
struct sockaddr_in server_sockaddr;
struct sockaddr_in client_sockaddr;
}MyServerStruct;
MyServerStruct * m_stpMyServerControl;
private:
//unsigned short int PORT; //端口号
unsigned int m_iMaxQueConnNum; //表示最大连接数
int m_iSinSize;
int m_iRecvbytes;
int m_iSendbytes;
int m_iSockfd;
//int m_iClientFd;
};
#endif // QTCP_H
TCP类实现
#include"Tcp.h"
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
/*****************tcp client*************************/
MyTcpClient::MyTcpClient()
{
//this->PORT=4322; //另一端的server port 端口号
this->m_iMaxQueConnNum=5; //表示最大连接数
this->m_iSockfd=0;
this->m_iSendbytes=0;
this->m_iRecvbytes=0;
this->m_stpMyClientControl =new MyClientStruct;
::memset(m_stpMyClientControl,0,sizeof(MyClientStruct));
std::cout<<"Create MyTcpClient Class finished !"<<std::endl;
}
MyTcpClient::~MyTcpClient()
{
if(!m_stpMyClientControl)
{
delete m_stpMyClientControl;
m_stpMyClientControl=NULL;
std::cout<<"Delete m_stpMyClientControl !!!"<<std::endl;
}
}
void MyTcpClient::ClientGetHostByName(const char *cpHostName)
{
/*地址解析函数*/
if ((m_stpMyClientControl->host = gethostbyname(cpHostName)) == NULL)
{
perror("gethostbyname");
exit(1);
}
std::cout<<"The Client Host Name:"<<cpHostName<<std::endl;
}
void MyTcpClient::ClientSocket()
{
/*创建socket*/
if ((m_iSockfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("socket");
exit(1);
}
std::cout<<"The Client Create The Socket !!"<<std::endl;
}
void MyTcpClient::ClientSocketAddresInit(const unsigned short int &iPort)
{
/*设置sockaddr_in 结构体中相关参数*/
m_stpMyClientControl->serv_addr.sin_family = AF_INET;
m_stpMyClientControl->serv_addr.sin_port = htons(iPort);
m_stpMyClientControl->serv_addr.sin_addr = *((struct in_addr *)m_stpMyClientControl->host->h_addr);
bzero(&(m_stpMyClientControl->serv_addr.sin_zero), 8);
std::cout<<"The client_sockaddr_init OK! The PORT:"<<iPort<<std::endl;
}
void MyTcpClient::ClientConnect()
{
/*调用connect函数主动发起对服务器端的连接*/
int my_true=1;
while(my_true)
{
if(connect(m_iSockfd,(struct sockaddr *)&m_stpMyClientControl->serv_addr, sizeof(struct sockaddr))== -1)
{
std::cout<<"Can not connect,I will try"<<std::endl;
}
else
my_true=0;
}
std::cout<<"The Client Connect OK !!"<<std::endl;
}
int MyTcpClient::ClientSend(unsigned const char *buffer,const int &iSize)//这里必须要用size,因为传过来的buffer只是一个地址。
{
int m_iSendbytes_ok=0;
while(m_iSendbytes_ok<iSize)
{
if ((m_iSendbytes = send(m_iSockfd, buffer+m_iSendbytes_ok,iSize-m_iSendbytes_ok, 0)) == -1)
{
perror("send");
exit(1);
}
m_iSendbytes_ok+=m_iSendbytes;
}
std::cout<<"Send OK!!"<<std::endl;
return m_iSendbytes;
}
int MyTcpClient::ClientReceive(char*pBuffer,const int &iBufferSize,const int &iWantByte)
{
memset(pBuffer , 0, iBufferSize);
int iGet = recv(m_iSockfd,pBuffer,iWantByte,0);
if(iGet < iWantByte)
{
std::cout<<"Socket Get Not Equle to WantByte"<<std::endl;
}
if(iGet < iBufferSize)
{
pBuffer[iGet] = '\0';
}
return iGet;
}
void MyTcpClient::ClientCloseSockfd(){
close(m_iSockfd);
}
void MyTcpClient::ClientControl(const char *cpHostName,const unsigned short int &iPort){
ClientGetHostByName(cpHostName);
ClientSocket();
ClientSocketAddresInit(iPort);
// 接收缓冲区
int nRecvBufLen = 32 * 1024; //设置为32K
setsockopt(m_iSockfd,SOL_SOCKET, SO_RCVBUF, (const char*)&nRecvBufLen, sizeof(int));
ClientConnect();
}
int MyTcpClient::GetClientFd()const
{
return m_iSockfd;
}
/*****************************tcp_server***************************************/
MyTcpServer::MyTcpServer()
{
//this->PORT=4321; //这个是服务端专属端口号
this->m_iMaxQueConnNum=5;
this->m_iSinSize=0;
this->m_iRecvbytes=0;
this->m_iSendbytes=0;
this->m_iSockfd=0;
this->m_iSockfd=0;
this->m_stpMyServerControl =new MyServerStruct;
::memset(m_stpMyServerControl,0,sizeof(MyServerStruct));
std::cout<<"Create MyTcpServer Class finished !"<<std::endl;
}
MyTcpServer::~MyTcpServer()
{
if(m_stpMyServerControl)
delete m_stpMyServerControl;
m_stpMyServerControl=NULL;
std::cout<<"Delete MyTcpServer Class finished !"<<std::endl;
}
void MyTcpServer::ServerSocket()
{
/*建立socket连接*/
if ((m_iSockfd = socket(AF_INET,SOCK_STREAM,0))== -1)
{
perror("socket");
exit(1);
}
std::cout<<"Server Socket Id ="<<m_iSockfd<<std::endl;
}
void MyTcpServer::ServerSocketAddresInit(const unsigned short int &iPort)
{
/*设置sockaddr_in 结构体中相关参数*/
m_stpMyServerControl->server_sockaddr.sin_family = AF_INET;
m_stpMyServerControl->server_sockaddr.sin_port = htons(iPort);
m_stpMyServerControl->server_sockaddr.sin_addr.s_addr = INADDR_ANY;
bzero(&(m_stpMyServerControl->server_sockaddr.sin_zero), 8);
}
void MyTcpServer::ServerSetSocketOpt()
{
bool Reused=true;/*使得重复使用本地地址与套接字进行绑定 设置调用closesocket()后,仍可继续重用该socket*/
setsockopt(m_iSockfd, SOL_SOCKET, SO_REUSEADDR,( const char* )&Reused,sizeof(bool));
int nSendBufLen = 32*1024; //设置为32K //发送缓冲区
setsockopt( m_iSockfd, SOL_SOCKET, SO_SNDBUF, ( const char* )&nSendBufLen, sizeof(int));
int nZero = 0;
setsockopt(m_iSockfd, SOL_SOCKET, SO_SNDBUF, ( char * )&nZero, sizeof( nZero ) );
}
void MyTcpServer::ServerBindListen()
{
/*绑定函数bind*/
if (bind(m_iSockfd, (struct sockaddr *)&m_stpMyServerControl->server_sockaddr, sizeof(struct sockaddr))== -1)
{
perror("bind");
exit(1);
}
std::cout<<"Server Bind success!"<<std::endl;
/*调用listen函数*/
if (listen(m_iSockfd, m_iMaxQueConnNum) == -1)
{
perror("listen");
exit(1);
}
std::cout<<"server listening ............."<<std::endl;
//std::cout<<"Server is Listening.... In IP:"<<inet_ntoa(m_stpMyServerControl->server_sockaddr.sin_addr)<<std::endl;
}
void MyTcpServer::ServerAccept()
{
/*调用accept函数,等待客户端的连接*/
if ((m_iSockfd = accept(m_iSockfd, (struct sockaddr *)&m_stpMyServerControl->client_sockaddr,
(socklen_t*)&m_iSinSize)) == -1)
{
perror("accept");
exit(1);
}
std::cout<<"server accept"<<std::endl;
//std::cout<<"server: got connection from"<<inet_ntoa(m_stpMyServerControl->client_sockaddr.sin_addr)<<std::endl;
}
//int MyTcpServer::ServerReceive(unsigned char *buffer)
//{
// /*调用recv函数接收客户端的请求*/
// memset(buffer , 0, sizeof(buffer));
// if ((m_iRecvbytes =recv(m_iSockfd, buffer,sizeof(buffer), 0)) == -1)
// {
// perror("recv");
// exit(1);
// }
// return m_iRecvbytes;
// }
/*
int MyTcpServer::server_send(void *buffer,int size)//这里必须要用size,因为传过来的buffer只是一个地址。
{
if ((m_im_iSockfd = send(m_iSockfd, buffer,size, 0)) == -1)
{
perror("send");
exit(1);
}
return m_isockfd;
}
*/
int MyTcpServer::ServerSend(const char *buffer,const int &iSize)//这里必须要用size,因为传过来的buffer只是一个地址。
{
int sendbytes_ok=0;
while(sendbytes_ok<iSize)
{
if ((m_iSendbytes = send(m_iSockfd, buffer+sendbytes_ok,iSize-sendbytes_ok, 0)) == -1)
{
perror("send");
exit(1);
}
sendbytes_ok+=m_iSendbytes;
}
std::cout<<"Send OK!!"<<std::endl;
return m_iSendbytes;
}
void MyTcpServer::ServerCloseSockfd()
{
close(m_iSockfd);
std::cout<<"Server Close The Sockfd !!!"<<std::endl;
}
void MyTcpServer::ServerControl(const unsigned short int &iPort)
{
ServerSocket();
ServerSocketAddresInit(iPort);
ServerSetSocketOpt();
ServerBindListen();
ServerAccept();
}