BSD socket simple package. Supports Android, iOS, Mac OSX

Source: Internet
Author: User
Tags htons

Cocos2d-x official does not encapsulate the native socket, only provides the websocket, if we need a socket, different teams have different wheel-making scheme, which uses ASIO library is more, but the ASIO library is too large, I do not want to use. In fact, just need to simply encapsulate the BSD socket is good, dozens of lines of code just.

Note If you test in Android, you need to add network access, and you cannot use it in the main thread.

Paste the code, just a simple test under, if there is a problem to improve slowly.

1 #ifndef __cpp_test__socket__2 #define__cpp_test__socket__3 4#include <sys/socket.h>5#include <netinet/inch.h>6 7typedef uint8_tbyte;8typedef unsigned Short ushort;9 Ten classSocket { One  Public: A Socket (); -Socket (intsockfd); -~Socket (); the      -     BOOLConnectConst Char* IP,ushortport); -     BOOLListenushortPortintsize); -socket*accept (); +      -ssize_t Send (byte*buffer, size_t size); +ssize_t recv (byte*buffer, size_t size); A      at Private: -     BOOLBindushortport); -      -     int_SOCKFD; -     structsockaddr_in _addr_local; - }; in  - #endif/* Defined (__cpp_test__socket__) */
Socket.h
1#include"Socket.h"2#include <netdb.h>3#include <unistd.h>4#include <arpa/inet.h>5#include <stdlib.h>6 7 Socket::socket () {8 }9 TenSocket::socket (intsockfd) { One_SOCKFD =sockfd; A } -  -socket::~Socket () { the Close (_SOCKFD); - } -  -  + BOOLSocket::bind (ushortPort) { -_addr_local.sin_family =af_inet; +_ADDR_LOCAL.SIN_ADDR.S_ADDR =Inaddr_any; A_addr_local.sin_port =htons (port); at     return:: Bind (_SOCKFD, (structsockaddr*) &_addr_local,sizeof(structSOCKADDR)) = =0; - } -  -  - BOOLSocket::listen (ushortPortintsize) { -_SOCKFD = socket (af_inet, Sock_stream,0); in     if(_SOCKFD <0) { -         return false; to     } +      -      This-bind (port); the     return:: Listen (_SOCKFD, size) = =0; * } $ Panax Notoginseng  -socket*socket::accept () { the     structsockaddr_in addr_remote; +socklen_t length =sizeof(addr_remote); A      the     intRemote =:: Accept (_SOCKFD, (structsockaddr*) &addr_remote, &length); +     if(Remote <0) { -         returnnullptr; $     } $     return NewSocket (remote); - } -  the  - BOOLSocket::connect (Const Char* IP,ushortPort) {Wuyi      This->bind (0); the      -     structsockaddr_in addr_remote; WuADDR_REMOTE.SIN_ADDR.S_ADDR =inet_addr (IP); -addr_remote.sin_family =af_inet; AboutAddr_remote.sin_port =htons (port); $      -     intSockfd_remote = socket (addr_remote.sin_family, Sock_stream,0); -     if(:: Connect (Sockfd_remote, (sockaddr*) &addr_remote,sizeof(Addr_remote)) <0) { - Close (sockfd_remote); A         return false; +     } the_SOCKFD =Sockfd_remote; -     return true; $ } the  the  thessize_t Socket::send (byte*buffer, size_t size) { the     return:: Send (_SOCKFD, buffer, size,0); - } in  the  thessize_t Socket::recv (byte*buffer, size_t size) { About     return:: recv (_SOCKFD, buffer, size,0); the}
Socket.cpp

BSD socket simple package. Supports Android, iOS, Mac OSX

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.