DEMO code of the select model on a single server and multiple clients

Source: Internet
Author: User

# Include <stdio. h>
# Include <iostream>
# Include <winsock2.h>
Using namespace STD;
Int main (){
Word version = makeword (2, 2 );
Wsadata;
Socket listensocket;
Socket acceptsocket;
Socket clientsocket [fd_setsize];
Int static Index = 0;
DWORD block = 1l;
Fd_set readset;
// Load the socket Library

Int F = wsastartup (version, & wsadata );
If (F! = 0 ){
Cout <"An error occurred while loading the Winsock database! Error Code: "<wsagetlasterror () <Endl;
Return-1;

}

// Create a listener socket

Listensocket = socket (af_inet, sock_stream, ipproto_ip );
If (listensocket = invalid_socket ){
Wsacleanup ();
Cout <"failed to create the listening socket! Error Code: "<wsagetlasterror () <Endl;
Return-1;

}
Cout <"listening socket created successfully! \ N "<Endl;

// Construct the server address

Sockaddr_in serverip;

Serverip. sin_family = af_inet;
Serverip. sin_port = htons (8108 );
Serverip. sin_addr.s_un.s_addr = htonl (inaddr_any );
If (BIND (listensocket, (sockaddr *) & serverip, sizeof (sockaddr) = socket_error ){
Cout <"server binding error! Error Code: "<wsagetlasterror () <Endl;
Return-1;

 

}

Cout <"binding successful! \ N "<Endl;
If (Listen (listensocket, 10) = socket_error ){
Cout <"error in listening mode! Error Code: "<wsagetlasterror () <Endl;
Return-1;
}
Cout <"the server has entered the listening mode! \ N "<Endl;

If (ioctlsocket (listensocket, fionbio, & Block) = socket_error ){
Cout <"the error message returned when the listener socket is set to non-blocking mode! Error Code: "<wsagetlasterror () <Endl;

Return-1;

}

Cout <"the listener socket has been set to non-blocking mode! \ N "<Endl;

// Accept client connection in non-blocking mode

While (true ){

Cout <"continues to accept client connection requests in non-blocking mode! \ N "<Endl;
// Clear the read set to 0

Fd_zero (& readset );

// Place the existing client connection to the collection!

For (INT I = 0; I <index; I ){

Cout <"put clientsocket [" <I <"] into the readset collection! \ N "<Endl;

Fd_set (clientsocket [I], & readset );
}
Cout <"listening socket into readset collection! \ N "<Endl;

Fd_set (listensocket, & readset );
If (select (0, & readset, null) = socket_error

Cout <"socet error of select listening! Error Code: "<wsagetlasterror () <Endl;
System ("pause ");
Return-1;
}

Cout <"if the SELECT statement is returned, the existing client is connected, the client data is read, or the client connection is disconnected! \ N "<Endl;

If (fd_isset (listensocket, & readset )){

Cout <"the current readset stores the listening socket! \ N "<Endl;
Acceptsocket = accept (listensocket, null, null );
If (acceptsocket! = Invalid_socket ){
Cout <"the client established a connection and saved it to clientsocket [" <index <! \ N "<Endl;
Clientsocket [Index] = acceptsocket;
Index ++;

} Else {
Cout <"client connection error received! Error Code: "<wsagetlasterror () <Endl;

Continue;

}
}

For (Int J = 0; j <index; j ){

If (fd_isset (clientsocket [J], & readset )){
Cout <"the current readset set contains client socket clientsocket [" <j <"] readable data! \ N [Message]"

Char buffer [100]; memset (buffer, 0,100 );

If (Recv (clientsocket [J], buffer, 100,0 )! = Socket_error ){

Cout <buffer <"[/messsage] \ n" <Endl;

} Else {

Cout <"client clientsocket [" <j <"] error! Error Code: "<wsagetlasterror () <Endl;
Clientsocket [J] = NULL;

Index --;
Closesocket (clientsocket [J])
Break;

}

}}
}
}

 

 

The code below is basically the same as the while (true) section described above, but I personally feel that the following method is better.

While (true)
{

Fd_set fdread = fdsocket;
If (: Select (0, & fdsocket, null) = socket_error)
{
Continue;
}
Else
{
Trace ("Wait somebody % I \ n", fdsocket. fd_count );
For (INT I = 0; I <(INT) fdsocket. fd_count; I ++)
{

If (fd_isset (fdsocket. fd_array [I], & fdsocket ))
{
If (fdsocket. fd_array [I] = slisten)
{
Cstring strip;
Sockaddr_in addrremote;
Int naddrlen = sizeof (addrremote );
Socket snew =: accept (slisten, (sockaddr *) & addrremote, & naddrlen );
Fd_set (snew, & fdsocket );
Strip. Format ("% s", inet_ntoa (addrremote. sin_addr ));
Trace ("new come % s \ n", strip );

} // End if = slisten
Else
{
Int nrecev =: Recv (fdsocket. fd_array [I], (char *) & recvpacket, sizeof (recvpacket), 0 );
If (nrecev> 0)
{
Trace ("Recv MSG % s \ n", recvpacket );

} Else
{
: Closesocket (fdsocket. fd_array [I]);
Fd_clr (fdsocket. fd_array [I], & fdsocket );
}
}
}

}
}
}
Return 0;

 

 

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.