TCPSocket.h
#ifndef _TCP_SOCKET_H_
#define _TCP_SOCKET_H_
#include
#include
#include
//#include
//#include
#include
//
class TCPServer
{
public:
TCPServer(int port = 8082);
~TCPServer();
// 打开
bool Open(QString address, int port);
// 关闭
void Close();
// 获取连接信息
void Accept();
// 发送数据
int Send(const QByteArray toSend);
// 接收数据
QByteArray Recv();
int Recv(QByteArray& toRecv, bool append = false);
// 获取IP
QString getIp(bool client = true);
// 获取端口号
u_short getPort(bool client = true);
// 获取IP和端口号
QString getIpPort(bool client = true);
private:
WSADATA wsaData;
SOCKET socketServer;
sockaddr_in serverAddr;
SOCKET socketClient;
sockaddr_in clientAddr;
QMutex m_mutex;
};
//
class TCPClient
{
public:
TCPClient();
~TCPClient();
// 连接
bool Connect(QString address, int port);
// 断开连接
void Disconnect();
// 发送数据
int Send(const QByteArray toSend);
// 接收数据
QByteArray Recv();
int Recv(QByteArray& toRecv, bool append = false);
// 获取IP
QString getIp();
// 获取端口号
u_short getPort();
// 获取IP和端口号
QString getIpPort();
private:
WSADATA wsaData;
SOCKET socketServer;
sockaddr_in serverAddr;
QMutex m_mutex;
};
#endif
TCPSocket.cpp
#include "TCPSocket.h"
#pragma comment (lib, "ws2_32")
#define MAX_BUFF 10000
//
TCPServer::TCPServer(int port)
{
}
TCPServer::~TCPServer()
{
Close();
}
bool TCPServer::Open(QString address, int port)
{
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
return false;
socketServer = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (socketServer == INVALID_SOCKET)
{
//int code = WSAGetLastError();
WSACleanup();
//exit(code);
return false;
}
char hostname[MAX_BUFF];
gethostname(hostname, MAX_BUFF);
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(port);
inet_pton(AF_INET, address.toLocal8Bit().data(), &serverAddr.sin_addr);
//serverAddr.sin_addr.S_un.S_addr = ((struct in_addr*)gethostbyname(hostname)->h_addr)->s_addr;
if (bind(socketServer, (SOCKADDR*)&serverAddr, sizeof(serverAddr)) != 0)
{
Close();
return false;
}
int timeout = 100;
setsockopt(socketServer, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct timeval));
setsockopt(socketServer, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(struct timeval));
//u_long iMode = 1;//非阻塞模式
//ioctlsocket(socketServer, FIONBIO, &iMode);
if (listen(socketServer, 1) != 0)
{
Close();
return false;
}
return true;
}
void TCPServer::Close()
{
if (socketClient)
closesocket(socketClient);
if (socketServer)
closesocket(socketServer);
WSACleanup();
}
void TCPServer::Accept()
{
int nSize = sizeof(clientAddr);
socketClient = accept(socketServer, (SOCKADDR*)&clientAddr, &nSize);
if (socketClient == INVALID_SOCKET)
Close();
}
int TCPServer::Send(const QByteArray toSend)
{
QMutexLocker locker(&m_mutex);
return send(socketClient, toSend.data(), toSend.size(), 0);
}
QByteArray TCPServer::Recv()
{
QMutexLocker locker(&m_mutex);
QByteArray revBuff;
revBuff.clear();
char buf[MAX_BUFF];
int length = recv(socketClient, buf, MAX_BUFF, 0);
if (length > 0)
revBuff.append(buf, length);
return revBuff;
}
int TCPServer::Recv(QByteArray& toRecv, bool append)
{
QMutexLocker locker(&m_mutex);
char buf[MAX_BUFF];
int length = recv(socketClient, buf, MAX_BUFF, 0);
if (!append)
{
toRecv.clear();
toRecv.append(buf, length);
}
else
toRecv.append(buf, length);
return length;
}
QString TCPServer::getIp(bool client)
{
char ipBuf[20];
QByteArray ip;
ip = QByteArray(inet_ntop(AF_INET, &(client ? clientAddr : serverAddr).sin_addr, ipBuf, 20));
QString ipStr;
ipStr = ip;
return ipStr;
//return std::string(inet_ntop(AF_INET, &(client ? clientAddr : serverAddr).sin_addr, ipBuf, 20));
}
u_short TCPServer::getPort(bool client)
{
return ntohs((client ? clientAddr : serverAddr).sin_port);
}
QString TCPServer::getIpPort(bool client)
{
QString str = getIp(client) + QString("-%0").arg(getPort(client));
return str;
}
//
TCPClient::TCPClient()
{
}
TCPClient::~TCPClient()
{
Disconnect();
}
bool TCPClient::Connect(QString address, int port)
{
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
return false;
socketServer = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (socketServer == INVALID_SOCKET)
{
//int code = WSAGetLastError();
WSACleanup();
return false;
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(port);
inet_pton(AF_INET, address.toLocal8Bit().data(), &serverAddr.sin_addr);
int timeout = 100;
setsockopt(socketServer, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct timeval));
setsockopt(socketServer, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(struct timeval));
//u_long iMode = 1;//非阻塞模式
//ioctlsocket(socketServer, FIONBIO, &iMode);
// connect
if (connect(socketServer, (SOCKADDR*)&serverAddr, sizeof(serverAddr)) != 0)
{
int code = WSAGetLastError();
closesocket(socketServer);
WSACleanup();
return false;
}
return true;
}
void TCPClient::Disconnect()
{
if (socketServer)
closesocket(socketServer);
WSACleanup();
}
int TCPClient::Send(const QByteArray toSend)
{
QMutexLocker locker(&m_mutex);
return send(socketServer, toSend.data(), toSend.size(), 0);
}
QByteArray TCPClient::Recv()
{
QMutexLocker locker(&m_mutex);
QByteArray recBuff;
recBuff.clear();
char buf[MAX_BUFF];
int length = recv(socketServer, buf, MAX_BUFF, 0);
if (length > 0)
recBuff.append(buf, length);
return recBuff;
}
int TCPClient::Recv(QByteArray& toRecv, bool append)
{
QMutexLocker locker(&m_mutex);
char buf[MAX_BUFF];
int length = recv(socketServer, buf, MAX_BUFF, 0);
if (!append)
{
toRecv.clear();
toRecv.append(buf, length);
}
else
toRecv.append(buf, length);
return length;
}
QString TCPClient::getIp()
{
char ipBuf[20];
QByteArray ip;
ip = QByteArray(inet_ntop(AF_INET, &serverAddr.sin_addr, ipBuf, 20));
QString ipStr;
ipStr = ip;
return ipStr;
}
u_short TCPClient::getPort()
{
return ntohs(serverAddr.sin_port);
}
QString TCPClient::getIpPort()
{
QString str = getIp() + QString("-%0").arg(getPort());
return str;
}