C ++ development of P2P timely message sending program (1)
Xu long 2007-12-11
I have been resigning for two months and have not found a suitable job .....
I have interviewed several companies recently and I don't think it is very suitable. I have neither the learning value nor the recognized salary, let alone it can help me build a job opportunities.
Idle is also idle, just a few years ago I wanted to build a timely communication tool similar to QQ, that is, I don't have time. Isn't it just the day-and-earth Lijia and now?
It's not a waste of opportunities from heaven. Let's get started.
Select the development language C ++ first. Why? Because I used to study it in books, so little practice.
Let's first customize the transmission protocol today:
// Data packet type
Typedef Enum packet_type //: Char
{
Invalid = 0,
Empty = 4,
// Connect to the server
P2s_syn_conn_req = 8,
S2p_ack_conn_req = 12,
P2s_fin_conn_req = 16,
// Obtain the initial connection information from the server
P2s_syn_inidata_req = 20,
S2p_ack_inidata_req = 24,
S2p_fin_inidata_req = 28,
// Client Login message
P2s_syn_login_req = 32, // the client sends a login request to the server
S2p_ack_login_req = 36, // The server receives the Client Login Request and replies with the login information.
P2s_fin_login_req = 40, // after the client receives s2p_ack_login_req, it replies to the server
// The Server client keeps connected messages
S2p_syn_keeplive = 44, // the server sends messages to maintain the connection.
P2s_ack_keeplive = 48, // The client replies to keep the connection
// Exchange data information
Ps_syn_data_req = 52, // PS sends data requests (including data size, package size, data integrity ID, and serial number)
Ps_ack_data_req = 56, // PS answers the request to receive data and sets the packet size (Data Integrity ID, and serial number)
Ps_fin_data_req = 60, // PS finally determines the Agreement (Data Integrity ID, and serial number) reached by the transmission request)
Ps_syn_data = 64, // PS sends data (data integrity ID, and serial number)
Ps_ack_data = 68, // PS answers the received data (data integrity ID, and serial number)
Ps_fin_data = 72, // PS data has been sent (Data Integrity ID, and serial number)
// The client applies for P2P messages with another client
P2s_syn_hole_req = 76, // "Submit Connection Client" register with server "to establish connection with another client" Application
S2p_syn_hole_req = 80, // the server sends a packet to the passive end, asking it to be ready to connect to others.
P2s_ack_hole_req = 84, // The passive side answers whether the server is ready.
S2p_ack_hole_req = 88, // The server answers the request received by the client (The response includes the connection information of the invited client)
P2s_fin_hole_req = 92, // receives the server's response to the application.
// Exchange data between clients
Pp_syn_data_req = 96, // pp sends a Data Request (including the data size, data integrity ID, and serial number)
Pp_ack_data = 100, // pp answers the received data (data integrity ID, and serial number)
Pp_fin_data = 104, // pp data has been sent (Data Integrity ID, and serial number)
// The client and the client keep connected messages
P2p_syn_keeplive = 108 ,//
P2p_ack_keeplive = 112 ,//
} Packet_type;
Typedef struct empty_pkt
{
Empty_pkt (): epackettype (empty), dwpacketsize (0), dwseqnumber (0), dwacknumber (0)
{}
Packet_type epackettype; // package type
DWORD dwpacketsize; // Package Size
DWORD dwseqnumber; // Package number
DWORD dwacknumber; // package validation serial number
} Packet_header;
Typedef struct p2s_syn_conn_req_pkt: packet_header
{
P2s_syn_conn_req_pkt ()
{
Epackettype = p2s_syn_login_req;
}
} P2s_syn_conn_req_pkt;
# Endif
When this problem occurs, the size of the elements defined by C ++ enumeration is four bytes by default. I want it to take only one byte for a while.
The test can be defined as follows:
Typedef Enum Etest: Char
{
E1;
E2;
}
However, after I defined a struct with him as the type, it was tested that it was also allocated in four bytes during space allocation.
Test search, the original vc2005 it has a special setting for the struct, is the byte alignment problem. In a 32-bit System (w2k is used ),
It is aligned by four bytes. That is to say, when your struct size is less than a multiple of four, the compiler will automatically fill it up for you. In this case
It is set by default (it can be set to one-byte alignment). Is it easy? simply change the enumeration above to the default one...
(Unfinished ..... To be continued)