The design of the network module is a more important part of the large multiplayer online game. I put the network module to the front, because many developers face this piece of time is full of doubts, but also feel very mysterious and abstruse. The difficulties we face are in fact due to our lack of understanding and our unfamiliarity with this area.
The design of the reference to the Tianlong eight/martial arts World Network module design, the package adjustment, and Tianlong Eight actually also refer to the Korean classic online game design, so in the stability of this aspect or there is a certain accumulation.
In the previous framework, you can see an approximate interaction flow chart, the player login, create the role, delete the role, select the role and so on by the login server (login) responsible, and the player into the game directly with the game server (server) to interact with, The public data is handled by the Central server (world). The benefit of this allocation is to let each function, on the server is the mode of voltage divider, while the network module also reduces a lot of pressure.
In this design, the use of socket (socket) for TCP network interactive access, in the processing of many-to-one network access, will use multithreading. So here I will first introduce the next socket and multithreading.
1. What is a socket?
Multiple TCP connections or multiple application processes may require data to be transmitted over the same TCP protocol port. To differentiate between different application processes and connections, many computer operating systems provide an interface called a socket (socket) for applications interacting with the TCP/IP protocol.
A simple example, the following is linux/windows generic.
1) servers (server)
#include <stdio.h>#include <stdlib.h>#include <String.h>#include <errno.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/In.h>#define MAXLINE 4096int main (int argc,char**argv) {IntLISTENFD, CONNFD;structSockaddr_in servaddr;Char buff[4096];IntNif (LISTENFD = socket (af_inet, Sock_stream,0)) = =-1) {printf ("Create socket Error:%s (errno:%d) \ n", Strerror (errno), errno); Exit0); } printf ("LISTENFD:%d\n", LISTENFD); memset (&SERVADDR,0,sizeof(SERVADDR)); servaddr.sin_family =Af_inet; SERVADDR.SIN_ADDR.S_ADDR =Htonl (Inaddr_any); Servaddr.sin_port = htons (6666);if (Bind (LISTENFD, (struct sockaddr*) &servaddr,sizeof (SERVADDR)) = =-1) {printf ("Bind socket Error:%s (errno:%d) \ n", Strerror (errno), errno); Exit0); }if (Listen (LISTENFD,10) = =-1) {printf ("Listen socket Error:%s (errno:%d) \ n", Strerror (errno), errno); Exit0); } printf ("======waiting for client ' s request======\n");while (1) {if (CONNFD = Accept (LISTENFD, (struct sockaddr*) NULL) = =-1) {printf ( "accept Socket Error:%s (errno:%d) "continue0); Buff[n] = \0 "; printf ( "recv msg from client:%s\n ", Buff); Close (CONNFD); } close (LISTENFD),
2) clients (client)
#include <stdio.h>#include <stdlib.h>#include <String.h>#include <errno.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/In.h>#define MAXLINE 4096int main (int argc,char**argv) {IntSOCKFD, N;Char recvline[4096], sendline[4096];structSockaddr_in servaddr;if (argc! =2) {printf ("Usage:./client <ipaddress>\n"); Exit0); }if (SOCKFD = socket (af_inet, Sock_stream,0)) <0) {printf ("Create socket Error:%s (errno:%d) \ n", Strerror (errno), errno); Exit0); } printf ("SOCKFD:%d\n", SOCKFD); memset (&SERVADDR,0,sizeof(SERVADDR)); servaddr.sin_family =Af_inet; Servaddr.sin_port = htons (6666);if (Inet_pton (Af_inet, argv[1], &servaddr.sin_addr) <=0) {printf ("Inet_pton error for%s\n", argv[1]); Exit0); }if (Connect (SOCKFD), (struct sockaddr*) &servaddr,sizeof (SERVADDR)) <0) {printf ("Connect Error:%s (errno:%d) \ n", Strerror (errno), errno); Exit0 "send msg to server: \ n "); Fgets (Sendline, 4096if (Send (SOCKFD, Sendline, strlen (sendline), 0) < Span style= "color: #800080;" >0) {printf (send msg Error:%s (errno:%d) \n " Strerror (errno), errno); Exit (00
Server mode: Create (socket), bind (BIND), listen (listen), wait for connection (accept), receive data (RECV)/Send data (send)
Client mode: Create (socket)-to-connect (connect), receive data (RECV)/Send data (send)
2, what is multi-threaded? How is multithreading in C + + implemented?
In this regard I do not introduce more, this introductory introduction has been explained very clearly: http://blog.163.com/[email protected]/blog/static/799089922010814104312911/
MMORPG large game design and development (part1 of net)