2D online game development (network) (four)

xiaoxiao2021-04-05  260

2D online game development (network) (four)

Author: akinggw

In the previous one, we just explain how to build a server or client. In this article, we will explain how the client is connected to the server.

#include "stdio.h" // printf and gets

#include "string.h" // strcpy

#include "rakclientInterface.h"

#include "raknetworkfactory.h"

#include "rakserverinterface.h"

#include "packetenumerations.h"

This is the header file included in our program, the most important thing is to have a packetenumerates.h. What is this header?

Open its files can be seen is some macro variables to process information obtained during the running process. I am here to translate, in the future application, we will make detailed explanation.

First, in the declaration of the variable, there are more statements:

Packet * packet;

Packet is a data structure for storing data in network transfers, which are as follows:

Struct packet

{

Playerid Playerid;

Unsigned long length;

Unsigned long bitsize;

CHAR * DATA;

}

Playerid indicates the source of the package. Each connection server client will be assigned a unique ID number to identify yourself.

Length and Bitsize tell you the data length and bits in this structure.

* DATA is the data in this package.

Then we build clients or servers, code, and front.

After the client or server is established, we determine whether the client is still the server:

IF (RakserverInterface)

{

// Server runs at 600,000 ports

RakserverInterface-> Start (32, 0, 0, 60000);

}

Else

{

// Run the client

Printf ("Enter Server IP Address: N");

Gets (STR);

// 127.0.0.1 Designates the feedback loop so we can test on one computer

IF (Str [0] == 0)

STRCPY (STR, "127.0.0.1");

RakClientInterface-> Connect (STR, 60000, 0, 0, 0);

}

In RakserverInterface-> Start (32, 0, 0, 60000); in this statement, the first parameter indicates how many clients are allowed to connect at the same time, here, we set 32. A 32 clients can be connected at the same time. This parameter can be set to 65535; the second parameter is used to reserve, set to 0; the third parameter is used to set the server update, the parameter is greater than or equal to 0, indicates that the number of milliseconds is updated every time the current set. It is set here: the last parameter is used to set the port of the server (noted that the client's port should be like the port of the server), in addition, the port number set should be above 32000, because at 32000 The lower ports are retained, used in other services such as WWW, FTP, POP3, etc.

Let's take a look at RakClientInterface-> Connect (STR, 60000, 0, 0, 0), which is used for client connection servers. The first parameter indicates the IP address of the server you want to connect. If it is in your own computer debugger, enter "127.0.0.1" or "localhost" directly; the second parameter represents the port of the server to connect; The parameter represents the client port to connect, mainly to exchange data between the client; the fourth parameter is not; the fifth parameter is the same in the third parameter in the server START function. Then, we are in the loop Data processing:

While (1)

{

IF (RakserverInterface)

Packet = rakserverinterface-> receive ();

Else

Packet = rakclientInterface-> receive ();

IF (Packet)

{

Switch (packet-> data [0])

{

Case id_remote_disconnection_notification:

Printf ("Another connection has been disconnected .n");

Break;

Case ID_remote_connection_lost:

Printf ("A client lost connection .n");

Break;

Case id_remote_new_incoming_connection:

Printf ("A client is online .n");

Break;

Case ID_connection_request_accepted:

Printf ("Our connection requires .n");

Break;

Case id_new_incoming_connection:

Printf ("has a new connection .n");

Break;

Case ID_no_free_incoming_connections:

Printf ("Server is full .n");

Break;

Case id_disconnection_notification:

IF (RakserverInterface)

Printf ("Client Loss .N");

Else

Printf ("Connection Interrupt .N");

Break;

Case id_connection_lost:

IF (RakserverInterface)

Printf ("Client Lost Connection .n");

Else

Printf ("Connection is lost .n");

Break;

DEFAULT:

Printf ("ID information% i is reached .n", packet-> data [0]);

Break;

}

IF (RakserverInterface)

Rak ServerInterface-> deallocatepacket (packet);

Else

RakClientInterface-> deallocatepacket (packet);

}

}

Here, we explain in detail their role.

IF (RakserverInterface)

Packet = rakserverinterface-> receive ();

Else

Packet = rakclientInterface-> receive ();

Accept data from the server or client, save it in the packet.

Next processes.

Because the network engine returns some information during the run, this information is to the client, and some gives the server, and some are two.

The first DATA [0] returned in the packet indicates these types, which interprets these types in packetenumerates.h.

Due to the relationship between the space, I will explain here, everyone is still going to see it. IF (RakserverInterface)

Rak ServerInterface-> deallocatepacket (packet);

Else

RakClientInterface-> deallocatepacket (packet);

It means that the package is received, so that it takes effect.

Finally, like a previous article, release the memory we occupy.

IF (RakClientInterface)

RaknetworkFactory :: destroyrakclientinterface (rakclientinterface);

Else IF (Rak ServerInterface)

RaknetWorkfactory :: DestroyrakServerInterface (RakserverInterface);

Return 0;

}

Server screenshot:

Figure 1

Client screenshot:

Figure 2

For more information, please visit the Golden Bridge Science website (http://popul.jqcq.com) Game development section, such as you need a book development, please refer to Jinqiao Book City Game Channel (http://book.jqcq.com/category / 1_70_740.html). If you have any good suggestions when you read this article, please let me, my e_mail: akinggw@126.com. If you have any questions when using SDL, please go to the Jinqiao Scot website (http: // popul. JQCQ.com) Game Development section, I will answer your questions in detail.

转载请注明原文地址:https://www.9cbs.com/read-132138.html

New Post(0)