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

标签 c sockets serversocket

在调试中,我可以看到 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 客户端发送消息。

最佳答案

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

在 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;
}

关于c - 我无法使用套接字在 C 服务器中打印从客户端收到的消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58837718/

相关文章:

c++ - 阻塞套接字 - 检测状态

Java - 无异常地关闭 ServerSocket?

java - Android TCP Listener 无法在 3G/4G 上运行?

c - c中的memcpy后断言失败

C 程序打印重复字符

php - 当我尝试在 php 中将套接字绑定(bind)到本地主机时获取 'Address already in use'

Java Server Socket绑定(bind)多个域名/主机名

c - strstr() 用于非空终止的字符串

c - channel 未阻塞时 libssh2_channel_write 的行为

java - 当发送到服务器的消息数量较多时,Netty 中客户端收不到消息