/*
* 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. 开源三维地球GIS引擎Cesium常用功能的开发

    Cesium是一个非常优秀的三维地球GIS引擎(开源且免费).能够加载各种符合标准的地图图层,瓦片图.矢量图等都支持.支持3DMax等建模软件生成的obj文件,支持通用的GIS计算:支持DEM高程图. ...

  2. 引用类型之数组array最全的详解

    Array类型 今天总结一下array类型. js中的数组是有着非常强大的功能.具有很大的灵活性,有两个方面的特点 1.数组的每一项可以保存任何的数据类型:2.数组大小可以动态的调整:看下面的例子: ...

  3. Linux指令--rm, rmdir

    rm是常用的命令,该命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目录及其下的所有文件及子目录均删除.对于链接文件,只是删除了链接,原有文件均保持不变.rm是一个危险的命令,使用的时 ...

  4. myeclipse环境搭建

    公司来了几个新人,老是在教他们环境搭建这些,每次在帮他们调试代码的时候老是不厌其烦的看着他们坐等myeclipse编译了,校验了什么的,而且在编码的时候也不使用快捷键,然后我就只能默默的坐回去了.为了 ...

  5. 错误:Unsupported major.minor version 51.0的解决

    问题: 在电脑上双击打开一个可执行的jar时报错:Unsupported major.minor version 51.0.原因是版本问题,该jar使用jdk1.7编译,而我电脑装的是jdk1.6. ...

  6. 怎样查看MYSQL数据库的端口号

    show variables like '%port%';

  7. 新awk整理

    总感觉上一篇awk的总结几乎是照着man翻译过来的,惨不忍睹 无意间在互联网上有找到了宝贵的资料 感觉整理的很好,想着照着这个来重新写下,对照新的man更新下吧,只是总是在改变的 一.awk简介二.a ...

  8. bzoj 4562 [Haoi2016]食物链

    4562: [Haoi2016]食物链 Time Limit: 10 Sec  Memory Limit: 128 MB Description 如图所示为某生态系统的食物网示意图,据图回答第1小题 ...

  9. mkdir与mkdirs的区别

    mkdir与mkdirs的区别 项目中需要在代码中读取或创建文件保存路径,用到了mkdir,查看还有个mkdirs方法,这里记录一下两者的区别. 1.关于两者的说明如下: boolean mkdir( ...

  10. Cypher查询语言--Neo4j-WHERE(三)

    目录 Where Boolean 操作类型 节点属性上的过滤 正则表达式 转义正则表达式 不分大小些正则表达式 关系类型上的过滤 属性存在性 如果缺失属性默认为true 如果缺失属性默认为false ...