Why does my read at line 57 print the errno resource already in use?

53 Views Asked by At

Sorry for the damn near unreadability, I've been changing variables and values and printf'ing debug statements all over the place so I can track down my error but I cant seem to find what's causing my read at like 57 to hang.

the fcntl was stopping the read from hanging at 40 and the read at 18 but wouldnt do the same at 57.

This is the Server file.


    #include <unistd.h>
    #include <stdio.h>
    #include <sys/socket.h>
    #include <stdlib.h>
    #include <netinet/in.h>
    #include <string.h>
    #include <arpa/inet.h>
    #include <errno.h>
    #include <fcntl.h>
    void write_file(int sockfd)
    {
        printf("1.0\n");
        int data,filenamdat,st,errnum,bytesize;
        FILE *fp;
        char fileName[100];
        char byte[16];
        printf("1.1\n");
        filenamdat = read(sockfd,fileName,100);
        fcntl(sockfd, F_SETFL, O_NONBLOCK);
        printf("%s\n",fileName);
        if(filenamdat == -1)
        {
            printf("filenamedat...\n");
            errnum = errno;
            fprintf(stderr, "Value of errno: %d\n", errno);
            perror("Error printed by perror");
            fprintf(stderr, "Error opening file: %s\n", strerror( errnum ));
            exit(0);
        }
        printf("1.2\n");
        char *buffer;
        buffer = (char*)malloc(sizeof(buffer)*sizeof(char));
        char filenameadd[107] = "SERVER";
        strcat(filenameadd,fileName);
    
        printf("1.3.0\n");
        fp = fopen(filenameadd, "w");
        printf("1.3.1\n");
        printf("%s\n",filenameadd);
        st = read(sockfd,byte,16);
        fcntl(sockfd, F_SETFL, O_NONBLOCK);
        printf("1.3.2\n");
        bytesize = atoi(byte);
        if(st == -1)
        {
            printf("st...\n");
            errnum = errno;
            fprintf(stderr, "Value of errno: %d\n", errno);
            perror("Error printed by perror");
            fprintf(stderr, "Error opening file: %s\n", strerror( errnum ));
            exit(0);
        }
        printf("1.4\n");
        //This entire while loop is basically the issue. 
        while (1) 
        {
            printf("1.5\n");
            data = read(sockfd, buffer, 1024); 
    //This is the read that hangs no matter what I do.
    //I set it equal to an int and then did an errno check to see what the problem was.
    //It says resource already in use but I cant seem to find a solution to it.

 
            if(data == -1)
            {
                printf("readbuffer...\n");
                errnum = errno;
                fprintf(stderr, "Value of errno: %d\n", errno);
                perror("Error printed by perror");
                fprintf(stderr, "Error opening file: %s\n", strerror( errnum ));
                exit(0);
            }
            printf("%d\n",data);
            printf("1.6\n");
            if (data <= 0)
            {
                printf("1.91\n");
                break;
                return;
            }
            printf("1.7\n");
            fprintf(fp, "%s", buffer);
            printf("1.8\n");
            bzero(buffer, 1024);
            printf("1.9\n\n");
        }
        return;
    }
    void sendFile()
    {
    
    }
    
    void server()
    {
        int sockfd, sock, portnum, connfd, errnum;
        int opt = 1;
        struct sockaddr_in addr,client = {};
        char* addrbuff;
        addrbuff = (char*)malloc(sizeof(addrbuff)*sizeof(void*));
        portnum = 8080;
        sockfd = socket(AF_INET,SOCK_STREAM,0);
        if (sockfd == -1) 
        {
            printf("Failed to create socket.\n");
            exit(0);
        }
        addr.sin_family = AF_INET;
        addr.sin_addr.s_addr = htonl(INADDR_ANY);
        addr.sin_port = htons(portnum);
    
        inet_ntop(AF_INET, &addr.sin_addr.s_addr, addrbuff, sizeof(addr));
    
        if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,&opt, sizeof(opt)))
        {
            perror("setsockopt");
            exit(EXIT_FAILURE);
        }
    
        if ((bind(sockfd, (struct sockaddr*)&addr, sizeof(addr))) != 0) 
        {
            printf("Failed to bind socket.\n");
            printf("Connection with the server failed...\n");
            errnum = errno;
            fprintf(stderr, "Value of errno: %d\n", errno);
            perror("Error printed by perror");
            fprintf(stderr, "Error opening file: %s\n", strerror( errnum ));
            exit(0);
        }
        if ((listen(sockfd, 10)) != 0) 
        {
            printf("Failed to listen.\n");
            errnum = errno;
            fprintf(stderr, "Value of errno: %d\n", errno);
            perror("Error printed by perror");
            fprintf(stderr, "Error opening file: %s\n", strerror( errnum ));
            exit(0);
        }
    
        printf("Listening on %s and port %d.\n", addrbuff, ntohs(addr.sin_port));
    
        socklen_t client_len = sizeof(client);
    
        connfd = accept(sockfd, (struct sockaddr*)&client, &client_len);
    
        if (connfd < 0) 
        {
            printf("Accept failed.\n");
            errnum = errno;
            fprintf(stderr, "Value of errno: %d\n", errno);
            perror("Error printed by perror");
            fprintf(stderr, "Error opening file: %s\n", strerror( errnum ));
            exit(0);
        }
        char command[5];
        char fileName[100];
        char bytes[16];    
        int w,e,st; 
        printf("Client accepted.\n");
        bzero(fileName,100);
        bzero(command,5);
        bzero(bytes,16);
        w = read(connfd,command,5);
        printf("%s\n",command);
        if(strncmp(command, "PUSH",4) == 0)
        {
            printf("1\n");
            write_file(connfd);
            printf("2\n");
        }
        return;
    }
    
    
    
    int main(int argc, char* argv[])
    {
        int port;
        char Address[1024];
        if(argc == 1)
        {
            server();
        }
        else
        {
            printf("%s\n", "Client/server functions never reached.");
            return 0;    
        }
        return 0;
    }

