Telnetting my socket listening port is closing the socket

I am new to this website, so I thought of it in advance if this question was answered even though I searched for it before opening it to everyone.

I am using socket programming in one of my C programs. It has server and client modules where the server and client can communicate in both directions. The program works fine as I can send files and messages in both directions. My server program is using the port 3873

and I confirmed this withnetstat -anp | grep 3873

I have observed one strange behavior with a socket, especially when I try to connect a socket using a browser like http://localhost:3873

or telnet localhost 3873

. It immediately closes the socket and the subsequent "netstat -anp | grep 3873 output" confirms that localhost is no longer listening on the port 3873

.

I would really appreciate if someone can shed some light on this behavior. Is the behavior expected?

Here is the relevant section from the server code: The main program initiates a dedicated thread and calls startFileServerMT, which then calls handleClient to serve each client connected to the server on a socket

int handleClient(void *ptr){
    DEBUG("Inside the %s %s() \n",__FILE__,__func__);
    int  connectSOCKET;
    connectSOCKET = (int ) ptr;
    char recvBUFF[4096],sendBUFF[4096];
    char *filename;
    FILE * recvFILE;
    char *header[4096];    
    while(1){
        if( recv(connectSOCKET, recvBUFF, sizeof(recvBUFF), 0) > 0){
            if(!strncmp(recvBUFF,"FBEGIN",6)) {                          
                recvBUFF[strlen(recvBUFF) - 2] = 0;
                parseArgs(header,recvBUFF);                                 
                filename = (char*) strngDup(header[1]);                                
                DEBUG("  About to receive file: %s\n", filename);
            }

            char *rfile = ALLOC(sizeof(char) * (strlen(This.uploadDIR) + strlen(filename) + 35));
            strcpy(rfile,This.uploadDIR);

            if (strngLastChar(rfile) == '/'){
                strcat(rfile,filename);
            }else{
                strcat(rfile,"/");
                strcat(rfile,filename);        
            }
            DEBUG("  Absolute file is : %s\n", rfile);
            recvBUFF[0] = 0;
            if ((recvFILE = fopen (rfile,"w" )) == NULL){
                LogError("Server could not create file %s on the shared location %s.\n",filename,This.uploadDIR);                
            }else{
                bzero(recvBUFF,4096);
                int fr_block_sz, write_sz;
                while((fr_block_sz = recv(connectSOCKET, recvBUFF, 512, 0)) > 0 ){
                    write_sz = fwrite (recvBUFF , sizeof(recvBUFF[0]) , fr_block_sz , recvFILE );
                    DEBUG("  Received buffer is : %s\n", recvBUFF);
                    if(write_sz < fr_block_sz){
                        LogError("Failed writing file %s on the Server shared location.\n",filename);
                        break;
                    }
                    bzero(recvBUFF,4096);
                    recvBUFF[0] = 0;
                    if(write_sz == 0 || fr_block_sz != 512 ){   
                        break;
                    }
                }

                if(fr_block_sz < 0){
                    if(errno == EAGAIN){
                        LogError("Server collection file %s receive timed out.\n",filename);
                    }else{
                        LogError("Failed file %s transfer due to error %d\n",filename,errno);
                        fclose(recvFILE);
                        FREE(rfile);
                        // Start - Following code send failed status to client    
                        sprintf(sendBUFF,"FSTATUS:FAILED\r\n");    

                        if (send(connectSOCKET, sendBUFF, sizeof(sendBUFF), 0) >= 0){
                            DEBUG("File transfer status for file %s sent\n",filename);
                        }else{
                            DEBUG("Failed sending transfer status for file %s\n",filename);
                            return FALSE;
                        } 

                        // End

                        close(connectSOCKET);
                        return FALSE;
                    }
                }
                    DEBUG("File %s received on OM Server successfully.\n",filename);

                    // Start - Following code send failed status to client    
                    sprintf(sendBUFF,"FSTATUS:SUCCESS\r\n");    

                    if (send(connectSOCKET, sendBUFF, sizeof(sendBUFF), 0) >= 0){
                        DEBUG("File transfer status for file %s sent\n",filename);
                    }else{
                        DEBUG("Failed sending transfer status for file %s\n",filename);
                        return FALSE;
                    } 
                    // End

                    fclose(recvFILE);
                    updateTargets(rfile);
                    FREE(rfile);
                    close(connectSOCKET);
                    break;

            }
        }
        else {
            LogInfo("Client dropped connection\n");
        }

/** End*/  
        return TRUE;
    }    
}

int startFileServerMT(){
    DEBUG("Inside %s %s() \n",__FILE__,__func__);

    int listenSOCKET, connectSOCKET[512],thread_status;
    int socketINDEX = 0;

    pthread_t clientFileThread[512];
    socklen_t clientADDRESSLENGTH[512];
    struct sockaddr_in clientADDRESS[512], serverADDRESS;    


    if((listenSOCKET = socket(AF_INET, SOCK_STREAM, 0)) < 0 ){
        LogAbortError("File server could not create socket.\n");
        close(listenSOCKET);
        return FALSE;        
    }

    serverADDRESS.sin_family = AF_INET;
    serverADDRESS.sin_addr.s_addr = htonl(INADDR_ANY);  
    serverADDRESS.sin_port = htons(This.serverport);

    if (bind(listenSOCKET, (struct sockaddr *) &serverADDRESS, sizeof(serverADDRESS)) < 0) {
        LogAbortError("File server could not bind socket and will stop server now.\n");
        close(listenSOCKET);
        This.stopped = TRUE;
        return FALSE;
    }

    if(listen(listenSOCKET, 5) == -1){
        LogAbortError("Server failed to listen on port %d and will stop server now.\n",This.serverport);
        close(listenSOCKET);
        This.stopped = TRUE;
        return FALSE;        
    }else{
        LogInfo("Server listening on port %d successfully.\n",This.serverport);        
    }

    clientADDRESSLENGTH[socketINDEX] = sizeof(clientADDRESS[socketINDEX]);

    while(TRUE){   
       // DEBUG("  Inside the file server main loop index[%d].\n",socketINDEX);

        connectSOCKET[socketINDEX] = accept(listenSOCKET, (struct sockaddr *) &clientADDRESS[socketINDEX], &clientADDRESSLENGTH[socketINDEX]);
            if(connectSOCKET[socketINDEX] < 0){
            LogError("Server could not accept connection.\n");
            close(listenSOCKET);
            return FALSE;
        }else
            DEBUG("  Another client connected to server socket.\n");

        ThreadCreateDetached( &clientFileThread[socketINDEX], handleClient, connectSOCKET[socketINDEX]);     

        if(socketINDEX=512) {
            socketINDEX = 0;
        } else { 
            socketINDEX++;
        }                                  

        if (This.stopped == TRUE){
            close(listenSOCKET);
            return TRUE;
        }      
/** End*/  
//        return TRUE;
    }

    close(listenSOCKET);
    return TRUE;

}

      

+3


source to share


1 answer


This is your problem - or at least part of your problem. Not only is this a "one by one" error (connectSOCKET accepts indices from 0-511 inclusive, so if you're at 512, you've already written past the end of the connectSOCKET array), but instead of comparing, you're doing the job:

    if(socketINDEX=512) { // Error: you are *setting* socketINDEX to 512, 
                          // then immediately *resetting* it back to 0. So
                          // every new connection overrides the existing
                          // socket.
        socketINDEX = 0;
    } else { 
        socketINDEX++;
    }

      



As a side note, you really need to refactor your code and try to clean it up.

0


source







All Articles