Fly Pigeon Biography Source analysis three-network

Source: Internet
Author: User
Tags htons

Reprint Please specify source:http://blog.csdn.net/mxway/article/details/44195099

This article is on the Fly pigeon 2.06 source code based on the analysis.

First, the initialization of the network

The following code is in the constructor of the class Tmainwin for the main window of the Flying Pigeon transfer book.

CFG = new cfg (nicaddr, PortNo = _portno), if ((msgmng = new Msgmng (nicaddr, PortNo, CFG))->getstatus () = = FALSE) {:: EXITP Rocess (0xFFFFFFFF); return;}

Call Wsockinit in the constructor of the Msgmng class.

MSGMNG::MSGMNG (ULONG nicaddr, int portno, CFG *_cfg) {        ... local.addr = Nicaddr;local.portno = Htons (PortNo);        ... if (Wsockinit (cfg? True:false) = = FALSE) return;        ...}

BOOL Msgmng::wsockinit (bool RECV_FLG) {wsadatawsadata;if (:: WSAStartup (0x0101, &wsadata)! = 0) Returngetsockerrormsg ("Wsastart ()"), false;if ((UDP_SD =:: Socket (af_inet, SOCK_DGRAM, 0)) = = Invalid_socket) Returngetsockerrormsg ("Please setup TCP/IP (controlpanel->network) \ r \ n"), false;if (RECV_FLG! = TRUE) returntrue;if ((TCP_SD =:: Socket (af_inet, sock_stream, 0)) = = Invalid_socket) returngetsockerrormsg ("Please setup2 TCP/IP ( controlpanel->network) \ r \ n "), False;struct sockaddr_inaddr;memset (&addr, 0, sizeof (addr)); addr.sin_family= Af_inet;addr.sin_addr.s_addr= local.addr;addr.sin_port= local.portno;if (:: Bind (UDP_SD, (LPSOCKADDR) &addr, sizeof (addr))! = 0) returngetsockerrormsg ("Bind ()"), False;if (:: Bind (TCP_SD, (lpsockaddr) &addr, sizeof (addr))! = 0 ) {:: Closesocket (TCP_SD); tcp_sd = Invalid_socket; Getsockerrormsg ("Bind (TCP) error. Can ' t support file Attach ");} BOOLFLG = true;//Non blockif (:: Ioctlsocket (UDP_SD, Fionbio, (unsigned long *) &FLG)! = 0) returngetsockerrormsg("Ioctlsocket (Nonblock)"), False;if (Isavailabletcp () &&:: Ioctlsocket (TCP_SD, Fionbio, (unsigned long *) & FLG)! = 0) returngetsockerrormsg ("Ioctlsocket TCP (Nonblock)"), FALSE;FLG = true;//Allow Broadcastif (:: setsockopt (UDP_ SD, Sol_socket, So_broadcast, (char *) &FLG, sizeof (FLG))! = 0) returngetsockerrormsg ("setsockopt (broadcast)"), False;intbuf_size = max_sockbuf, buf_minsize = max_sockbuf/2;//UDP if (:: setsockopt (UDP_SD, Sol_socket, So_sndbuf, (cha R *) &buf_size, sizeof (int))! = 0&&::setsockopt (UDP_SD, Sol_socket, SO_SNDBUF, (char *) &buf_minsize, sizeof (int))! = 0) getsockerrormsg ("setsockopt (SENDBUF)"); buf_size = max_sockbuf, buf_minsize = max_sockbuf/2;if (:: SetSockOpt (UDP_SD, Sol_socket, So_rcvbuf, (char *) &buf_size, sizeof (int))! = 0&&::setsockopt (UDP_SD, Sol_ SOCKET, So_rcvbuf, (char *) &buf_minsize, sizeof (int))! = 0) getsockerrormsg ("setsockopt (RECVBUF)"); FLG = true;// Reuse Addrif (Isavailabletcp () &&:: setsockopt (TCP_SD, Sol_socket, SO_REUSEADDR, (char *) &FLG, sizeof (FLG))! = 0) getsockerrormsg ("SetSockOpt TCP (REUSEADDR)"); if ( Isavailabletcp () &&:: Listen (TCP_SD, 5)! = 0) returnfalse;returntrue;

Initialize the DLL to be used for network programming in Wsockinit, set the TCP service and UDP port number, and set UDP and TCP socket as non-blocking mode. The ports that the TCP and UDP services occupy are the constructors that are passed to the msgmng by the Tmainwin constructor. The Tmainwin constructor is passed to the MSGMNG's end number from the Tmainwin constructor. Back to Ipmsg.cpp's Tmsgapp::initwindow function (refer to the previous Fly Pigeon biography Source analysis article)

void Tmsgapp::initwindow (void) {        ... intport_no = atoi (cmdline); if (port_no = = 0) port_no = ipmsg_default_port;        ...        Mainwnd = new Tmainwin (nicaddr, port_no); Mainwnd->create (Class_name, ip_msg, Ws_overlappedwindow | (Isnewshell ()? ws_minimize:0));..}

In the Initwindow function, you can see the initialization of the port used, CmdLine is the port parameter that runs the Fly Pigeon book in the command line mode, in order to simplify the problem regardless of the command run. Port_no initialized to Ipmsg_default_port,ipmsg_default_port is a macro definition

#define Ipmsg_default_port 0x0979

Flying Pigeon Biography The default service port is hexadecimal 979, which is the decimal 2425.

Second, network non-blocking mode

There are several ways to implement a socket under Windows, and the Fly Pigeon Book uses a message to send a network event to the window handle, while the window that handles the network event is Tmainwin. End of Initwindow call in IPmsg.cpp mainwnd->create (class_name, ip_msg, Ws_overlappedwindow | (Isnewshell ()? ws_minimize:0)), followed by the execution of the Tmainwin evcreate (the invocation process is described in detail in the second article of the message mechanism)

BOOL tmainwin::evcreate (LPARAM LPARAM) {        ...        Msgmng->asyncselectregist (hWnd);        ...        if (Msgmng->getstatus ())        entryhost ();} BOOL Msgmng::asyncselectregist (HWND hwnd) {if (Hasyncwnd = = 0) Hasyncwnd = hwnd;if (:: WSAAsyncSelect (UDP_SD, HWND, wm_ Udpevent, fd_read) = = Socket_error) returnfalse;if (:: WSAAsyncSelect (TCP_SD, HWnd, Wm_tcpevent, fd_accept| fd_close) = = Socket_error) returnfalse;returntrue;}
WSAAsyncSelect (Udp_sd,hwnd, Wm_udpevent,fd_read), when sending data to UDP_SD, is referred to the TMAINWIN message processing mechanism. Since wm_udpevent is the message mechanism of the Flying Pigeon Biography program, it is handled by Tmainwin's Eventuser (refer to the message mechanism of the second Flying Pigeon biography).

BOOL Tmainwin::eventuser (UINT umsg, WPARAM WPARAM, LPARAM LPARAM) {        ... case wm_udpevent:udpevent (LPARAM); Returntrue;        ...}
Third, the user on-line notification

Flying Pigeon is a work is a local area network program, when a new device using the Flying Pigeon Transfer program, will automatically notify other users who have been online, will display themselves to other users of the list.

After the Msgmng->asyncselectregist (HWND) is called, and the network event is hooked up with the window handle, the program broadcasts the local area network and tells others to open the Fly Pigeon biography program.

void Tmainwin::entryhost (void) {        ... Broadcastentry (ipmsg_br_entry);        ...}
void Tmainwin::broadcastentry (ULONG mode) {msgmng->send (ULONG) ~0, htons (PortNo), Host_status, Getnicknameex (), CFG->GROUPNAMESTR);}
Msgmng->send (ULONG) ~0, htons (PortNo), Host_status, Getnicknameex (), CFG->GROUPNAMESTR), send broadcast messages to the LAN and tell others to go online. Other programs that are running receive this message and store it for later use, and the process is no longer detailed. Introduction to the following article about the format of sending messages to the Flying Pigeon biography.




Fly Pigeon Biography Source analysis three-network

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.