cocos2dx 跨平台Socket

前端之家收集整理的这篇文章主要介绍了cocos2dx 跨平台Socket前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Sever和Client用的是同一个类,只是使用的函数不同

Sever运行的函数顺序是:

Socket.Init();                                      //初始化库 
Socket.Create(AF_INET,SOCK_STREAM,0);               //创建socket 
Socket.Bind(5010);                                  //绑定端口 
Socket.Listen();                                    //开启监听 
Socket.Accept(clientsocket,ipClient);               //接受一个客户端请求 
clientsocket.Recv(recvBuf,64,0);                    //发送消息 
clientsocket.Send("lidaha",strlen("lihaha")+1,0);   //接收消息
Socket.Close();                                     //关闭socket
Socket.Clean();                                     //释放库

Client运行的函数顺序是:

Socket.Init();                                      //初始化库
Socket.Create(AF_INET,0);               //创建socket
Socket.Connect("127.0.0.1",5010);                  //链接服务器  
Socket.Send("lidaha",strlen("lidaha")+1,0);        //发送数据  
Socket.Recv(buf,0);                              //接收数据
Socket.Close();                                     //关闭socket
Socket.Clean();                                     //释放库

Socket类:

Socket.h

#ifndef _ODSOCKET_H_
#define _ODSOCKET_H_

#ifdef WIN32
#include <winsock.h>
typedef int             socklen_t;
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <arpa/inet.h>
typedef int             SOCKET;
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#endif

class Socket {
public:
    Socket(SOCKET sock = INVALID_SOCKET);
    ~Socket();
    bool Create(int af,int type,int protocol = 0);
    bool Connect(const char* ip,unsigned short port);
    bool Bind(unsigned short port);
    bool Listen(int backlog = 5);
    bool Accept(Socket& s,char* fromip = NULL);
    int Send(const char* buf,int len,int flags = 0);
    int Recv(char* buf,int flags = 0);
    int Close();
    int GetError();
    static int Init();
    static int Clean();
    Socket& operator = (SOCKET s);
    operator SOCKET ();
protected:
    SOCKET m_sock;
};
#endif

Socket.cpp

#include <stdio.h>
#include "Socket.h"

#ifdef WIN32
#pragma comment(lib,"wsock32")
#endif

Socket::Socket(SOCKET sock)
{
    m_sock = sock;
}

Socket::~Socket()
{
}

int Socket::Init()
{
#ifdef WIN32
    WSADATA wsaData;
    WORD version = MAKEWORD(2,0);
    int ret = WSAStartup(version,&wsaData);
    if (ret) {
        return -1;
    }
#endif
    return 0;
}

int Socket::Clean()
{
#ifdef WIN32
    return (WSACleanup());
#endif
    return 0;
}

Socket& Socket::operator = (SOCKET s)
{
    m_sock = s;
    return (*this);
}

Socket::operator SOCKET ()
{
    return m_sock;
}


bool Socket::Create(int af,int protocol)
{
    m_sock = socket(af,type,protocol);
    if (m_sock == INVALID_SOCKET) {
        return false;
    }
    return true;
}

bool Socket::Connect(const char* ip,unsigned short port)
{
    struct sockaddr_in svraddr;
    svraddr.sin_family = AF_INET;
    svraddr.sin_addr.s_addr = inet_addr(ip);
    svraddr.sin_port = htons(port);
    int ret = connect(m_sock,(struct sockaddr*)&svraddr,sizeof(svraddr));
    if (ret == SOCKET_ERROR) {
        return false;
    }
    return true;
}

bool Socket::Bind(unsigned short port)
{
    struct sockaddr_in svraddr;
    svraddr.sin_family = AF_INET;
    svraddr.sin_addr.s_addr = INADDR_ANY;
    svraddr.sin_port = htons(port);
    int opt = 1;
    if (setsockopt(m_sock,SOL_SOCKET,SO_REUSEADDR,(char*)&opt,sizeof(opt)) < 0)
        return false;
    int ret = bind(m_sock,sizeof(svraddr));
    if (ret == SOCKET_ERROR) {
        return false;
    }
    return true;
}

bool Socket::Listen(int backlog)
{
    int ret = listen(m_sock,backlog);
    if (ret == SOCKET_ERROR) {
        return false;
    }
    return true;
}

bool Socket::Accept(Socket& s,char* fromip)
{
    struct sockaddr_in cliaddr;
    socklen_t addrlen = sizeof(cliaddr);
    SOCKET sock = accept(m_sock,(struct sockaddr*)&cliaddr,&addrlen);
    if (sock == SOCKET_ERROR) {
        return false;
    }
    s = sock;
    if (fromip != NULL)
        sprintf(fromip,"%s",inet_ntoa(cliaddr.sin_addr));
    return true;
}

int Socket::Send(const char* buf,int flags)
{
    int bytes;
    int count = 0;
    while (count < len) {
        bytes = send(m_sock,buf + count,len - count,flags);
        if (bytes == -1 || bytes == 0)
            return -1;
        count += bytes;
    }
    return count;
}

int Socket::Recv(char* buf,int flags)
{
    return (recv(m_sock,buf,len,flags));
}

int Socket::Close()
{
#ifdef WIN32
    return (closesocket(m_sock));
#else
    return (close(m_sock));
#endif
}

int Socket::GetError()
{
#ifdef WIN32
    return (WSAGetLastError());
#else
    return (-1);
#endif
}
原文链接:https://www.f2er.com/cocos2dx/339880.html

猜你在找的Cocos2d-x相关文章