c - 打开 : No such file or directory in UDP client

标签 c sockets ubuntu udp

我是套接字编程的新手。我正在编写一个服务器端和一个客户端代码,以在 c 中通过 UDP 发送文件。这两个代码都编译但是当我在 ubuntu bash 上运行它们时,服务器端代码工作正常但客户端给我一个错误:打开:没有这样的文件或目录 谁能帮我解决这个问题!!!

这是服务器端:

/************* 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;
}

这是客户端:

/************* 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);
}

最佳答案

.. i am very new to socket programming..

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

错误open: No such file or directory 与套接字编程无关。我的猜测是 file_path(即 input.tmp)不存在。如果您想创建一个文件以防它不存在,您还需要添加 O_CREAT 标志。参见 open(2) .

关于c - 打开 : No such file or directory in UDP client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40201747/

相关文章:

python - 打包C/Python项目时使用distutils的原因

c - fread 同一个文件,但返回不同的结果

java - W/System.err : java.net.ConnectException:连接超时

Linux 服务监听多个套接字

ubuntu - 日志解析 stackdriver 中来自 textPayload 的警报?

shell - 记录 Shell 输出

c - C 中的数学函数

c++ - 如何获取共享内存中的共享对象

c - Linux UDP 接收 : What do I get at the start of my buffer?

java.io.IOException : Cannot run program "usr/bin/ffmpeg ": error=2, 没有那个文件或目录