Simple use of Socket in Cocos2d-x

Source: Internet
Author: User

Simple use of Socket in Cocos2d-x

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: defines the entry point of the console application. // # Include "stdafx. h" # include
 
  
# Include
  
   
# Include "windows. h "# pragma comment (lib," ws2_32.lib ") int _ tmain (int argc, _ TCHAR * argv []) {// network programming WORD wVersionRequested; WSADATA wsaData in Windows; // It is used to accept the structure information of Windows Socket int err; wVersionRequested = MAKEWORD (1.1); // requests the windows database err = WSAStartup (wVersionRequested, & wsaData) of version ); if (err! = 0) {return-1; // if the returned value is 0, WSAStartup is successfully applied.} if (LOBYTE (wsaData. wVersion )! = 1 | HIBYTE (wsaData. wVersion )! = 1) {// check whether the low byte is 1 high byte or not, and whether it is our request version 1.1 WSACleanup (); // otherwise, use this function to clear information, end return-1 ;} /*************************************** * ********************************* or above is required by windows added content, you don't need to add a copy under the Mac. Just copy it. It is the same every time, the following are the codes to be written on the server *//***************************** **************************************** * ** // 1. create Socket // type, stream Socket, protocol int socket_fd = socket (AF_INET, SOCK_STREAM, PF_UNSPEC); if (socket_fd =-1 ){ // If the socket returns-1, it will be returned to prevent collapse .... Printf ("Server Socket Err"); return-1;} // 2. bind sockaddr_in serverAddress; serverAddress. sin_family = AF_INET; // In general, AF_INET (address family) PF_INET (protocol family) 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) must be added )); if (bind_fd =-1) {printf ("bind fiald"); return-1;} // 3. listen listener 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,); if (recv_data =-1) {printf ("receive falid \ n"); continue;} else if (recv_data = 0) {closesocket (socket_fd);} else {// 6. send data printf ("% s \ n", buf); // print the data sent from the client char sentData [100]; std: cin. getline (sentData, 100, '\ n'); // obtain a row of data, ending with' \ n' int sendLenght = send (client_fd, sentData, 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: defines the entry point of the console application. // # Include "stdafx. h" # include
 
  
# Include
  
   
# Include "windows. h "# pragma comment (lib," ws2_32.lib ") int _ tmain (int argc, _ TCHAR * argv []) {WORD wVersionRequested; WSADATA wsaData; // It is used to accept the structure information of Windows Socket int err; wVersionRequested = MAKEWORD (1.1); // requests the windows database err = WSAStartup (wVersionRequested, & wsaData) of version ); if (err! = 0) {return-1; // if the returned value is 0, WSAStartup is successfully applied.} if (LOBYTE (wsaData. wVersion )! = 1 | HIBYTE (wsaData. wVersion )! = 1) {// check whether the low byte is 1 high byte or not, and whether it is our request version 1.1 WSACleanup (); // otherwise, use this function to clear information, end 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; // The server address 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,); if (receLen =-1) {printf ("received faild"); continue ;} printf ("% s \ n", receData); // output from the server} // 5. closeclosesocket (socket_fd); getchar (); return 0 ;}
  
 

Run Interface

Start the Server first and start the client


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.