Search code examples
csocketsserversocket

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


In debugging I can see that values of rx_buffer changes to what is send from client but printf function and even fputs function is not printng the value on terminal or updating the output file

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

In debugging I can see that values of rx_buffer are changing but not able to put that in file or print the message.

Note:- I am sending message from a python client.


Solution

    • in while ,you should open your file always and after putting data into the file close file descriptor properly.

    see this code in 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;
    }