我有此UDP客户端-服务器模型,其中服务器将文件发送到客户端。一切正常,除了在客户端创建的文件包含一些垃圾数据。我不知道为什么会这样。
server.c
#include <stdio.h>
#include <sys/socket.h> //for sock()
#include <string.h>//for using memset
#include <arpa/inet.h>// for inet_addr()
#include <unistd.h>//for using write() function
int main(){
int sock=0;
char data_send[1024],file_name[1024],symbol;
struct sockaddr_in ServerIp;
struct sockaddr_storage ClientIp;
//AF_INET : The address family value for IPv4 addresses
// SOCK_DGRAM argument confirms that we are using UDP as a protocol in this communication
sock = socket(AF_INET, SOCK_DGRAM, 0);
socklen_t addr_len;
addr_len = sizeof(ClientIp);
//addr_len to store the length of client address
//https://stackoverflow.com/questions/13327155/memset-definition-and-use
memset(&ServerIp,'0',sizeof(ServerIp) );
ServerIp.sin_family = AF_INET;
ServerIp.sin_port = htons(1234);
ServerIp.sin_addr . s_addr = inet_addr("127.0.0.1");
if( bind( sock,(struct sockaddr* )&ServerIp, sizeof(ServerIp)) == -1 )
printf("\n Socket binding failed \n");
else
printf("\n Server Started !!\n ");
// https://linux.die.net/man/2/recvfrom
recvfrom( sock,file_name, sizeof(file_name) ,0, (struct sockaddr *)&ClientIp,&addr_len );
printf("\n filename received from client: %s \n",file_name);
FILE *fp = fopen("test.txt", "r");
if(fp == NULL){
printf("cannot open file");
}
else{
while((symbol = getc(fp)) != EOF) {
strcat(data_send,&symbol);
}
}
fclose(fp);
if( (sendto(sock,data_send, sizeof(data_send) ,0, (struct sockaddr *)&ClientIp, addr_len)) ==-1 )
printf("sending failed\n");
//sendto(sock,data_send,sizeof(data_send),0, (struct sockaddr *)&ClientIp, sizeof(ClientIp) );
printf(" Message send is : %s \n",data_send);
return 0;
}
client.c
#include <stdio.h>
#include <sys/socket.h> //for sock()
#include <string.h>//for using memset
#include <arpa/inet.h>// for inet_addr()
#include <unistd.h>//for using write() function
#include <netinet/in.h>
int main(){
int sock=0;
char data_send[1024],data_received[1024];
struct sockaddr_in ServerIp;
if( (sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0 )
printf("\n Socket creation failed \n");
memset(&ServerIp,'0',sizeof(ServerIp) );
ServerIp.sin_family = AF_INET;
ServerIp.sin_port = htons(1234);
ServerIp.sin_addr . s_addr = inet_addr("127.0.0.1");
printf(" Enter the file name to be send to server: \n>> ");
fgets(data_send, sizeof(data_send), stdin );
sendto(sock,data_send,sizeof(data_send),0, (struct sockaddr *)&ServerIp, sizeof(ServerIp) );
printf(" Message send to server is : %s \n",data_send);
recvfrom(sock,data_received,sizeof(data_received) ,0,NULL,NULL );
FILE *fp = fopen("response.txt", "w");
if (fp == NULL){
printf("Error opening file!\n");
}
fprintf(fp,"%s",data_received);
printf("\n Data received from server: %s \n",data_received);
return 0;
}
如果服务器中的“ test.txt”文件包含这样的内容
1. The server starts and waits for filename.
2. The client sends a filename.
3. The server receives filename.
If file is present,
然后,客户端生成的输出文件“ response.txt”将在产生的输出的所有字符之间具有<0x10>。但是如果我将其打印为输出,那就没问题了。
我无法将其粘贴到这里,因为如果我将其粘贴到这里,它将看起来很好