This is the client file

I cant change this file, as it was written by my partner and its read only. It appears to be doing everything it's supposed to though.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <errno.h>
    
    void pushFile(char *fileName, int clientSocket){
        FILE *fp;
        struct stat stats;
        int intfileSizeinBytes;
        char fileSizeinBytes[10];
    
        int w = send(clientSocket, fileName, sizeof(fileName)+1,0);//sending filename to server
        int st = stat(fileName,&stats);
        if(st == -1){
            printf("Error in stat method...\n");
        } else {
            intfileSizeinBytes = (int)stats.st_size;
            intfileSizeinBytes+=2;
            sprintf(fileSizeinBytes,"%d",intfileSizeinBytes);
        }
        printf("sizeoffilebytes: %ld\n",sizeof(fileSizeinBytes));
        w = send(clientSocket, fileSizeinBytes, sizeof(fileSizeinBytes), 0); //send filesize to server
        if(w == -1){
            printf("Error in sending file size...\n");
            exit(0);
        }
    
    
        char data[intfileSizeinBytes];
    
        fp = fopen(fileName, "r");
        if(fp == NULL){
            printf("File can't be opened...\n");
            exit(0);
        }
    
        while(fgets(data, intfileSizeinBytes, fp) != NULL){
            int tmp;
            tmp = send(clientSocket, data, sizeof(intfileSizeinBytes), 0); //sending data to server
            printf("%s\n",data);
            if(tmp == -1){
                printf("Error in sending data...\n");
                exit(0);
            }
            bzero(data, intfileSizeinBytes);
        }
        fclose(fp);
    
    }
    
    void pullFile(char *fileName, int clientSocket, FILE *fp){
        
        char buff[1024], fileLength[100];
        
    
        fp = fopen(fileName, "w+");
        if(fp == NULL){
            printf("File can't be opened...\n");
            exit(0);
        }
    
        int w = send(clientSocket, fileName, sizeof(fileName),0);//send file name to server
        if(w == -1){
            printf("Error in sending file name\n");
            exit(0);
        }
    
        w = recv(clientSocket, fileLength, 100, 0);//recieve file size from server
        if(w == -1){
            printf("Error in recieving file\n");
            exit(0);
        }
    
        int intfileLength = atoi(fileLength);
    
        char file[intfileLength]; //makes a "string" that will contain the file data
    
        w = recv(clientSocket, buff, intfileLength, 0); //recieves the actual file
    
        fprintf(fp, "%s", file); //prints data into file
        bzero(file, intfileLength);
    
        fclose(fp);
    
    }
    
    int main(){
    
        int clientSocket;
        struct sockaddr_in serverAddr;
        char buffer[1024];
    
    
        clientSocket = socket(AF_INET, SOCK_STREAM, 0); //creates a client socket
    
        if(clientSocket == -1){
            printf("Socket failed...\n");
            exit(0);
        }
        printf("Socket success...\n"); 
    
        bzero(&serverAddr, sizeof(serverAddr));
    
        serverAddr.sin_family = AF_INET;
        serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); //Change
        serverAddr.sin_port = htons(8080);
    
        int c = connect(clientSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
    
        if(c == -1){ //connects the socket to the given address
           printf("Connection with the server failed...\n");
           exit(0);
        } 
        printf("Connection with the server success...\n");
    
        int flag = 1;
        printf("This is a remote backup system.\nYou can send files, retrieve files, and exit\n");
        printf("To send files type [PUSH 'filename']\n");
        printf("To retrieve a file from the server type [PULL 'filename']\n");
        printf("To exit type [QUIT]\n");
        while(flag == 1){
            char command[5]; 
            char fileName[100];
    
            scanf("%s", command); //%s %s
    
            if(strncmp(command, "PUSH", 4) == 0){
                printf("Please specify file you want to transer to server:\n");
                scanf("%s", fileName);
                printf("%s\n", fileName);
    
                int w = send(clientSocket, command, sizeof(command), 0);
                if(w == -1){
                    printf("Sending command to server failed...\n");
                    exit(0);
                }
                pushFile(fileName, clientSocket);
                printf("File sent...\n");
    
            } else if(strncmp(command, "PULL", 4) == 0){
                FILE *fp;
                printf("Please specify file you want to recieve from server:\n");
                scanf("%s", fileName);
                printf("%s\n", fileName);
    
                int w = send(clientSocket, command, sizeof(command), 0);
                if(w == -1){
                    printf("Sending command to server failed...\n");
                    exit(0);
                }
                pullFile(fileName, clientSocket, fp);
                printf("File recieved...\n");
                printf("PULL\n");
            } else if(strncmp(command, "QUIT", 4) == 0){
                printf("QUIT\n");
                flag--;
            } else {
            printf("Please enter an actual command\n");
            }
        }
        close(clientSocket);
    
    
        return 0;
        
    }

I've been awake 16 hours trying to work this out and have completely hit a wall. Anything helps even if you see little things I can do to make it more efficient and less spaghetti.

0

There are 0 best solutions below