私はソケットプログラミングにとって新しいです。私はcでUDP上でファイルを送信するためにサーバー側とクライアント側のコードを書いています。しかし、両方のコードをコンパイルすると、私はubuntu bashでサーバ側のコードはうまく動作しますが、クライアント側でエラーが出ます:open:いいえそのようなファイルやディレクトリ 誰もが私を助けてください!ここでopen:UDPクライアントにそのようなファイルやディレクトリがありません
は、サーバー側である:ここで
/************* 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);
}
ファイルは存在しますか?クライアントが別のディレクトリから実行されている間に相対パスを使用していますか? – blackpen