我无法使用套接字在 c 服务器中打印从客户端收到的消息?

I am unable to print message recived from client side in c server using socket?

在调试中,我可以看到 rx_buffer 的值更改为从客户端发送的值,但是 printf 函数甚至 fputs 函数都没有在终端上打印值或更新输出文件

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 
#include <arpa/inet.h>
#include <unistd.h>


// Constants defined
#define SERVER_PORT 3333
#define RX_BUFFER_SIZE 1024
#define TX_BUFFER_SIZE 1024

#define MAXCHAR 1000                // max characters to read from txt file

// Global variables
struct sockaddr_in dest_addr;
struct sockaddr_in source_addr;

char rx_buffer[RX_BUFFER_SIZE];     // buffer to store data from client
char tx_buffer[RX_BUFFER_SIZE];     // buffer to store data to be sent to client

char ipv4_addr_str[128];            // buffer to store IPv4 addresses as string
char ipv4_addr_str_client[128];     // buffer to store IPv4 addresses as string

int listen_sock;

char line_data[MAXCHAR];

FILE *input_fp, *output_fp;



int socket_create(struct sockaddr_in dest_addr, struct sockaddr_in source_addr){

    int addr_family;
    int ip_protocol;

    dest_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    dest_addr.sin_family = AF_INET;
    dest_addr.sin_port = htons(SERVER_PORT);
    addr_family = AF_INET;
    ip_protocol = IPPROTO_IP;

    int sock,p;
        printf("Create the socket\n");
        sock=socket(addr_family , SOCK_STREAM , 0);
        if((bind(sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr)))<0){
               perror("Bind failed.");
               }
        else{

            printf("bind done");
}
char client[100];
        listen(sock,1);
        printf("Waiting for incoming connections...\n");
        p = accept(sock, (struct sockaddr *)&source_addr, (socklen_t*)&source_addr);
         if(p<0){ perror("accept failed");} printf("Client Address=%s\n",inet_ntop(AF_INET,&source_addr.sin_addr,client,sizeof(client)));
return p;


}



int receive_from_send_to_client(int sock){
      char mess[10]="hello";
      int len;
      len=recv(sock , rx_buffer, sizeof(rx_buffer),0);
     send(sock , mess , 5,0);


    return 0;

}


int main() {


    char *output_file_name = "data_from_client.txt";

    // Create socket and accept connection from client
    int sock = socket_create(dest_addr, source_addr);


    output_fp = fopen(output_file_name, "w");

    if (output_fp == NULL){
        printf("Could not open file %s\n",output_file_name);
        return 1;
    }

    while (1) {


        receive_from_send_to_client(sock);
          printf("%s",rx_buffer);
        fputs(rx_buffer, output_fp);
        fputs("\n", output_fp);

    }

    return 0;
}

在调试中,我可以看到 rx_buffer 的值正在改变,但无法将其放入文件或打印消息。

注意:- 我正在从 python 客户端发送消息。

  • 同时,您应该始终打开文件,并在将数据放入文件后正确关闭文件描述符。

在 main() 中查看此代码,

int main() {


    char *output_file_name = "data_from_client.txt";

    // Create socket and accept connection from client
    int sock = socket_create(dest_addr, source_addr);





    while (1) {

    output_fp = fopen(output_file_name, "a+");
  if (output_fp == NULL){
        printf("Could not open file %s\n",output_file_name);
        return 1;
    }

        receive_from_send_to_client(sock);
          printf("%s",rx_buffer);
        fprintf(output_fp,"%s",rx_buffer);
        fclose(output_fp);
    }

    return 0;
}