   multipal connection
  hello all.i'm using winsock and i want to make multipal connections.i saw a tutorial.. and learned it but i have no idea how to step forward.this code is working for only 1 connection at a time:#pragma comment(lib, "WS2_32.lib")#include <winsock.h>#include <stdio.h>#define NETWORK_ERROR -1#define NETWORK_OK0int main(){char buffer[256];WORD sockVersion;WSADATA wsaData;int nret;sockVersion = MAKEWORD(1, 1);// We'd like Winsock version 1.1// We begin by initializing WinsockWSAStartup(sockVersion, &wsaData);// Next, create the listening socketSOCKET listeningSocket;listeningSocket = socket(AF_INET,// Go over TCP/IP    SOCK_STREAM,   // This is a stream-oriented socket IPPROTO_TCP);// Use TCP rather than UDP// Use a SOCKADDR_IN struct to fill in address informationSOCKADDR_IN serverInfo;serverInfo.sin_family = AF_INET;serverInfo.sin_addr.s_addr = INADDR_ANY;// Since this socket is listening for connections,// any local address will doserverInfo.sin_port = htons(8888);// Convert integer 8888 to network-byte order// and insert into the port field// Bind the socket to our local server addressnret = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));// Make the socket listennret = listen(listeningSocket, 10);// Up to 10 connections may wait at any// one time to be accept()'ed// Wait for a clientSOCKET theClient;theClient = accept(listeningSocket,   NULL,// Address of a sockaddr structure (see explanation below)   NULL);// Address of a variable containing size of sockaddr structwhile (100>3){nret = recv(theClient,    buffer,    256,// Complete size of buffer    0);printf("reaciving a massage: \n");puts(buffer);printf("\n");}// Send and receive from the client, and finally,closesocket(theClient);closesocket(listeningSocket);// Shutdown WinsockWSACleanup();return NETWORK_OK;}any idea how can i improve this so it can have multipal connections?thanks in advance   
Linky;  huh?while (100>3)Theres a couple of options, two are:use multiple threadsand I believe winsock has a func like bsd's select which is "WSASelect"both of these are covered already on this site.   ;Quote:Original post by Anonymous Posterhuh?*** Source Snippet Removed ***Theres a couple of options, two are:use multiple threadsand I believe winsock has a func like bsd's select which is "WSASelect"both of these are covered already on this site.*lol* i used while (100>3)so it will stay there... i wasnt thinking a lot about it..:P.anyways i try to understand about the select and i just cant... can anyone give me another website that explaines or explaine it to me himself?thanks in advance   ;  select is a relatively archaic function, which takes 5 parameters:select( int max_socket, fd_set *read_set, fd_set *write_set, fd_set *exception_set, timeval *timeout );max_socket - the highest socket value you want to check, plus one.(unnecissary if working on windows)read_set - the set of sockets to check/modify if they're ready to   be read from. (or for listening sockets, if there is a pending  connection to be accepted) [passing null to this parameter is   identical to passing an empty set]write_set - the set of sockets to check/modify if they're ready to    be written to. [null to ignore]exception_set - the set of sockets to check for some non-read/write   problem (can be ignored for beginner work) [null to   ignore]timeout - the amount of time to wait for one of the sockets to become  available. (a value of 0 causes select to return immediately,  passing null causes select to check indefinately until one of  the checks is successful)So, select when called will continually check all of the sockets in read_set, write_set, and exception_set to see if the sockets have data to be read, are able to be written to, or have a problem respectively. It returns as soon as one of those socket conditions are met, or the timeout is reached (whichever is first). It returns 0 on timeout, -1 on error, otherwise the number of sockets which met their conditions. The socket sets (and the timeout on some systems) are modified by select to include only those whose conditions are set.So, example time...Assume you have 3 sockets, 1 listening socket (Listener), and two connected sockets (Foo and Bar).To use select to recieve info from these, you'll need two fd_sets; one "working" set, and one "master" set. (since select modifies the set, and we want to remember what sockets we have)Example:before select you make a fd_set like this (note: this isn't code, just a diagram, you need to use FD_SET and its kin to manipulate fd_sets):working = { Listener, Foo, Bar }Assume that a new client tries to connect with you, the client Foo is idle, and the client Bar is sending you info. After select is called, it will have modified the set to look like this: working = { Listener, Bar }fd_sets are just structures, and can be declared as such: fd_set master;fd_set working;For the example, we'll explicitly initialize the master set. In practical code, this will be done programmatically when clients connect (more on this later).FD_SET(Listener, &master);FD_SET(Foo, &master);FD_SET(Bar, &master);So, for the common server setup, you'll be in a loop waiting for someone to connect with you, or send you info. (for this example, assume Bar is the highest socket; once again in practical code you'll keep track of this as clients connect/disconnect, or ignore it since you're on windows)SOCKET Newbie;while(!quit_condition){    working = master;    // Copy the set, so select doesn't mangle what//  we want to remember.    if( -1 != select(Bar+1, &working, 0, 0, 0) ){   //   // So write_set, and exception_set are null, since we don't care   //  about them for this example. timeout is null as well, since the   //  server just wants to wait until something happens.   //   if( FD_ISSET( Listener, &working ) ){  //  // select modifies the set to what is ready.   //  This checks if there is a client waiting to be accepted.  //  Newbie=accept(Listener,...,...);  if (-1 == Newbie){ // // Error. //  }else{ // // Add newbie to the master set (to check in the next loop //  iteration if it has stuff to say), and do application  //  specific things. // FD_SET(Newbie, &master);  }   }   //   // Now, check "normal" sockets.   //   // For the example, we'll explicitly check. In practical code   //  you'll loop through an array/list/vector of "connected" sockets.   //   if( FD_ISSET( Foo, &working ) ){  //  // recv here. If recv returns <=0, disconnect client.  //   }   if( FD_ISSET( Bar, &working ) ){  //  //  //   }    }else{   //    // select returns -1, some error condition occured   //    }}And that's it. The basic server loop. select is a bit antiquated and archaic, but it works and it works everywhere.   ;Quote:Original post by Telastynselect is a relatively archaic function, which takes 5 parameters:select( int max_socket, fd_set *read_set, fd_set *write_set, fd_set *exception_set, timeval *timeout );max_socket - the highest socket value you want to check, plus one.(unnecissary if working on windows)read_set - the set of sockets to check/modify if they're ready to   be read from. (or for listening sockets, if there is a pending  connection to be accepted) [passing null to this parameter is   identical to passing an empty set]write_set - the set of sockets to check/modify if they're ready to    be written to. [null to ignore]exception_set - the set of sockets to check for some non-read/write   problem (can be ignored for beginner work) [null to   ignore]timeout - the amount of time to wait for one of the sockets to become  available. (a value of 0 causes select to return immediately,  passing null causes select to check indefinately until one of  the checks is successful)So, select when called will continually check all of the sockets in read_set, write_set, and exception_set to see if the sockets have data to be read, are able to be written to, or have a problem respectively. It returns as soon as one of those socket conditions are met, or the timeout is reached (whichever is first). It returns 0 on timeout, -1 on error, otherwise the number of sockets which met their conditions. The socket sets (and the timeout on some systems) are modified by select to include only those whose conditions are set.So, example time...Assume you have 3 sockets, 1 listening socket (Listener), and two connected sockets (Foo and Bar).To use select to recieve info from these, you'll need two fd_sets; one "working" set, and one "master" set. (since select modifies the set, and we want to remember what sockets we have)Example:before select you make a fd_set like this (note: this isn't code, just a diagram, you need to use FD_SET and its kin to manipulate fd_sets):working = { Listener, Foo, Bar }Assume that a new client tries to connect with you, the client Foo is idle, and the client Bar is sending you info. After select is called, it will have modified the set to look like this: working = { Listener, Bar }fd_sets are just structures, and can be declared as such: fd_set master;fd_set working;For the example, we'll explicitly initialize the master set. In practical code, this will be done programmatically when clients connect (more on this later).FD_SET(Listener, &master);FD_SET(Foo, &master);FD_SET(Bar, &master);So, for the common server setup, you'll be in a loop waiting for someone to connect with you, or send you info. (for this example, assume Bar is the highest socket; once again in practical code you'll keep track of this as clients connect/disconnect, or ignore it since you're on windows)*** Source Snippet Removed ***And that's it. The basic server loop. select is a bit antiquated and archaic, but it works and it works everywhere.thanks a lot!!!!!!!i can see it took you some time to write itand i appriciate it.again... thanks! :)   