Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello:

I am trying to create a socket server that is working correctly for me.

I just have a problem. I try to write what I receive from the data in the socket in a file but it is not writing it to me.

The other extreme point sends me the data in ascii text through the socket. I see them in the console of the running service but it does not write it to the file that I want. My idea is also to make that file overwrite everything every 5 second I receive data.

But my main help that I need is to correct the problem to be able to write what comes to me in the file.

Here is an example of the code I'm working on:


C#
<pre>#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <stdlib.h>

#define BUFS 1024
#define MAX_LENGTH 4096

int     Asock;  /* socket en listen on */

/* function prototypes */
void err_log(char []);
void do_server(int);
void cleanUp();

main(int argc, char *argv[])
{
    int     sock;
    int     hostport, len, pid;
    struct sockaddr_in  server, client;

    /* Linea de comando Valida */
    if(argc != 2) {
        fprintf(stderr,"Usage: sockserver <port no.>\n");
        exit(1);
    }

    /* get server port */
    hostport = atoi(argv[1]);

    /* Abre un TCP stream socket */
    if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        err_log("ERROR: socket() failed");
        exit(1);
    }

    /* bind the server port */
    memset((char *) &server, 0, sizeof server);
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    server.sin_port = htons(hostport);
    if(bind(sock, (struct sockaddr *) &server, sizeof server) < 0 ) {
        err_log("ERROR: bind() failed");
        exit(1);
    }
<pre> /* listen on the port for a connection request */
    listen(sock, 2);

    /* do a clean exit when user stops this program via  Break/Delete */
    signal(SIGINT, cleanUp);


    /* infinite loop */
    for( ; ; ) {
        fprintf(stderr, "Listening on port %d...\n", hostport);
        len = sizeof client;
        if((Asock = accept(sock, (struct sockaddr *)&client, &len)) < 0) {
            err_log("ERROR: accept() failed");
            exit(1);
        }

        /* fork a child processes for each connection */
        if((pid = fork()) < 0) {
            err_log("ERROR: fork() failed");
            exit(1);
        }

        if(pid == 0) {  /* child */
            close(sock);
            do_server(Asock);   /* read & write the data */
            exit(0);
        }
        else {          /* parent */
            close(Asock);
        }
    }
}


/*
 * Function: err_log() - error logging
 */
void err_log(char msg[])
{
    FILE    *fp;

    fp = fopen("./sockserver.log", "a+");
    fprintf(fp, "%s\n\n", msg);
    fclose(fp);
    fprintf(stderr, "\n%s\n\n", msg);
}


/*
 * Function: do_server() - write data to stdout
 */
void do_server(int fd)
{
    char buf[BUFS];
    int r;
    int read_bytes(int, char *, int);
<pre>    while((r = read_bytes(fd, buf, 1)) > 0)
        write(1, buf, r);

}


/*
 * Function: read_bytes() - read data into a buffer
 *
 * See UNIX Network Programming, 2nd Edition, Volume 1, pg. 77
 *     by W. Richard Stevens
 */
int read_bytes(int fd, char *buf, int bytes)
{
    int n,r;

    n = bytes;
    while (n > 0) {
        r = read(fd, buf, n);
        if(r > 0) {
            n = n - r;
            buf = buf + r;
        }
        else {
            if(r == 0)
                break;      /* end */
            else
                return(r);  /* error */
        }
    }
    return (bytes - n);
}

/*int write_line(char string[MAX_LENGTH])
{

    FILE *fp;


    fp = fopen("./vdn.log", "a+");
    fwrite(string, sizeof ( char ), strlen(string), fp);
    fclose(fp);
    return 0;
}
*/
int write_line(char string[MAX_LENGTH]){
    FILE *fp;

    char* array = strdup(string);
    char* filename="vdn.txt";
    char* dir=get_current_dir_name();

    char fullpath[MAX_LENGTH];

    strcat(fullpath, dir);
    strcat(fullpath, "/");
    strcat(fullpath, filename);
<pre>
    fp=fopen(fullpath, "a");

    if(fp==NULL)
    {
        return -1 ;
    }

    int i;

    for (i=0; i<strlen(string); i++)
    {
        fprintf(fp,"%c",*(array+i));
    }
    fclose(fp);

    bzero(fullpath, MAX_LENGTH);

    return 0;
}

/*
 * Function: cleanUp() - close the socket quickly
 */
void cleanUp()
{
    err_log("NOTICE: caught intr signal");
    shutdown(Asock, 2); /* clean & quick close */
    close(Asock);
    exit(0);
}


What I have tried:

hello:

I am trying to create a socket server that is working correctly for me.

I just have a problem. I try to write what I receive from the data in the socket in a file but it is not writing it to me.

The other extreme point sends me the data in ascii text through the socket. I see them in the console of the running service but it does not write it to the file that I want. My idea is also to make that file overwrite everything every 5 second I receive data.

But my main help that I need is to correct the problem to be able to write what comes to me in the file.

Here is an example of the code I'm working on
Posted
Comments
Richard MacCutchan 1-May-23 7:28am    
You do not call the write_line function anywhere.
Member 15992924 1-May-23 7:34am    
And in which part do you think I should put it better? in the read?
Thanks for your quick response.
Richard MacCutchan 1-May-23 8:01am    
I have not studied your code close enough to make such a decision. You need to put it in the most appropriate part of the code, which is when you have some data to write.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900