Areeba Kamil
Areeba Kamil

Reputation: 23

open: No such file or directory in UDP client

i am very new to socket programming. i am writing a server side and a client side code to send a file over UDP in c. both the codes compile however when i run them on ubuntu bash, the server side code works fine but the client side gives me an error: open: No such file or directory can anyone please help me out with this!!!

Here is the server side:

/************* UDP SERVER CODE *******************/


#include <fcntl.h>
#include <netdb.h> /* getprotobyname */
#include <sys/stat.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/types.h>
#include <ctype.h>

int main(){
  int udpSocket, nBytes;
  char buffer[BUFSIZ]; 
  struct sockaddr_in serverAddr, clientAddr;
  struct sockaddr_storage serverStorage;
  socklen_t addr_size, client_addr_size;
  int i;
  int client_sockfd;
  int filefd;
  ssize_t read_return;
  char *file_path = "output.txt";


  /*Create UDP socket*/
  udpSocket = socket(PF_INET, SOCK_DGRAM, 0);

  /*Configure settings in address struct*/
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_port = htons(7891);
  serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  

  /*Bind socket with address struct*/
 if(bind(udpSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr))!=0)
    printf("Not binded\n");
  else
    printf("Binded and listening\n");


  /*Initialize size variable to be used later on*/
  addr_size = sizeof serverStorage;


    while (1) {
    /*client_addr_size = sizeof(clientAddr);
    puts("waiting for client");
    client_sockfd = accept(udpSocket,(struct sockaddr*)&clientAddr,&client_addr_size);*/
    puts("waiting for client");
    nBytes = recvfrom(udpSocket,buffer,BUFSIZ,0,(struct sockaddr *)&serverStorage, &addr_size);

    filefd = open(file_path,O_WRONLY | O_CREAT | O_TRUNC,S_IRUSR | S_IWUSR);
    if (filefd == -1) {
        perror("open");
        exit(EXIT_FAILURE);
    }
    do {
        read_return = read(nBytes, buffer, BUFSIZ); //read from the client's buffer//
        if (read_return == -1) {
            perror("read");
            exit(EXIT_FAILURE);
        }
        if (write(filefd, buffer, read_return) == -1) {
            perror("write");
            exit(EXIT_FAILURE);
        }
    } while (read_return > 0);
    close(filefd);
    close(client_sockfd);
    }
    return EXIT_SUCCESS;
}

Here is the client side:

/************* UDP CLIENT CODE *******************/

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <stdlib.h>
#include <fcntl.h>
#include <netdb.h> /* getprotobyname */
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>

int main(){

  int clientSocket, portNum, nBytes;
  struct sockaddr_in serverAddr;
  socklen_t addr_size;
  char *file_path = "input.tmp";
  int filefd;
  ssize_t read_return;
  char buffer[BUFSIZ];
  char *user_input = NULL;
  char *server_reply = NULL;

  /*Create UDP socket*/
  clientSocket = socket(PF_INET, SOCK_DGRAM, 0);

  /*Configure settings in address struct*/
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_port = htons(7891);
  serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  

  /*Initialize size variable to be used later on*/
  addr_size = sizeof serverAddr;


    while (1) {

    filefd = open(file_path, O_WRONLY | O_APPEND);
    if (filefd == -1) {
    perror("open");
    exit(EXIT_FAILURE);
    }
    else {
        printf("Type a sentence to send to server/file:\n");
        fgets(buffer,BUFSIZ,stdin);
    write (filefd,buffer,BUFSIZ);
     printf("You typed: %s",buffer);
    }

    read_return = read(filefd, buffer, BUFSIZ);
    nBytes = strlen(buffer) + 1;
    if (read_return == 0)//indicated end of file
        break;
    if (read_return == -1) {
        perror("read");
        exit(EXIT_FAILURE);
    }

    /*Send message to server*/
    sendto(clientSocket,buffer,nBytes,0,(struct sockaddr *)&serverAddr,addr_size);

    /*if (write(clientSocket, buffer, read_return) == -1) {
        perror("write");
        exit(EXIT_FAILURE);
    }else{printf("input file read successfully into the buffer\n");}*/

    }
    free(user_input);
    free(server_reply);
    close(filefd);
    exit(EXIT_SUCCESS);
}

Upvotes: 0

Views: 2027

Answers (2)

blackpen
blackpen

Reputation: 2424

open: No such file or directory

That is because the file that you are trying to write doesn't exist (You may want to use O_CREAT).

read: Bad file descriptor

Once you fix the first error, you may also want to fix this second error, which is a result of trying to read from a file that has been opened in write only mode.

Also you may want to write only as many bytes as you have in the buffer (not entire BUFSIZ).

Upvotes: 1

Steffen Ullrich
Steffen Ullrich

Reputation: 123380

.. i am very new to socket programming..

filefd = open(file_path, O_WRONLY | O_APPEND);
if (filefd == -1) {
    perror("open");

The error open: No such file or directory is unrelated to socket programming. My guess is that file_path (i.e. input.tmp) does not exist. If you want to create a file in case it does not exist you need to add the O_CREAT flag too. See open(2).

Upvotes: 3

Related Questions