Simple use of socket in Cocos2d-x

Source: Internet
Author: User
Tags htons

Cocos2d-x socket simple socket

Summary of How to Use SOCKET today, in Baidu .. The link found above (there is a link to Baidu, which is not fully written. You can check the link ,)... If any big God has a new demo, please share it !!!

In the Cocos2d-x about the network request: Get post put Delete (the latter two I rarely used in development), there is socket (for timely communication, network game teammates call ).


Create a project under vs (remember that it is not a Cocos project !!!) C ++ file. As a server


Baidu above the relevant interpretation: http://baike.baidu.com/view/2355183.htm? Fr = Aladdin


Serverdemo. cpp:

// WebSocketDemo.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <WinSock2.h>#include <iostream>#include "windows.h"#pragma comment(lib,"ws2_32.lib")int _tmain(int argc, _TCHAR* argv[]){//Windows下面的网络编程WORD wVersionRequested;WSADATA wsaData;//用于接受Windows Socket 的结构信息int err;wVersionRequested = MAKEWORD(1,1);  //请求1.1版本的windows 库err = WSAStartup(wVersionRequested,&wsaData);if (err !=0){return -1;//返回值为 0 表示 成功申请 WSAStartup}if (LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!=1){//检测低字节是不是 1  高字节是不是 1 ,确定是不是我们请求的 1.1版本WSACleanup();// 否则用这个函数清除信息,结束return -1;}/************************************************************************//* 以上是windows需要添加的内容,Mac下面不用添加,直接抄写就行,每次都一样,以下才是服务器端需要写的代码                                                                     *//************************************************************************/// 1. 创建Socket   //类型 、流式Socket 、协议int socket_fd = socket(AF_INET,SOCK_STREAM,PF_UNSPEC);if (socket_fd == -1){//如果socket返回 -1就 返回,防止崩。。。。printf("Server Socket Err");return -1;}//  2. bind 绑定sockaddr_in serverAddress;serverAddress.sin_family = AF_INET;  //一般来说AF_INET(地址族)PF_INET(协议族)serverAddress.sin_addr.S_un.S_addr = htonl(ADDR_ANY);serverAddress.sin_port = htons(7777);//必须 加上 htonsint bind_fd = bind(socket_fd,(sockaddr*)(&serverAddress),sizeof(serverAddress));if (bind_fd == -1){printf("bind fiald");return -1;}// 3 .Listen 监听if (listen(socket_fd,2) == -1){printf("listen faild");return -1;}// 4 .acceptint client_fd;sockaddr_in clientAddress;int s = sizeof(clientAddress);client_fd = accept(socket_fd,(sockaddr *)&clientAddress,&s);if (client_fd == -1){printf("accept falid");return -1;}//  5 . receivewhile (true){char buf[100];int recv_data = recv(client_fd,buf,100,0);if (recv_data == -1){printf("receive falid\n");continue;}else if(recv_data == 0){closesocket(socket_fd);}else{// 6 .send 发送数据printf("%s\n",buf);     //打印客户端发来的数据char sentData[100];std::cin.getline(sentData,100,'\n');//获取一行数据,以‘\n’结束int sendLenght = send(client_fd,sentData,100,0);if (sendLenght == -1){printf("send falid");continue;}}}// 7 . closeclosesocket(client_fd);return 0;}

Note the header file introduced at the beginning, notoneless !!!


The following is the client:

Clientdemo. cpp

// ClientSocketDemo.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include<winsock.h>#include <iostream>#include "windows.h"#pragma comment(lib,"ws2_32.lib")int _tmain(int argc, _TCHAR* argv[]){WORD wVersionRequested;WSADATA wsaData;//用于接受Windows Socket 的结构信息int err;wVersionRequested = MAKEWORD(1,1);  //请求1.1版本的windows 库err = WSAStartup(wVersionRequested,&wsaData);if (err !=0){return -1;//返回值为 0 表示 成功申请 WSAStartup}if (LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!=1){//检测低字节是不是 1  高字节是不是 1 ,确定是不是我们请求的 1.1版本WSACleanup();// 否则用这个函数清除信息,结束return -1;}// 1 . socket,int socket_fd = socket(AF_INET,SOCK_STREAM,PF_UNSPEC);if (-1 == socket_fd){printf("client socket falid");return -1;}// 2 . contectsockaddr_in setverAddress;   //服务器的地址setverAddress.sin_port = htons(2223); //iPsetverAddress.sin_family = AF_INET;setverAddress.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");int connect_fd = connect(socket_fd,(sockaddr *)&setverAddress,sizeof(setverAddress));if (connect_fd == -1){printf("contect falid ");return -1;}// 3 sendwhile (true){char sendData[100];std::cin.getline(sendData,100,'\n');if (send(socket_fd,sendData,100,0) == -1){printf("send faild");continue;}// 4 . receive char receData[100];int receLen = recv(socket_fd,receData,100,0);if (receLen == -1){printf("received faild ");continue;}printf("%s\n",receData);// 输出服务器传过来的}// 5. closeclosesocket(socket_fd);getchar();return 0;}

Run Interface

Start the server first and start the client


Simple use of socket in Cocos2d-x

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.