Network interconnection After understanding the basic form of online games, let us enter the real actual application part. First of all, as a online game, in addition to the things necessary for the conventional stand-alone game, we also need to add a network communication module. Of course, this is the main part of the online game, let's discuss how to implement the network communication module. A perfect network communication module involves fairly wide, this article is only discussed for more basic processing methods. Online games are composed of clients and servers, and there are two different network communication processing methods, but we have the same place, we will introduce their common point. We use Microsoft Windows 2000 [2000 Server] as a development platform, and use Winsock as a network interface (probably some friends will consider using DirectPlay to make network communication, but for current online games, DirectPlay is not suitable, specific reasons are not available here Discussed).
After determining the platform and the interface, we start some of the necessary initialization work before the network connection creation, whether it is a client or server. Let's take a look at the code snippet below:
Word WVersionRequested; WSADATAWSADATA; WVERSIONREQUESTED MAKEWORD (1, 1); IF (WsaStartup (WinsionRequested, & WSADATA)! 0) {Failed (Winsock Version Error! ");}
By calling Windows Socket API functions, the network device is initialized, and then the creation of network sockets, the code snippet is as follows:
Socket SSocket Socket (AF_INET, M_LPROTOCOL, 0); if (SSOCKET == INVALID_SOCKET) {Failed ("Winsocket Create Error!");}
Here, the number of Socket connections required for the client and the server is different. The client only needs a socket connection to meet the needs of the game, and the server must create a Socket connection for communication for each player user. Of course, it is not to say that if there is no player on the server, it does not need to create a socket connection. The server side generates a special socket when starting, and the player can create a request for the server to respond, and wait for the network listening part. There is more detailed description.
Initialization and creation inevitably release and delete, let's take a look at the release below:
IF (SSOCKET! = INVALID_SOCKET) {CloseSocket (SSOCKET);} if (wsacleanup ()! = 0) {Warning ("can't release winsocket";
The two steps here are all released accordingly for the initialization of the creation of the previous. Next, look at a network execution process of the server side. Here we assume that the server has created a socket for use, what we have to do is to let this socket becomes a dedicated interface that listens to the network connection request, see the following code snippet:
SockAddr_in addr; Memset (& addr, 0, sizeof (addr); addr.sin_family = AF_ITDR = HTON_ADDR.S_ADDR = HTONL (INADDR_ADDR_Addr); // port); // port is the port number to listen // Bind Socketif (Bind (SSOCKET, SOCKADDR *) & addr, sizeof (addr) == SOCKET_ERROR) {failed ("Winsocket Bind Error!");} // Listening IF (Listen (SSOCKET, SOMAXCONN) = = Socket_ERROR) {FAILED ("Winsocket Listen Error!");} This is the blocking communication process. At this time, the program will be in the state of waiting for the player user connection, if there is a client connection in this time, by accept () Create a socket connection for this player user, the code snippet is as follows:
SockAddraddrserver; int Nlen sizeof (addrserver); socket splayersocket accept (socket, & addrserver, & nlen); if (splayersocket == invalid_socket) {Failed (Winsocket Accept Error! ");}
Here we create a splayersocket connection. After that, the game server and the player user have passed this socket. So far, our server already has the function of accepting player users, let us see how the game client is connected. To the game server, the code snippet is as follows:
SockAddr_in Addr; Memset (& addr, 0, sizeof (addr); addr.sin_family = AF_INET; // To connect the game server port number addr.sin_addr.s_addr = inet_addr (ip); // To connect the game server IP address Addr.sin_port = HTONS (port); // To this, the client and server already have a bridge of communication, // Next is the transmission and reception of data: Connect (SSOCKET, (SockAddr *) & addr, sizeof AddR); IF (SSOCKET, PBUFFER, LLENGTH, 0) == SOCKET_ERROR) {Failed ("Winsocket Send Error!");
The PBUFFER here is the data buffer pointer to be sent, and LLEngth is the length of the data that needs to be sent. Through this Socket API function, we can send data from the client or server, and we can pass RECV () This Socket API function is received:
IF (RECV (SSOCKET, PBUFFER, LLENGTH, 0) == Socket_ERROR) {Failed ("Winsocket Recv Error!");