Home > Software engineering >  Watching a SOCKET programming found when the server will also call the following code after closed,
Watching a SOCKET programming found when the server will also call the following code after closed,

Time:11-03

 void CChatDlg: : ServerClose ()//server close call 
{
if (! M_ServerOnline)
{
return;
}
CSocket sock.
SOCKADDR_IN SockAddr.
M_ServerOnline=FALSE;
SockAddr. Sin_family=AF_INET;
SockAddr. Sin_addr. S_un. S_addr=inet_addr (" 127.0.0.1 ");
SockAddr. Sin_port=htons (m_ListenPort);
The sock. The Create ();
The sock. Connect ((SOCKADDR *) & amp; SockAddr, sizeof (SockAddr));
DWORD I=GetLastError ();
If (m_bExit)
{
PostMessage (WM_CLOSE);
}
}

This is not shut down the server to create a connection and connection SOCKET

CodePudding user response:

See the IP, to create a connection within the machine,

CodePudding user response:

It is ok that this code directly away
Simply write blather, perhaps is disorderly copy paste lane
Reality in addition to the beginner, novice, who would have to write code in CSocket? Let alone write a server-side code in CSocket

CodePudding user response:

Have to get rid of what is the effect?

CodePudding user response:

The
reference 3 floor VisualEleven response:
have removed what effect?

You didn't see it CSocket is created within the function? A function which is automatically destroyed, there is a hair effect.
Remove directly, it is ok to keep global parameters of several modified

CodePudding user response:

reference 4 floor danscort2000 response:
Quote: reference VisualEleven reply: 3/f

Have to get rid of what is the effect?

You didn't see it CSocket is created within the function? A function which is automatically destroyed, there is a hair effect.
Remove directly, it is ok to keep global parameters of several modified
remove this function after click button shut down the server didn't respond,,,,

CodePudding user response:

reference 5 floor niruihui reply:
Quote: refer to 4th floor danscort2000 response:

Quote: refer to the third floor VisualEleven response:

Have to get rid of what is the effect?

You didn't see it CSocket is created within the function? A function which is automatically destroyed, there is a hair effect.
Remove directly, it is ok to keep global parameters of several modified
remove this function after click button shut down the server didn't respond,,,,

Do you take the if (m_bExit)
{
PostMessage (WM_CLOSE);
}
Also commented out?

CodePudding user response:

 
Void CChatDlg: : OnSend ()//send button
{
//TODO: Add your the control notification handler code here
Cstrings STR.
M_InputText. GetWindowText (STR);
If (STR) IsEmpty ())
{
AfxMessageBox (" send content cannot be empty!" );
return;
}
M_InputText. SetWindowText (" ");
If (m_ServerOnline)//if chat content from the server
{
Cstrings strSend (" server message: "+ STR);
for (int i=0; i {
M_ServerSocket [I] - & gt; Send (strSend, strSend GetLength () + 1);
}
STR="server:" + STR;
}
If (m_ClientOnline)//if chat content comes from me, that is the client
{
M_ClientSocket - & gt; Send (STR, STR. GetLength () + 1);
STR="I say:" + STR;
}
ShowText (STR);
}

Void CChatDlg: : OnConnect ()//connect to the server button
{
//TODO: Add your the control notification handler code here
If (m_ServerIP IsBlank ())//when the IP address field is empty, popups
{
MessageBox (" please enter the IP address!" , "tip", MB_OK | MB_ICONWARNING);
return;
}
DWORD dwAddress;
M_ServerIP. GetAddress (dwAddress);
The UpdateData ();
If (m_Name. IsEmpty ())
{
Char name [BufferSize];
The gethostname (name, sizeof (name));
M_Name=name;
The UpdateData (FALSE);
}
ShowText (cstrings (" is connect to the server, please wait... "));
SOCKADDR_IN SockAddr.
SockAddr. Sin_family=AF_INET;//TCP/IP protocol
SockAddr. Sin_addr. S_un. S_addr=htonl (dwAddress);
SockAddr. Sin_port=htons (m_ServerPort);//set the server port to the input port
M_ClientSocket=new CClientSocket (this);
M_ClientSocket - & gt; The Create ();//create the client Socket
M_ClientSocket - & gt; SetTimeOut (1000);
If (m_ClientSocket - & gt; The Connect ((SOCKADDR *) & amp; SockAddr, sizeof (SockAddr)))//a connection request

{
M_ClientSocket - & gt; KillTimeOut ();
M_ClientOnline=TRUE;
M_ClientSocket - & gt; Send (m_Name, m_Name GetLength () + 1);
EnableServer (FALSE, FALSE);//make some control input window, not with
EnableClient (FALSE);//make some control input window, not with
ShowText (cstrings (" connect to the server success!" ));
}
The else
{
M_ClientSocket - & gt; KillTimeOut ();
M_ClientSocket - & gt; Close ();
The delete m_ClientSocket;
ShowText (cstrings (" connect to the server failed!" ));
}
}

Void CChatDlg: : OnConnectClose disconnected ()//button
{
//TODO: Add your the control notification handler code here
ConnectClose ();
}

Void CChatDlg: : OnServerCreate ()//build server button
{
//TODO: Add your the control notification handler code here
The UpdateData ();
M_hListenThread=CreateThread (NULL, 0, ListenThread, this, NULL, NULL);
The CloseHandle (m_hListenThread);


}

Void CChatDlg: : OnServerClsoe ()//close the server button
{
//TODO: Add your the control notification handler code here
ServerClose ();
}



DWORD WINAPI CChatDlg: : ListenThread (LPVOID p)
{
//this is a thread function that will create a thread object do translate
PDlg CChatDlg *=(CChatDlg *) p;
//define two socket object
CSocket ListenSocket AcceptSocket;

//according to port initialization ListenSocket
If (ListenSocket. Create (pDlg - & gt; M_ListenPort)==FALSE)
{
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related