Linux socket 类封装 (面向对象方法)

时间:2022-02-21 04:20:33
 /*
* socketfactory.h
*
* Created on: 2014-7-19
* Author: root
*/ #ifndef SOCKETFACTORY_H_
#define SOCKETFACTORY_H_
#include<sys/types.h> /*
* 在网路编程中, 一般分为服务端和客户端,两者的行为有相似之处,也有非常多的不同。在linux中对socket程序设计
* 仅提供了socket(), bind(), connection(), accept() listern() 几个函数,并未区分是服务端还是客户端。
* 在java或其他高级语言中,一般都对socket进行了封装,简化使用。
* 此端程序将linux socket进行封装在三个类中。一个工厂类,两个接口类。
*
*/ namespace socketfactory { class ISocket;
class IServerSocket; /*
* SocketFactory 负责创建 ISocket, IServerSocket 的实例,同时负责销毁实例。
* SocketFactory的方法全部是静态方法。此处采用了工厂模式。
*
*/
class SocketFactory {
public:
static ISocket* createSocket(const char* tIP, int tPort); /* 创建客户端 socket */
static IServerSocket* createServerSocket(int port); /* 创建服务端 socket */
static int destroy(ISocket* psocket); /* 销毁 */
static int destroy(IServerSocket* psocket); /* 销毁 */
}; /*
* ISocket 为接口,定义为纯虚类。面向接口编程
*
*/ class ISocket {
public:
virtual int read(void* buf, size_t len)=; /* 读取对端数据 */
virtual int write(void* buf, size_t len)=; /* 写入对端数据 */
virtual int close()=; /* 关闭连接 */
}; /*
* IServerSocket 为接口,定义为纯虚类。面向接口编程。
*
*/ class IServerSocket {
public:
virtual ISocket* accept()=; /* 接受连接,返回与对端通信的socket */
virtual int listen(int backlog)=; /* 启动服务端口 监听 */
virtual int close()=; /* 关闭 服务端 socket */
}; } #endif /* SOCKETFACTORY_H_ */

