Socket封装

来源:互联网 发布:mdf数据格式文件 编辑:程序博客网 时间:2024/05/22 06:49

socket.h

//////////////////////////

#ifndef SOCK_SOCKET_H_
#define SOCK_SOCKET_H_


// #include <standard library headers>
#include <string>

// #include <other library headers>
#ifdef _WIN32
#include <Winsock2.h>
#pragma comment(lib, "Ws2_32.lib")
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <memory.h>
#include <arpa/inet.h>
#endif

// #include "customer headers"


#ifdef _WIN32
typedef int socklen_t;
#else
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
typedef int SOCKET;
#endif


namespace sock
{

    const int MAXSEND = 1000;
    const int MAXRECV = 1000;

    class Socket
    {
    public:
        Socket(int type = SOCK_STREAM);
        virtual ~Socket();

    public:
        // Server initialization
        bool Bind(int port);
        bool Listen();
        bool Accept(Socket& ns);

        // Client initialization
        bool Connect(const std::string& host, int port);
        bool Reconnect();

        int SelectRead(long sec = -1, long usec = -1);
        int SelectWrite(long sec = -1, long usec = -1);

        // TCP
        bool Send(const std::string& s);
        bool Send(const char* buf, int len);
        int Recv(std::string& s);
        int Recv(char* buf, int len);

        // UDP
        bool SendTo(const std::string& s, const std::string& host, int port);
        bool SendTo(const char* buf, int len, const std::string& host,
                    int port);
        int RecvFrom(std::string& s, std::string& host, int& port);
        int RecvFrom(char* buf, int len, std::string& host, int& port);

        void SetNonBlocking(const bool b);
        bool IsValid() const;

        const sockaddr_in& GetSocketAddr() const { return mAddr; }
        std::string GetIp() const { return inet_ntoa(mAddr.sin_addr); }
        u_short GetPort() const { return ntohs(mAddr.sin_port); }

    private:
        bool Create(int type);
        void Close();
        int Select(int type, long sec, long usec);

        static void PrintError();
        static bool MakeSockaddr(sockaddr_in* addr, const std::string& host,
                                 int port);
        static bool ParseSockAddr(const sockaddr_in& addr, std::string& host,
                                  int& port);

    private:
        Socket(const Socket& sock);
        Socket& operator = (const Socket& sock);

    private:
        SOCKET mSock;
        sockaddr_in mAddr;
        std::string mHost;
        int mPort;

    }; // end of class Socket

}


#endif // SOCK_SOCKET_H_

//////////////////////////

 

 

原创粉丝点击