c-正确地将数据从客户端发送到服务器.服务器未接收到来自客户端的完整文件



我正在尝试将JSON文件从客户端发送到服务器。我有一个工作程序,但是,我打印是为了查看服务器在缓冲区中接收到的内容,而不是逐行打印文件。我相信这与while循环中将数据放入缓冲区有关。我已经附上了while循环的代码部分。我还附上了我的服务器、客户端和JSON文件代码。有人能帮我吗?非常感谢。

//while loop
while (fgets(buff,MAX,fp) != NULL ) // fgets reads upto MAX character or EOF 
write(sockfd,buff,sizeof(buff));    // sent the file data to stream
//client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <json-c/json.h>
#define MAX 100
#define SA struct sockaddr 
void sentFile(int sockfd) 
{ 
char buff[MAX];                         // for read operation from file and used to sent operation 
// create file 
FILE *fp=fopen("myconfig.json","r");    // open file uses both stdio and stdin header files
// file should be present at the program directory
if( fp == NULL ){
printf("Error IN Opening File .. n");
return ;
}
while (fgets(buff,MAX,fp) != NULL ) // fgets reads upto MAX character or EOF 
write(sockfd,buff,sizeof(buff));    // sent the file data to stream
fclose (fp);                            // close the file 
printf("File Sent successfully !!! n");
} 
void parse(int sockfd) {
char buff[MAX];
struct json_object *parsed_json; //structure that holds parsed JSON
//stores rest of fields of the JSON file
struct json_object *Server_IP_Address;
struct json_object *Source_Port_Number_UDP;
struct json_object *Destination_Port_Number_TCP_Head;
struct json_object *Destination_Port_Number_TCP_Tail;
struct json_object *Port_Number_TCP;
struct json_object *Size_UDP_Payload;
struct json_object *Inter_Measurement_Time;
struct json_object *Number_UDP_Packets;
struct json_object *TTL_UDP_Packets;
FILE *fp=fopen("myconfig.json","r");
fread(buff, 1024, 1, fp); //reads files and puts contents inside buffer
parsed_json = json_tokener_parse(buff); //parse json file's contents and convert them into a json object
json_object_object_get_ex(parsed_json, "Server_IP_Address", &Server_IP_Address);
json_object_object_get_ex(parsed_json, "Source_Port_Number_UDP", &Source_Port_Number_UDP);
json_object_object_get_ex(parsed_json, "Destination_Port_Number_TCP_Head", &Destination_Port_Number_TCP_Head);
json_object_object_get_ex(parsed_json, "Destination_Port_Number_TCP_Tail", &Destination_Port_Number_TCP_Tail);
json_object_object_get_ex(parsed_json, "Port_Number_TCP", &Port_Number_TCP);
json_object_object_get_ex(parsed_json, "Size_UDP_Payload", &Size_UDP_Payload);
json_object_object_get_ex(parsed_json, "Inter_Measurement_Time", &Inter_Measurement_Time);
json_object_object_get_ex(parsed_json, "Number_UDP_Packets", &Number_UDP_Packets);
json_object_object_get_ex(parsed_json, "TTL_UDP_Packets", &TTL_UDP_Packets);
printf("Server_IP_Address: %sn", json_object_get_string(Server_IP_Address));
printf("Source_Port_Number_UDP: %sn", json_object_get_string(Source_Port_Number_UDP));
printf("Destination_Port_Number_TCP_Head: %sn", json_object_get_string(Destination_Port_Number_TCP_Head));
printf("Destination_Port_Number_TCP_Tail: %sn", json_object_get_string(Destination_Port_Number_TCP_Tail));
printf("Port_Number_TCP: %sn", json_object_get_string(Port_Number_TCP));
printf("Size_UDP_Payload: %sn", json_object_get_string(Size_UDP_Payload));
printf("Inter_Measurement_Time: %sn", json_object_get_string(Inter_Measurement_Time));
printf("Number_UDP_Packets: %dn", json_object_get_int(Number_UDP_Packets));
printf("TTL_UDP_Packets: %dn", json_object_get_int(TTL_UDP_Packets));
}
int main(int argc, char *argv[]) 
{ 
int sockfd, connfd; 
struct sockaddr_in serv_addr, cli; 
// socket create and varification 
sockfd = socket(AF_INET, SOCK_STREAM, 0); 
if (sockfd == -1) { 
printf("socket creation failed...n"); 
exit(0); 
} 
else
printf("Socket successfully created..n"); 
bzero(&serv_addr, sizeof(serv_addr)); 
// assign IP, PORT 
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr("10.0.0.249");
serv_addr.sin_port = htons(8765);
// connect the client socket to server socket 
if (connect(sockfd, (SA*)&serv_addr, sizeof(serv_addr)) != 0) { 
printf("connection with the server failed...n"); 
exit(0); 
} 
else
printf("connected to the server..n"); 
FILE *fp = fopen(argv[1], "r");
// function for sending File 
sentFile(sockfd); 
parse(sockfd);
// close the socket 
close(sockfd); 
} 
//server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <json-c/json.h>
#define MAX 100
#define SA struct sockaddr 
void recvFile(int sockfd) 
{ 
char buff[MAX];     // to store message from client
FILE *fp=fopen("myconfig.json","w");
if( fp == NULL ){
printf("Error IN Opening File ");
return ;
}
while( read(sockfd,buff,MAX) > 0 )
fprintf(fp,"%s",buff);
} 
void parse(int sockfd) {
char buff[MAX];
struct json_object *parsed_json; //structure that holds parsed JSON
//stores rest of fields of the JSON file
struct json_object *Server_IP_Address;
struct json_object *Source_Port_Number_UDP;
struct json_object *Destination_Port_Number_TCP_Head;
struct json_object *Destination_Port_Number_TCP_Tail;
struct json_object *Port_Number_TCP;
struct json_object *Size_UDP_Payload;
struct json_object *Inter_Measurement_Time;
struct json_object *Number_UDP_Packets;
struct json_object *TTL_UDP_Packets;
FILE *fp=fopen("myconfig.json","r");
fread(buff, 1024, 1, fp); //reads files and puts contents inside buffer
parsed_json = json_tokener_parse(buff); //parse json file's contents and convert them into a json object
json_object_object_get_ex(parsed_json, "Server_IP_Address", &Server_IP_Address);
json_object_object_get_ex(parsed_json, "Source_Port_Number_UDP", &Source_Port_Number_UDP);
json_object_object_get_ex(parsed_json, "Destination_Port_Number_TCP_Head", &Destination_Port_Number_TCP_Head);
json_object_object_get_ex(parsed_json, "Destination_Port_Number_TCP_Tail", &Destination_Port_Number_TCP_Tail);
json_object_object_get_ex(parsed_json, "Port_Number_TCP", &Port_Number_TCP);
json_object_object_get_ex(parsed_json, "Size_UDP_Payload", &Size_UDP_Payload);
json_object_object_get_ex(parsed_json, "Inter_Measurement_Time", &Inter_Measurement_Time);
json_object_object_get_ex(parsed_json, "Number_UDP_Packets", &Number_UDP_Packets);
json_object_object_get_ex(parsed_json, "TTL_UDP_Packets", &TTL_UDP_Packets);
printf("Server_IP_Address: %sn", json_object_get_string(Server_IP_Address));
printf("Source_Port_Number_UDP: %sn", json_object_get_string(Source_Port_Number_UDP));
printf("Destination_Port_Number_TCP_Head: %sn", json_object_get_string(Destination_Port_Number_TCP_Head));
printf("Destination_Port_Number_TCP_Tail: %sn", json_object_get_string(Destination_Port_Number_TCP_Tail));
printf("Port_Number_TCP: %sn", json_object_get_string(Port_Number_TCP));
printf("Size_UDP_Payload: %sn", json_object_get_string(Size_UDP_Payload));
printf("Inter_Measurement_Time: %sn", json_object_get_string(Inter_Measurement_Time));
printf("Number_UDP_Packets: %dn", json_object_get_int(Number_UDP_Packets));
printf("TTL_UDP_Packets: %dn", json_object_get_int(TTL_UDP_Packets));
}
int main(int argc, char *argv[])  
{ 
int sockfd, connfd, len;                // create socket file descriptor 
struct sockaddr_in serv_addr, cli;      // create structure object of sockaddr_in for client and server
// socket create and verification 
sockfd = socket(AF_INET, SOCK_STREAM, 0);           // creating a TCP socket ( SOCK_STREAM )
if (sockfd == -1) { 
printf("socket creation failed...n"); 
exit(0); 
} 
else
printf("Socket successfully created..n"); 
// empty the 
bzero(&serv_addr, sizeof(serv_addr)); 
// assign IP, PORT 
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr("10.0.0.218");
serv_addr.sin_port = htons(8765);
// Binding newly created socket to given IP and verification 
if ((bind(sockfd, (SA*)&serv_addr, sizeof(serv_addr))) != 0) { 
printf("socket bind failed...n"); 
exit(0); 
} 
else
printf("Socket successfully binded..n"); 
// Now server is ready to listen and verification 
if ((listen(sockfd, 5)) != 0) { 
printf("Listen failed...n"); 
exit(0); 
} 
else
printf("Server listening..n"); 
FILE *fp = fopen(argv[1], "w");
len = sizeof(cli); 
// Accept the data packet from client and verification 
connfd = accept(sockfd, (SA*)&cli, &len);   // accepts connection from socket
if (connfd < 0) { 
printf("server acccept failed...n"); 
exit(0); 
} 
else
printf("server acccept the client...n"); 
// Function for chatting between client and server 
recvFile(connfd);
parse(sockfd);
// After transfer close the socket 
close(sockfd); 
} 
{
//myconfig.json
"Server_IP_Address": "127.0.0.1",
"Source_Port_Number_UDP": "9876",
"Destination_Port_Number_UDP": "8765",
"Destination_Port_Number_TCP_Head": "x",
"Destination_Port_Number_TCP_Tail": "y",
"Port_Number_TCP": "22",
"Size_UDP_Payload": "1000",
"Inter_Measurement_Time": "15",
"Number_UDP_Packets": "6000",
"TTL_UDP_Packets": "255",
}

问题出现在write((中:

write(sockfd,buff,sizeof(buff));

应该是

write(sockfd,buff,strlen(buff) + 1);

请参阅此处:如何在c程序中通过TCP套接字编程中的读写函数传递整个字符串

相关内容

  • 没有找到相关文章

最新更新