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

 1 /*
 2  * socketfactory.h
 3  *
 4  *  Created on: 2014-7-19
 5  *      Author: root
 6  */
 7
 8 #ifndef SOCKETFACTORY_H_
 9 #define SOCKETFACTORY_H_
10 #include<sys/types.h>
11
12 /*
13  * 在网路编程中, 一般分为服务端和客户端,两者的行为有相似之处,也有非常多的不同。在linux中对socket程序设计
14  * 仅提供了socket(), bind(), connection(), accept() listern() 几个函数,并未区分是服务端还是客户端。
15  * 在java或其他高级语言中,一般都对socket进行了封装,简化使用。
16  *  此端程序将linux socket进行封装在三个类中。一个工厂类,两个接口类。
17  *
18  */
19
20 namespace socketfactory {
21
22 class ISocket;
23 class IServerSocket;
24
25 /*
26  * SocketFactory 负责创建 ISocket, IServerSocket 的实例,同时负责销毁实例。
27  * SocketFactory的方法全部是静态方法。此处采用了工厂模式。
28  *
29   */
30 class SocketFactory {
31 public:
32     static ISocket* createSocket(const char* tIP, int tPort);    /* 创建客户端 socket */
33     static IServerSocket* createServerSocket(int port);            /* 创建服务端 socket */
34     static int destroy(ISocket* psocket);                                /* 销毁 */
35     static int destroy(IServerSocket* psocket);                        /* 销毁 */
36 };
37
38 /*
39  * ISocket 为接口,定义为纯虚类。面向接口编程
40  *
41  */
42
43 class ISocket {
44 public:
45     virtual int read(void* buf, size_t len)=0;        /*    读取对端数据 */
46     virtual int write(void* buf, size_t len)=0;        /* 写入对端数据 */
47     virtual int close()=0;                                    /* 关闭连接 */
48 };
49
50 /*
51  * IServerSocket 为接口,定义为纯虚类。面向接口编程。
52  *
53  */
54
55 class IServerSocket {
56 public:
57     virtual ISocket* accept()=0;                        /* 接受连接,返回与对端通信的socket */
58     virtual int listen(int backlog)=0;                /*    启动服务端口 监听 */
59     virtual int close()=0;                                /* 关闭 服务端 socket */
60 };
61
62 }
63
64 #endif /* SOCKETFACTORY_H_ */

实现类的头文件

 1 /*
 2  * socketimpl.h
 3  *
 4  *  Created on: 2014-7-20
 5  *      Author: root
 6  */
 7
 8 #ifndef SOCKETIMPL_H_
 9 #define SOCKETIMPL_H_
