/*
* 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. 利用 HTML5 WebGL 构建的 3D 拓扑图

    现在,3D 模型已经用于各种不同的领域.在医疗行业使用它们制作器官的精确模型:电影行业将它们用于活动的人物.物体以及现实电影:视频游戏产业将它们作为计算机与视频游戏中的资源:在科学领域将它们作为化合物 ...

  2. 解决eclipse出现This Android SDK requires Andro...date ADT to the latest version.问题

    更新完android SDK之后,eclipse出现了“This Android SDK requires Andro...date ADT to the latest version.”问题,这是因 ...

  3. React Native学习(八)—— 对接七鱼客服

    本文基于React Native 0.52 Demo上传到Git了,有需要可以看看,写了新内容会上传的.Git地址 https://github.com/gingerJY/React-Native-D ...

  4. get最简单直接粗爆git与github教程

    Git是分布式版本控制系统(可以理解为文件管理拓展工具) github一个在线文件托管系统(可以理解为一个在线云盘) 准备工作,在git官网下载git软件件,安装git软件,以windows.为例,下 ...

  5. FORTH运算符

    body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...

  6. java面向对象——类

    一.类 类(class)是构造对象的模板或蓝图.由类构造(construct)对象的过程称为创建类的实例(instance). 用 java 编写的所有代码都位于某个类的内部.标准的Java 库提供了 ...

  7. js控制滚动条滑动

    window.scrollTo(0,document.body.scrollHeight);或者通过设置Location的hash属性参见:http://www.cnblogs.com/oospace ...

  8. 【转】CString,string,char*综合比较

    (一)  概述 1.string和CString均是字符串模a板类: 2.string为标准模板类(STL)定义的字符串类,已经纳入C++标准之中: 3.CString(typedef CString ...

  9. tomcat监控(二)

    标签: linux 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 这里介绍二种监控Tomcat的方法 使用windows版本的jdk监控 使用zabbix监控 ...

  10. Bom部分

    Bom部分  browser object model(浏览器对象模型)将浏览器中的各个组件封装成对象,我们可以使用这些对象来操作浏览器的一些动作.封装的规则基于ECMAScriptBom的组成--— ...