Linux 服务器 socket封装

一、Socket网络封装

  1、CHostAddress类 地址类 包括获取端口号和sockaddr_in 结构体(通信协议,端口,IP地址)

  CHostAddress.h

代码实现:

-----------------------------------------------------------------------

#pragma once
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>

class CHostAddress
{
public:
CHostAddress(unsigned short port);
~CHostAddress();
void setPort(unsigned short port);
unsigned short getPort();
struct sockaddr_in getAddr_in();
const struct sockaddr* getAddr();
private:
unsigned short port;//端口号
struct sockaddr_in s_addr;//结构体

};

-----------------------------------------------------------

CHostAddress.cpp

代码实现:

#include "CHostAddress.h"

CHostAddress::CHostAddress(unsigned short port)
{
this->port = port;
this->s_addr.sin_family = AF_INET;
this->s_addr.sin_port = htons(this->port);
this->s_addr.sin_addr.s_addr = INADDR_ANY;
}

CHostAddress::~CHostAddress()
{
}

void CHostAddress::setPort(unsigned short port)
{
this->port = port;
}

unsigned short CHostAddress::getPort()
{
return this->port;
}

sockaddr_in CHostAddress::getAddr_in()
{
return this->s_addr;
}

const sockaddr* CHostAddress::getAddr()
{
return (sockaddr *)&(this->s_addr);
}

 2、CBaseSocket类  socket基础类 作为基类

 CBaseSocket.h

代码实现:

#pragma once
#include <string.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>


class CBaseSocket
{
public:
CBaseSocket(unsigned short port);//端口号 在调用的时候赋值
~CBaseSocket();
int getSocketfd();
void SocketStart();//网络启动
virtual void Run()=0;//开始
virtual void Stop()=0;//停止

protected:
int socketfd;

};

 

 CBaseSocket.cpp

#include "CBaseSocket.h"

CBaseSocket::CBaseSocket(unsigned short port)
{
this->socketfd = 0;

}

int CBaseSocket::getSocketfd()
{
return this->socketfd;
}

void CBaseSocket::SocketStart()
{
this->socketfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (this->socketfd < 0)
{
perror("socket error:");
}
this->Run();
}
CBaseSocket::~CBaseSocket()
{

}

3、CTcpSocket类 CTcpSocket.h

#pragma once
#include "CBaseSocket.h"
#include "CHostAddress.h"
#include <unistd.h>
#include <stdio.h>
#include <iostream>
#define LISTEN_MAX_NUM 10
using namespace std;
class CTcpSever:public CBaseSocket
{
public:
CTcpSever(unsigned short port);
~CTcpSever();
void Run();
void Stop();
CHostAddress* getAddress();
void setAddress(CHostAddress* address);

private:
CHostAddress* address;
};

 CTcpSever.cpp

代码实现:

#include "CTcpSever.h"

CTcpSever::CTcpSever(unsigned short port):CBaseSocket(port)
{
this->address = new CHostAddress(port);
}

CTcpSever::~CTcpSever()
{
}

void CTcpSever::Run()
{
int val = 1;
int new_fd = setsockopt(this->socketfd, SOL_SOCKET, SO_REUSEADDR, (const void*)&val, sizeof(val));
cout <<"new_fd=" << new_fd <<endl;
if (new_fd <0)
{
perror("setsockopt error:");
}
int new_fd_1 = bind(this->socketfd, this->address->getAddr(), sizeof(this->address->getAddr_in()));
cout << "new_fd_1=" << new_fd_1 << endl;
if (new_fd_1 <0)
{
perror("bind error:");
}
int new_fd_2= listen(this->socketfd, LISTEN_MAX_NUM);
cout << "new_fd_2" << new_fd_2 << endl;
if (new_fd_2 <0)
{
perror("listen error:");
}

}

void CTcpSever::Stop()
{
if (this->socketfd != 0)
{
close(this->socketfd);
this->socketfd = 0;
}
}

CHostAddress* CTcpSever::getAddress()
{
return this->address;
}

void CTcpSever::setAddress(CHostAddress* address)
{
this->address = address;
}

上一篇:22 网络编程


下一篇:java利用socket实现长运行服务器端接收客户端数据