10 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include "socketfactory.h"
13
14 using namespace socketfactory;
15
16 /*
17  * ISocket 和 IServerSocket 的实现类。
18  *  SocketFactory工厂类创建这些实现类。
19  *
20  */
21
22 class SocketImpl: public ISocket
23 {
24 public:
25     int read(void* buf, size_t len);
26     int write(void* buf, size_t len);
27     int close();
28
29     int ssocket(int domain, int type, int protocol);
30     int cconnect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
31     SocketImpl();
32     virtual ~SocketImpl();
33
34 public:
35     int fd;
36     struct sockaddr  address;
37 };
38
39
40 class ServerSocketImpl: public IServerSocket
41 {
42 public:
43     ISocket* accept();
44     int listen(int backlog);
45     int close();
46
47
48     int ssocket(int domain, int type, int protocol);
49     int bbind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
50     ServerSocketImpl();
51     virtual ~ServerSocketImpl();
52
53 public:
54     int fd;
55     struct sockaddr  address;
56 };
57
58
59 #endif /* SOCKETIMPL_H_ */
  1 /*
  2  * socketimpl.cpp
  3  *
  4  *  Created on: 2014-7-20
  5  *      Author: root
  6  */
  7
  8 #include <unistd.h>
  9 #include "socketimpl.h"
 10 #include <sys/types.h>
 11 #include <sys/socket.h>
 12
 13 #include <stdio.h>
 14
 15
 16 SocketImpl::SocketImpl() {
 17     this->fd = -1;
 18 }
 19
 20 int SocketImpl::ssocket(int domain, int type, int protocol) {
 21     this->fd = socket(domain, type, protocol);
 22     if (this->fd < 0)
 23         perror("SocketImpl::ssocket");
 24     return this->fd;
 25 }
 26
 27 int SocketImpl::cconnect(int sockfd, const struct sockaddr *addr,
 28         socklen_t addrlen) {
 29     int ret = connect(sockfd, addr, addrlen);
 30     if (ret != 0)
 31         perror("SocketImpl::cconnect");
 32     return ret;
 33
 34 }
 35
 36 SocketImpl::~SocketImpl() {
 37
 38 }
 39
 40 int SocketImpl::read(void* buf, size_t len) {
 41     int ret = ::read(this->fd, buf, len);
 42     if (ret < 0)
 43         perror("SocketImpl::read");
 44     return ret;
 45 }
 46 ;
 47
 48 int SocketImpl::write(void* buf, size_t len) {
 49     int ret = ::write(this->fd, buf, len);
 50     if (ret < 0)
 51         perror("SocketImpl::write");
 52     return ret;
 53 }
 54 ;
 55
 56 int SocketImpl::close() {
 57     if (this->fd > 0) {
 58         int ret = ::close(this->fd);
 59         if (ret != 0)
 60         {
 61             perror("SocketImpl::close");
 62             return ret;
 63         }else
 64             this->fd = -1;
 65     }
 66     return 0;
 67 }
 68
 69 ServerSocketImpl::ServerSocketImpl() {
 70     this->fd = 0;
 71 }
 72
 73 ServerSocketImpl::~ServerSocketImpl() {
 74 }
 75
 76 int ServerSocketImpl::ssocket(int domain, int type, int protocol) {
 77     this->fd = socket(domain, type, protocol);
 78     if (this->fd < 0)
 79         perror("ServerSocketImpl::ssocket");
 80     return this->fd;
 81 }
 82
 83 int ServerSocketImpl::bbind(int sockfd, const struct sockaddr *addr,
 84         socklen_t addrlen) {
 85     int ret = bind(this->fd, addr, addrlen);
 86     if (ret < 0)
 87         perror("ServerSocketImpl::bbind");
 88     return ret;
 89 }
 90
 91 ISocket* ServerSocketImpl::accept() {
 92     SocketImpl* nsocket = new SocketImpl();
 93     int addlen=0;
 94     int nfd =  ::accept(this->fd, &nsocket->address, (socklen_t*)&addlen);
 95     if (nfd == -1) {
 96         delete nsocket;
 97         perror("ServerSocketImpl::accept");
 98         return NULL;
 99     }
100     nsocket->fd = nfd;
101     return nsocket;
102 }
103
104 int ServerSocketImpl::listen(int backlog) {
105     int ret = ::listen(this->fd, backlog);
106     if (ret < 0)
107         perror("ServerSocketImpl::listen");
108     return ret;
109 }
110
111 int ServerSocketImpl::close() {
112     if(this->fd >0 )
113     {
114         int ret=::close(this->fd);
115         if(ret!=0 )
116         {
117             perror("ServerSocketImpl::close");
118             return ret;
119         }else
120             this->fd =-1;
121     }
122     return 0;
123 }
/*
 * 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, 0, 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) <= 0){
        delete nsocket;
        perror("SocketFactory::createSocket:inet_pton");
        return NULL;
    }
    int ret=nsocket->ssocket(AF_INET, SOCK_STREAM, 0);
    if(ret < 0 ){
        perror("SocketFactory::createSocket:ssocket");
        delete nsocket;
        return NULL;
    }
    ret=nsocket->cconnect(nsocket->fd, &nsocket->address, sizeof(sockaddr));
    if(ret < 0 ){
        perror("SocketFactory::createSocket:cconnect");
        nsocket->close();
        delete nsocket;
        return NULL;
    }
    return nsocket;
}

IServerSocket* SocketFactory::createServerSocket(int port)
{
    ServerSocketImpl *nssocket=new ServerSocketImpl();
    memset(&nssocket->address, 0, 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, 0);
    if(ret<0){
        perror("SocketFactory::createServerSocket:ssocket");
        delete nssocket;
        return NULL;
    }
    ret=nssocket->bbind(nssocket->fd, &nssocket->address, sizeof(sockaddr));
    if(ret<0){
        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 0;
}

int SocketFactory::destroy(IServerSocket* psocket)
{
    ServerSocketImpl* pssocket=(ServerSocketImpl*)psocket;
    pssocket->close();
    delete pssocket;
    return 0;
}
时间: 2024-08-25 09:32:57

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

Java网络编程从入门到精通(18):Socket类的getter和setter方法(2)

二.用于获得和设置Socket选项的getter和setter方法 Socket选择可以指定Socket类发送和接受数据的方式.在JDK1.4中共有8个Socket选择可以设置.这8个选项都定义在java.net.SocketOptions接口中.定义如下: public final static int TCP_NODELAY = 0x0001;    public final static int SO_REUSEADDR = 0x04;    public final static int

操作类封装

/*操作类封装 */ /*调用方法 如下: * var str= new IStrManipulation();//实例化字符串处理接口 * console.log(str.StrManipulation('StrManipulation',"111sss23123").getLength()); * var convert =new IConvert();//实例化类型转换接口 * console.log(convert.Convert('Convert',"1112312

使用Socket类接收和发送数据

网络应用分为客户端和服务端两部分,而Socket类是负责处理客户端通信的Java类.通过这个类可以连接到指定IP或域名的服务器上,并且可以和服务器互相发送和接受数据.在本文及后面的数篇文章中将详细讨论Socket类的使用,内容包括Socket类基础.各式各样的连接方式.get和set方法.连接过程中的超时以及关闭网络连接等. 在本文中,我们将讨论使用Socket类的基本步骤和方法.一般网络客户端程序在连接服务程序时要进行以下三步操作. 1.连接服务器 2.发送和接收数据 3.关闭网络连接 一.连

Java网络编程从入门到精通(13):使用Socket类接收和发送数据

网络应用分为客户端和服务端两部分,而Socket类是负责处理客户端通信的Java类.通过这个类可以连接到指定IP或域名的服务器上,并且可以和服务器互相发送和接受数据.在本文及后面的数篇文章中将详细讨论Socket类的使用,内容包括Socket类基础.各式各样的连接方式.get和set方法.连接过程中的超时以及关闭网络连接等. 在本文中,我们将讨论使用Socket类的基本步骤和方法.一般网络客户端程序在连接服务程序时要进行以下三步操作. 1.         连接服务器 2.         发送

面向对象:封装(一):构造函数;类的主方法;权限修饰符;对象的创建

对象:一切皆为对象. 对象包括两部分内容:属性(名词形容词),行为(动词). 对象和对象之间是有关系的. 派生(子类与父类的关系),关联,依赖. 类:对同一类别的众多对象的一种抽象. 类,还是用来生成对象的一种模板,对象是类的一种具体化的表现. 面向对象的三大特性:封装,继承,多态. 一:类的定义: class 类名{ 访问修饰符 成员变量的定义; 访问修饰符 成员函数(方法)的定义; } 例1: public class Student{ public String name; public

Linux组件封装(八)——Socket的封装

我们要封装Socket,首先我们需要了解Socket需要哪些要素: 1) 首先,一个套接字创建后,需要绑定一块网卡的IP,以及连接的对口号,所以我们先封装InetAddr. 在class中,仅有的一个私有成员就是struct sockaddr_in类型的一个对象,我们需要将该对象的几种赋值与创建封装到类中,这样,我们仅需传递相应的IP与port即可获得一个addr. 在这里,我们为了方便获得该addr的IP及port,封装几个将addr转化为IP及port的函数,这样我们仅需调用函数即可. 然后

面向对象(类,封装,this,构造方法)

无论面向对象还是面向过程, 这俩都是解决问题的思路而已, 只是角度不同. 面向过程: 强调解决问题的每一个步骤都亲力亲为,每一个细节都自己手动实现. 面向对象: 使用特定功能对象去解决特定的问题, 每一个细节不需要关注,只需要创建对应的对象即可. 面向对象是基于面向过程的 类和对象及他们的关系 类: 具有相同特征和行为(功能)的事物的统称 , 是一个抽象概念 对象: 这类事物中某个确定的个体 类和对象的关系 一个类可以创建多个对象 , 类是对象的抽象, 对象是类的实例. 描述一个事物---->

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

一.Python面向对象中的新旧式类 1)新式类(推荐使用):在定义类时,类后边括号里要继承基类(object).在python3.x中若没有指定父类,会默认使用的是object作为基类:在python2.x中,若没指定父类,则不会以object作为基类. 2)旧式类(经典类):在定义类时,类后边括号中不用继承object类,甚至不用括号. 3)dir方法是用来查看类的内置方法. 二.私有方法和属性 1)私有属性是对象不希望公开的属性:私有方法是对象不希望公开的方法.在定义私有属性和方法时,在属

类(面向对象、增删改查、继承、多态、封装、反射)

类的增删改查可以操作类里面的函数.数据属性,实例. 1.函数与类的区别 函数实现功能的模块化 类是实现功能和数据的模块化 ——init——类的构造:初始化类,实例化的时候自动执行2.类用法 4.什么是类, 类是把一类事物的相同的特征和同一动作整合到一类,类是抽象的5.什么是对象 对象就是基于类创建的一种事物,也是特征和动作整合到一起的6.面向对象 就是__init__独特构造7.类和对象的关系 对象都是类产生的.8.实例化:有类产生的过程叫实例化9.新式类类名后面括号里加object10.什么是