I watched a console interface from the book of the server-side program, because after the console application receives the message will automatically pop up, so there is no problem, but I want to change it to MFC version of the program, the above problems, how to let any client sent message, the server will automatically displays a message in the list box, the following is my program, how to modify the good,
void CSelwinsDlg: : OnCreate ()
{
/* * * initializes the winsock2. DLL/* * *
WSADATA WSADATA;
WORD wVersionRequested=MAKEWORD (2, 2);//generated version 2.2
If (WSAStartup (wVersionRequested, & amp; WsaData)!=0)
{
C_recvbuf. AddString (" loading winsock DLL failed! \n");
}
/* * * * * */create socket
If ((sock_server=socket (AF_INET SOCK_STREAM, 0)) & lt; 0)
{
C_recvbuf. AddString (" failed to create a socket! \n");
WSACleanup ();
}
/* * * to binding local address * * */
Int addr_len=sizeof (struct sockaddr_in);
Memset ((void *) & amp; Addr, 0, addr_len);
Addr. Sin_family=AF_INET;
Addr. Sin_port=htons (PORT);
Addr. Sin_addr. S_addr=htonl (INADDR_ANY);//allow the socket to use the machine any IP
/* * * to listening socket binding address * * */
If (bind (sock_server, (struct sockaddr *) & amp; Addr, sizeof (addr))!=0)
{
C_recvbuf. AddString (" address binding failed! \n");
Closesocket (sock_server);
WSACleanup ();
}
/* * * will socket set to listening state * * * */
If (listen (sock_server, 0)!=0)
{
C_recvbuf. AddString (" listen function call failed! \n");
Closesocket (sock_server);
WSACleanup ();
}
The else
C_recvbuf. AddString (" listenning... \n");
FD_ZERO (& amp; Fdsock);//initialize fdsock
FD_SET (sock_server, & amp; Fdsock);//add the listening socket to socket set fdsock
/* * * cycle: receive connection requests and send and receive data * * */
While (true)
{
FD_ZERO (& amp; Fdread);//initialize fdread
Fdread=fdsock;//will all the socket in the fdsock added to the fdread
If (select (0, & amp; Fdread, NULL, NULL, NULL) & gt; 0)
{
for(int i=0; i{
If (FD_ISSET (fdsock fd_array [I], & amp; Fdread))
{
If (fdsock fd_array [I]==sock_server)
{//a client connection request arrives, receives the connection request
Newsock=accept (sock_server, (struct sockaddr *) & amp; Client_addr, & amp; Addr_len);
If (newsock==INVALID_SOCKET)
{//accept error to terminate all communication, the end of the program
C_recvbuf. AddString (" the accept function call failed! \n");
for(int j=0; JClosesocket (fdsock fd_array [j]);//close all socket
WSACleanup ();//logout WinSock dynamic link library
}
The else
{
C_recvbuf. AddString (inet_ntoa (client_addr sin_addr));
Send (newsock, MSG, sizeof (MSG), 0).//send a message
FD_SET (newsock, & amp; Fdsock);//to add new socket fdsock
}
}
The else
{//a client data, receive data
Memset ((void *) msgbuffer, 0, sizeof (msgbuffer));//buffer reset
Int size=recv (fdsock fd_array [I], msgbuffer, sizeof (msgbuffer), 0).
If (size
0)//receiving informationC_recvbuf. AddString (receive information failed! "" );
Else if (size==0)
C_recvbuf. AddString (someone has closed!" \n");
The else
{//show received information
Getpeername (fdsock fd_array [I], (struct sockaddr *) & amp; Client_addr, & amp; Addr_len);//get the other IP address
C_recvbuf. AddString (msgbuffer);
}
Closesocket (fdsock fd_array [I]);//close the socket
FD_CLR (fdsock fd_array [I], & amp; Fdsock);//remove the socket closed
}
}
}
}
The else
{
C_recvbuf. AddString (" the Select call failed!" );
break;//terminate the loop exit program
}
}
}
CodePudding user response:
Open threads, specially the select recv news,You this OnCreate is completely blocked, while dead circulation, open a thread, of course, calls OnCreate, so BuKa main interface
CodePudding user response:
the socket of the select function in detailCodePudding user response: