The code is as follows:
the UpdateData ();
If (LocaIP IsBlank ())
{
AfxMessageBox (" set up the machine IP address, please!" );
return;
}
If (locaPort. IsEmpty ())
{
AfxMessageBox (please specify the communication port number! "" );
return;
}
//initialize the socket and binding
WSADATA WSADATA;
Int iErrorCode;
If (WSAStartup (MAKEWORD (2, 1), & amp; WsaData))//calls the Windows Sockets DLL
{
M_MessageList. ReplaceSel (" Winsock unable to initialize! \r\n");
WSACleanup ();
return;
}
M_MessageList. ReplaceSel (" began to create a Socket... \r\n");
SelfRcvSocket=socket (PF_INET, SOCK_DGRAM, 0);//create the native process of Socket, type SOCK_DGRAM, connectionless communication
If (SelfRcvSocket==INVALID_SOCKET)
{
M_MessageList. ReplaceSel (" failed to create a socket! \r\n");
return;
}
//get the native process of IP and port
BYTE nFild [4].
Cstrings sIP;
NFild LocaIP. GetAddress (nFild [0], [1], nFild [2], nFild [3]).
SIP. The Format (" % d % d, % d, % d ", nFild [0], nFild [1], nFild [2], nFild [3]).
M_sockSelfRcvAddr. Sin_family=AF_INET;
M_sockSelfRcvAddr. Sin_addr. S_un. S_addr=inet_addr (sIP);
M_sockSelfRcvAddr. Sin_port=htons (atoi (locaPort));
//to bind the socket and the native process
If (bind (SelfRcvSocket, (LPSOCKADDR) & amp; M_sockSelfRcvAddr, sizeof (m_sockSelfRcvAddr))==SOCKET_ERROR)
{
M_MessageList. ReplaceSel (" binding failed! \r\n");
return;
}
IErrorCode=WSAAsyncSelect (SelfRcvSocket m_hWnd, WM_CLIENT_READCLOSE, FD_READ);
If (iErrorCode==SOCKET_ERROR)
{
M_MessageList. ReplaceSel (" WSAAsyncSelect set failed! - used to connect the request message \ r \ n ");
return;
}
M_MessageList. ReplaceSel (" the native process start-up success! \r\n");
M_MessageList. ReplaceSel (" address "+ sIP + + locaPort" port ");
This - & gt; SetWindowTextA (" the native process (" + sIP + ":" + locaPort + ") is running... - ptopTalker ");
//interface
M_Start. EnableWindow (false);
M_Stop. EnableWindow (true);
LocaIP. EnableWindow (false);
M_LcaPrt. EnableWindow (false);
M_Login. EnableWindow (true);
SevrIP. EnableWindow (true);
M_SvrPrt. EnableWindow (true);
M_nicnam. EnableWindow (true);
M_MessageList. EnableWindow (true);
M_exit. EnableWindow (false);
M_nicnam. SetFocus ();
//
return;
//ADD
CDialog: : OnOK ();
}
Void CP2PTalkerDlg: : OnStop ()
{
//TODO: add the control notification handler code
//ADD
//when the program stops running, to empty SOCKET
M_MessageList. ReplaceSel (" \ r \ n are closing the Socket... \r\n");
Closesocket (SelfRcvSocket);
WSACleanup ();
Stop running m_MessageList. ReplaceSel (" the native process. \r\n");
This - & gt; PtopTalker SetWindowTextA (" ");
//interface
M_Start. EnableWindow (true);
M_Stop. EnableWindow (false);
LocaIP. EnableWindow (true);
M_LcaPrt. EnableWindow (true);
M_exit. EnableWindow (true);
M_Login. EnableWindow (false);
M_nicnam. EnableWindow (false);
SevrIP. EnableWindow (false);
M_SvrPrt. EnableWindow (false);
LocaIP. SetFocus ();
//
//ADD
}
Void CP2PTalkerDlg: : OnLogin ()
{
//TODO: add the control notification handler code
//ADD
//input validation
The UpdateData ();
If (nickname. IsEmpty ())
{
AfxMessageBox (" give themselves a nickname!" );
return;
}
If (SevrIP IsBlank ())
{
AfxMessageBox (" please fill out the chosen for P2P server host IP address!" );
return;
}
If (sevrPort. IsEmpty ())
{
AfxMessageBox (" please specify port of P2P service process!" );
return;
}
BYTE nFild [4].
Cstrings sIP;
//generated login registration message
NFild LocaIP. GetAddress (nFild [0], [1], nFild [2], nFild [3]).
SIP. The Format (" % d % d, % d, % d ", nFild [0], nFild [1], nFild [2], nFild [3]).
RegisterAddr="RGST_ADDR;";
RegisterAddr +=nickname + ", "+ sIP;
RegisterAddr +=":" + locaPort;
//get the P2P service process IP and port
NFild SevrIP. GetAddress (nFild [0], [1], nFild [2], nFild [3]).
SIP. The Format (" % d % d, % d, % d ", nFild [0], nFild [1], nFild [2], nFild [3]).
//send a message
This - & gt; StartSndSocket (RegisterAddr, sIP, sevrPort);
This - & gt; SetWindowTextA (" logged-in P2P server (" + sIP + ":" + sevrPort + ") - ptopTalker ");
//interface
M_Stop. EnableWindow (false);
M_Login. EnableWindow (false);
M_Logout. EnableWindow (true);
SevrIP. EnableWindow (false);
M_SvrPrt. EnableWindow (false);
M_nicnam. EnableWindow (false);
M_UserList. EnableWindow (true);
M_EditWords. EnableWindow (true);
M_Send. EnableWindow (true);
M_EditWords. SetFocus ();
//
//ADD
}
//ADD
LRESULT CP2PTalkerDlg: : OnReadMsg (WPARAM WPARAM, LPARAM LPARAM)
{
//receiving messages from the other end system
Cstrings STR, strHead, strData;
The switch (WSAGETSELECTEVENT (lParam))
{
Case FD_READ:
Socklen=sizeof (m_sockSelfRcvAddr);
Recvfrom (SelfRcvSocket, (char *) & amp; Msgbuf, sizeof (msgbuf), 0, (LPSOCKADDR) & amp; M_sockSelfRcvAddr, (int *) & amp; Socklen);
WSAAsyncSelect (SelfRcvSocket m_hWnd, WM_CLIENT_READCLOSE FD_READ);
STR. The Format (" % s ", msgbuf MSG);
//intercept packet header and data section
Int n=STR. ReverseFind ('; ');
StrHead=STR. Left (n);
StrData=https://bbs.csdn.net/topics/str.Right (STR) GetLength () - (n + 1));
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull