Advertisement

berkeley sockets and select()

Started by November 18, 2003 11:49 AM
3 comments, last by AcidInjury 21 years, 2 months ago
I am writing a non-blocking linux server and am having some problems with select. There are two main function for the server, checking for new connections and handling messages from clients. When checkig for connections the server assigns the listen_socket to the fd set and checks select() for a readable socket. When the socket is accepted, the socket is saved in a structure array. Later when looking for messages, the server adds all active clients in the array to a different fd set and checks select for readability. The problem I am having is that for some reason whenever I send data from the client, the server thinks it is a connection request and so it sets up a new socket. I am at work and can''t currently post any code, I will do that as soon as I can. If any one has any initial ideas please let me know. Thanks.
are you using TCP or UDP? (SOCK_STREAM or SOCK_DGRAM)
Advertisement
Right now i'm using TCP, although UDP might be easier to implement.

Here is the code i've got, it's slightly modified from the tutorial.

// Check For Connectionsint Check_Connections(){        int i;  // Just a counter        int s;  // s is where the data is stored from the select function        int nfds;        fd_set conn; // Setup the read variable for the Select function        int Client_Length = sizeof(struct sockaddr); // Store the length of the client address        char* ClientIp; // Stores text version of client IP address        timeout.tv_sec=0; // Set the timeout to 0 for Non Blocking        timeout.tv_usec=0;        if(ClientsConnected < MAX_CLIENTS)        {                FD_ZERO(&conn);                FD_SET(listen_sock, &conn);                nfds = listen_sock + 1;                printf("Listen socket is number %d\n", listen_sock);                if((s = select(nfds, &conn, NULL, NULL, &timeout)) == -1)                        printf("Socket error. 'select()' failed.\n");                if(s > 0)                        for(i = 0; i < MAX_CLIENTS; i++)                                if(Client[i].InUse == 0)                                {                                        printf("S is equal to %d.\n", s);                                        Client[i].ClientSocket = accept(listen_sock,                                                        (struct sockaddr *)&Client[i].cli_addr,                                                        &Client_Length);                                        ClientIp = inet_ntoa(Client[ClientsConnected].cli_addr.sin_addr);                                        printf("Accepted connection from '%s'.\n", ClientIp);                                        ClientsConnected++;                                        Client[i].InUse = 1;                                        printf("Client %d now in use. (%d)\n", Client[i].ClientSocket,                                                        Client[ClientsConnected-1].ClientSocket);                                        break; // exit for loop                                }        }        return 1;}


And here is the code for checking sockets for activity.
(A lot if commented out because I was trying to track down problems)

void CheckMessage(){        char mybyte;        int i;        char* ClientIp;        fd_set input_set, exc_set; // Create Input and Error sets for the select function        int s, // This is Used for the Select command to store the value it gets back        nfds; // Used for Compatability        timeout.tv_sec=0; // Used for Timeout        timeout.tv_usec=0;        // Set up the input,and exception sets for select().        FD_ZERO(&input_set);//      FD_ZERO(&exc_set);        nfds = 1;        for(i = 0; i < ClientsConnected;i++)        {                FD_SET(Client[i].ClientSocket,&input_set);//              FD_SET(Client.ClientSocket,&exc_set);<br></font><br>                <font color=blue>if</font>(nfds &lt;= Client[<font color=purple>i</font>].ClientSocket)<br>                        nfds = Client[<font color=purple>i</font>].ClientSocket + 1;<br>        }<br><br>        s = select(nfds,&input_set,NULL,NULL,&timeout);<br><br>        <font color=blue>if</font>(s &gt; 0)<br>        {<br>                printf(<font color=darkred>"Got something.\n"</font>);<br>                <font color=blue>for</font>(i = 0; i &lt; ClientsConnected; i++)<br>                {<br><font color=gray>//                      if (FD_ISSET(Client.ClientSocket, &exc_set))<br></font><br><font color=gray>//                      {<br></font><br><font color=gray>//                              ClientIp = inet_ntoa(Client.cli_addr.sin_addr);<br></font><br><font color=gray>//                              printf("This Client Has Disconnected: %s\n", ClientIp);<br></font><br><font color=gray>//                              Disconnect(i);<br></font><br><font color=gray>//                      }<br></font><br><br>                        <font color=blue>if</font> ((FD_ISSET(Client[<font color=purple>i</font>].ClientSocket, &input_set))) <font color=gray>// Actual Data coming through?<br></font><br>                        {<br>                                printf(<font color=darkred>"Processing Message.\n"</font>);<br>                                ProcessMessage(i);<br>                        }<br>                }<br>        }<br>}<br><br><br></pre><!–ENDSCRIPT–>      <br><br><SPAN CLASS=editedby>[edited by - AcidInjury &#111;n November 18, 2003 7:13:17 PM]</SPAN>   <br><br><SPAN CLASS=editedby>[edited by - AcidInjury on November 18, 2003 7:17:01 PM]</SPAN>
HHAHAHHA! I feel great now! Well thanks for anyone who would have helped, but I found the problem. The problem wasn''t in the server after all.

I hate stupid programming mistakes.

Will
Was the client closing the socket between messages or something?

This topic is closed to new replies.

Advertisement