/*
* 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 ;
}

Linux socket 类封装 (面向对象方法)的更多相关文章

  1. 封装获取网络信息Linux—API类

    封装获取网络信息Linux—API类 封装好的库: #ifndef NETINFORMATION_H #define NETINFORMATION_H #include <netdb.h> ...

  2. Socket类 以及 ServerSocket类 讲解

    Socket类 套接字是网络连接的端点,套接字使应用可以从网络中读取数据,可以向网络中写入数据.不同计算机上的两个应用程序可以通过连接发送或接收字节流,以此达到相互通信的目的. 为了从一个应用程序向另 ...

  3. luogg_java学习_05_面向对象(方法和类)

    这篇总结断断续续写了2天,内容来自Oracle java8编程入门官方教程和课外搜索总结,希望自己以后返回来看的时候都懂,也希望可以起到帮助初学者的作用. 转载请注明 出自 luogg的博客园 , 因 ...

  4. 【iOS 开发】Objective - C 面向对象 - 方法 | 成员变量 | 隐藏封装 | KVC | KVO | 初始化 | 多态

    一. Objective-C 方法详解 1. 方法属性 (1) OC 方法传参机制 Object-C 方法传参机制 : OC 中得参数传递都是值传递, 传入参数的是参数的副本; -- 基本类型 (值传 ...

  5. Python面向对象 --- 新旧式类、私有方法、类属性和类方法、静态方法

    一.Python面向对象中的新旧式类 1)新式类(推荐使用):在定义类时,类后边括号里要继承基类(object).在python3.x中若没有指定父类,会默认使用的是object作为基类:在pytho ...

  6. python(面向对象-类封装调用)

    一.面对对象思想 (1)大家肯定听过 Python 中”一切皆对象“的说法,但可能并不了解它的具体含义,只是在学习的时候听说 Python 是面向对象的编程语言,本节将向大家详细介绍 Python 面 ...

  7. day22:面向对象封装对象操作&类操作&面向对象删除操作

    面向对象程序开发 1.类的三种定义方式 class MyClass: pass class MyClass(): #(推荐) pass class MyClass(object): # object类 ...

  8. 第7.8节 Python中隐秘的类封装方法

    前面章节已经介绍了Python中的多态和继承,本节将介绍面向对象程序设计OOP三大特征的另一个特征--封装. 一.    概念 封装是将对象的状态信息(也就是数据.属性)隐藏在对象内部,将对象的属性和 ...

  9. php部分---面向对象静态、抽象类、oop接口、加载类、魔术方法、关键字。

    静态  static关键字 普通成员普通成员是属于对象的 静态成员静态成员是属于类的 普通方法里面可以调用静态成员静态方法里面不能调用普通成员self关键字 在类里面代表该类 普通类class Ren ...

随机推荐

  1. CCF系列之数列分段(201509-1)

    试题名称: 数列分段 试题编号: 201509-1 时间限制: 1.0s 内存限制: 256.0MB 问题描述 给定一个整数数列,数列中连续相同的最长整数序列算成一段,问数列中共有多少段? 输入格式 ...

  2. mysql 多列索引的生效规则

    mysql中 myisam,innodb默认使用的是 Btree索引,至于btree的数据结构是怎样的都不重要,只需要知道结果,既然是索引那这个数据结构最后是排好序:就像新华字典他的目录就是按照a,b ...

  3. java -cp用法

    原文出处:http://blog.csdn.net/zhuying_linux/article/details/7714194.感谢作者的分享 java -cp classpath Specify a ...

  4. 使用EndNote在Word中插入参考文献的格式设置

    endnote其实自带了很多参考文献格式的样式,如下图,但往往跟我们要使用的会有所出入,本文主要介绍的就是设置自定义endnote参考文献格式,以endnote X6和word2003为例,其它版本以 ...

  5. 在CentOS7上实现NFS共享

    一.介绍 NFS 是Network File System的缩写,即网络文件系统.一种使用于分散式文件系统的协定,功能是让客户端通过网络访问不同主机上磁盘里的数据,主要用在类Unix系统上实现文件共享 ...

  6. 使用Z3破解简单的XOR加密

    使用Z3破解简单的XOR加密 翻译:无名侠 原文地址: https://yurichev.com/blog/XOR_Z3/ 如果我们有一段用简单XOR加密过的文本,怎么寻找密钥呢?密钥的长度可能很长, ...

  7. LANMP系列教程之MySQL编译安装CentOS7环境

      以MySQL5.5.33版本为例 1.准备工作: 1.首先准备好源代码包 2.并且确保已安装好 "开发工具" 包组和cmake编译工具 3.确保安装好ncurses-devel ...

  8. C语言深度剖析-笔记

    关键字: C语言关键字32个: 关键字                                         意 义 auto                           声明自动变 ...

  9. HTTP面试题都在这里

    HTTP常见面试题 Http与Https的区别: Http与Https的区别: HTTP 的URL 以http:// 开头,而HTTPS 的URL 以https:// 开头 HTTP 是不安全的,而 ...

  10. c# Nlog 非xml cs方法配置

    public static void InitLog(TargetWithLayout target = null, string level = "Debug", string ...