实现类的头文件

 /*
* socketimpl.h
*
* Created on: 2014-7-20
* Author: root
*/ #ifndef SOCKETIMPL_H_
#define SOCKETIMPL_H_
#include <sys/socket.h>
#include <netinet/in.h>
#include "socketfactory.h" using namespace socketfactory; /*
* ISocket 和 IServerSocket 的实现类。
* SocketFactory工厂类创建这些实现类。
*
*/ class SocketImpl: public ISocket
{
public:
int read(void* buf, size_t len);
int write(void* buf, size_t len);
int close(); int ssocket(int domain, int type, int protocol);
int cconnect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
SocketImpl();
virtual ~SocketImpl(); public:
int fd;
struct sockaddr address;
}; class ServerSocketImpl: public IServerSocket
{
public:
ISocket* accept();
int listen(int backlog);
int close(); int ssocket(int domain, int type, int protocol);
int bbind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
ServerSocketImpl();
virtual ~ServerSocketImpl(); public:
int fd;
struct sockaddr address;
}; #endif /* SOCKETIMPL_H_ */
 /*
* socketimpl.cpp
*
* Created on: 2014-7-20
* Author: root
*/ #include <unistd.h>
#include "socketimpl.h"
#include <sys/types.h>
#include <sys/socket.h> #include <stdio.h> SocketImpl::SocketImpl() {
this->fd = -;
} int SocketImpl::ssocket(int domain, int type, int protocol) {
this->fd = socket(domain, type, protocol);
if (this->fd < )
perror("SocketImpl::ssocket");
return this->fd;
} int SocketImpl::cconnect(int sockfd, const struct sockaddr *addr,
socklen_t addrlen) {
int ret = connect(sockfd, addr, addrlen);
if (ret != )
perror("SocketImpl::cconnect");
return ret; } SocketImpl::~SocketImpl() { } int SocketImpl::read(void* buf, size_t len) {
int ret = ::read(this->fd, buf, len);
if (ret < )
perror("SocketImpl::read");
return ret;
}
; int SocketImpl::write(void* buf, size_t len) {
int ret = ::write(this->fd, buf, len);
if (ret < )
perror("SocketImpl::write");
return ret;
}
; int SocketImpl::close() {
if (this->fd > ) {
int ret = ::close(this->fd);
if (ret != )
{
perror("SocketImpl::close");
return ret;
}else
this->fd = -;
}
return ;
} ServerSocketImpl::ServerSocketImpl() {
this->fd = ;
} ServerSocketImpl::~ServerSocketImpl() {
} int ServerSocketImpl::ssocket(int domain, int type, int protocol) {
this->fd = socket(domain, type, protocol);
if (this->fd < )
perror("ServerSocketImpl::ssocket");
return this->fd;
} int ServerSocketImpl::bbind(int sockfd, const struct sockaddr *addr,
socklen_t addrlen) {
int ret = bind(this->fd, addr, addrlen);
if (ret < )
perror("ServerSocketImpl::bbind");
return ret;
} ISocket* ServerSocketImpl::accept() {
SocketImpl* nsocket = new SocketImpl();
int addlen=;
int nfd = ::accept(this->fd, &nsocket->address, (socklen_t*)&addlen);
if (nfd == -) {
delete nsocket;
perror("ServerSocketImpl::accept");
return NULL;
}
nsocket->fd = nfd;
return nsocket;
} int ServerSocketImpl::listen(int backlog) {
int ret = ::listen(this->fd, backlog);
if (ret < )
perror("ServerSocketImpl::listen");
return ret;
} int ServerSocketImpl::close() {
if(this->fd > )
{
int ret=::close(this->fd);
if(ret!= )
{
perror("ServerSocketImpl::close");
return ret;
}else
this->fd =-;
}
return ;
}
/*
* socketfactory.cpp
*
* Created on: 2014-7-20
* Author: root
*/ #include "socketfactory.h"
#include "socketimpl.h" #include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>
#include <arpa/inet.h> ISocket* SocketFactory::createSocket(const char* tIP, int tPort)
{
SocketImpl* nsocket=new SocketImpl();
memset(&nsocket->address, , sizeof(sockaddr));
struct sockaddr_in* padd=(sockaddr_in*)(&nsocket->address);
padd->sin_family=AF_INET;
padd->sin_port=htons(tPort);
if( inet_pton(AF_INET, tIP, &padd->sin_addr) <= ){
delete nsocket;
perror("SocketFactory::createSocket:inet_pton");
return NULL;
}
int ret=nsocket->ssocket(AF_INET, SOCK_STREAM, );
if(ret < ){
perror("SocketFactory::createSocket:ssocket");
delete nsocket;
return NULL;
}
ret=nsocket->cconnect(nsocket->fd, &nsocket->address, sizeof(sockaddr));
if(ret < ){
perror("SocketFactory::createSocket:cconnect");
nsocket->close();
delete nsocket;
return NULL;
}
return nsocket;
} IServerSocket* SocketFactory::createServerSocket(int port)
{
ServerSocketImpl *nssocket=new ServerSocketImpl();
memset(&nssocket->address, , sizeof(sockaddr));
struct sockaddr_in* padd=(sockaddr_in*)(&nssocket->address);
padd->sin_family=AF_INET;
padd->sin_addr.s_addr=htonl(INADDR_ANY);
padd->sin_port=htons(port);
int ret=nssocket->ssocket(AF_INET, SOCK_STREAM, );
if(ret<){
perror("SocketFactory::createServerSocket:ssocket");
delete nssocket;
return NULL;
}
ret=nssocket->bbind(nssocket->fd, &nssocket->address, sizeof(sockaddr));
if(ret<){
perror("SocketFactory::createServerSocket:bbind");
nssocket->close();
delete nssocket;
return NULL;
}
return nssocket;
} int SocketFactory::destroy(ISocket* psocket)
{
SocketImpl* psockimpl=(SocketImpl*)psocket;
psockimpl->close();
delete psockimpl;
return ;
} int SocketFactory::destroy(IServerSocket* psocket)
{
ServerSocketImpl* pssocket=(ServerSocketImpl*)psocket;
pssocket->close();
delete pssocket;
return ;
}