Linux socket 类封装 (面向对象方法)
/*
* 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 类封装 (面向对象方法)的更多相关文章
- 封装获取网络信息Linux—API类
封装获取网络信息Linux—API类 封装好的库: #ifndef NETINFORMATION_H #define NETINFORMATION_H #include <netdb.h> ...
- Socket类 以及 ServerSocket类 讲解
Socket类 套接字是网络连接的端点,套接字使应用可以从网络中读取数据,可以向网络中写入数据.不同计算机上的两个应用程序可以通过连接发送或接收字节流,以此达到相互通信的目的. 为了从一个应用程序向另 ...
- luogg_java学习_05_面向对象(方法和类)
这篇总结断断续续写了2天,内容来自Oracle java8编程入门官方教程和课外搜索总结,希望自己以后返回来看的时候都懂,也希望可以起到帮助初学者的作用. 转载请注明 出自 luogg的博客园 , 因 ...
- 【iOS 开发】Objective - C 面向对象 - 方法 | 成员变量 | 隐藏封装 | KVC | KVO | 初始化 | 多态
一. Objective-C 方法详解 1. 方法属性 (1) OC 方法传参机制 Object-C 方法传参机制 : OC 中得参数传递都是值传递, 传入参数的是参数的副本; -- 基本类型 (值传 ...
- Python面向对象 --- 新旧式类、私有方法、类属性和类方法、静态方法
一.Python面向对象中的新旧式类 1)新式类(推荐使用):在定义类时,类后边括号里要继承基类(object).在python3.x中若没有指定父类,会默认使用的是object作为基类:在pytho ...
- python(面向对象-类封装调用)
一.面对对象思想 (1)大家肯定听过 Python 中”一切皆对象“的说法,但可能并不了解它的具体含义,只是在学习的时候听说 Python 是面向对象的编程语言,本节将向大家详细介绍 Python 面 ...
- day22:面向对象封装对象操作&类操作&面向对象删除操作
面向对象程序开发 1.类的三种定义方式 class MyClass: pass class MyClass(): #(推荐) pass class MyClass(object): # object类 ...
- 第7.8节 Python中隐秘的类封装方法
前面章节已经介绍了Python中的多态和继承,本节将介绍面向对象程序设计OOP三大特征的另一个特征--封装. 一. 概念 封装是将对象的状态信息(也就是数据.属性)隐藏在对象内部,将对象的属性和 ...
- php部分---面向对象静态、抽象类、oop接口、加载类、魔术方法、关键字。
静态 static关键字 普通成员普通成员是属于对象的 静态成员静态成员是属于类的 普通方法里面可以调用静态成员静态方法里面不能调用普通成员self关键字 在类里面代表该类 普通类class Ren ...
随机推荐
- Python3基础知识之字符串
1.运算符 * >>> b=a*5>>> b'pythonpythonpythonpythonpython'>>> b.replace('t',' ...
- 什么是bgp线路
https://www.douban.com/note/319956581/ BGP(边界网关协议)主要用于互联网AS(自治系统)之间的互联,BGP的最主要功能在于控制路由的传播和选择最好的路由.中国 ...
- hexo部署github和gitment操作简单介绍
优点: 快速高效 支持markdown 布局自定义简单,无广告 部署简单 因为想开始写博客,但又找不到好的博客平台,平时都看博客园和开源中国看博客文章,但博客园的那个皮肤是真有点难受,所以就想自己打个 ...
- js_7_dom文本
dom编程核心? 绑定事件 找到标签(innerText找标签里文本内容,innerHTML找标签里所有内容) 特殊的标签:input,select,textarea ,通过.value来获取值 3. ...
- Java中Unsafe类详解
http://www.cnblogs.com/mickole/articles/3757278.html Java不能直接访问操作系统底层,而是通过本地方法来访问.Unsafe类提供了硬件级别的原子操 ...
- junit4X系列--Runner解析
前面我整理了junit38系列的源码,那junit4X核心代码也基本类似.这里我先转载一些关于junit4X源码解析的好文章.感谢原作者的分享.原文地址:http://www.blogjava.net ...
- DAY10-万物皆对象-2018-2-2
许久没有写了,虽然每天都有在学,但是学的东西也少了,后面难度慢慢加大,学习速度也是变慢了.这是许多天积累下来的笔记,从第一次接触对象,到慢慢去了解,现在处于还待深入了解的状态.万物皆对象,那是不是说没 ...
- Android Camera2采集摄像头原始数据并手动预览
Android Camera2采集摄像头原始数据并手动预览 最近研究了一下android摄像头开发相关的技术,也看了Google提供的Camera2Basic调用示例,以及网上一部分代码,但都是在Te ...
- 4个强大的Linux服务器监控工具[转]
本文介绍了一些可以用来监控网络使用情况的Linux命令行工具.这些工具可以监控通过网络接口传输的数据,并测量目前哪些数据所传输的速度.入站流量和出站流量分开来显示. 一些命令可以显示单个进程所使用的带 ...
- cmd中控制某个命令执行多少次
for /l %i in (1,1,5) do calc五次启动计算器程序for /l %i in (start ,step,end) do commandfor /l 表示从数字start开始,以s ...