Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3440012
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:22:52+00:00 2026-05-18T08:22:52+00:00

I’m trying to get a simple send and receive UDP program working, but I’m

  • 0

I’m trying to get a simple send and receive UDP program working, but I’m having a bit of trouble with saving the received data. As far as I can tell the data is being sent and received properly, as I’ve printed it out on both ends. Before I write the data to the file (if I just print out the received chunk) it doesn’t have any extra characters, so I’m a bit lost as to where they are coming from.

When I append each chunk of received data to the file it adds a “^P^B^GÐ^?” after every chunk written. for example one of the chunks ended with “We, therefore^P^B^GÐ^?,” instead of “We, therefore,”.

Any help is appreciated, thanks in advance.


UPDATE:

I’ve seemed to have gotten things working semi-better, I’m now having an issue with it replacing the first character of every chunk with a null character, for example:

“^@N CONGRESS, July 4, 1776.” instead of “IN CONGRESS, July 4, 1776.”

It’s doing this for the first char of every chunk received, I’ve tried multiple debug print statements but can’t seem to figure out what the issue is.

Here is my Receive and Send functions:

void receiveFile() {
int addr_len, bytesRead;
char recvData[BUFSIZE]; // Buffer to store received data
struct sockaddr_in server_addr, client_addr;

// Set up struct to receive data from our port and address
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(server_addr.sin_zero), 8);

addr_len = sizeof (struct sockaddr);

printf("\nWaiting for data on port %d\n", port);


//Keep reading data from the socket
while (1) {
    FILE *fp;
    fp=fopen("dummyfile.txt", "ab");

    memset(recvData, 0, BUFSIZE);
    bytesRead = recvfrom(sock, recvData, BUFSIZE, 0,
            (struct sockaddr *) &client_addr, &addr_len);
    int x;
    for(x = 0; x < bytesRead; x++) {
        fputc(recvData[x], fp);
    }

    // Print out who we're receiving from and what we're recieving
    printf("Receiving data from %s : %d\n", inet_ntoa(client_addr.sin_addr),
            ntohs(client_addr.sin_port));

    fclose(fp);
}}

Here is the Send Function:

void sendFile() {
// Announce who we're sending data to
if(DEBUG) { printf("\nSending %s to %s:%d\n", filename, address, port); }

// Open file
FILE * file = fopen(filename, "rb");
if (file == NULL) {
  perror("Invalid File\n");
  exit(1);
}

// Get size of the file
fseek(file, 0, SEEK_END);
int filesize = ftell(file);
rewind(file);

int curPos = 0;
int dataSize = 0;

while(curPos < filesize) {

    struct sockaddr_in server_addr; 
    struct hostent *recvr;

    char sendData[BUFSIZE]; // stores message to be sent
    memset(sendData, 0, BUFSIZE);

    int byte, i;
    for(i = 0; i < BUFSIZE; i++){
        if((filesize - curPos) > 0) {
            byte = fgetc(file);
            sendData[i] = byte;
            curPos++;
            dataSize++;
        }
        else { break; }
    }

    recvr = gethostbyname(address);
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(port);
    server_addr.sin_addr = *((struct in_addr *) recvr->h_addr);
    bzero(&(server_addr.sin_zero), 8);

    if(DEBUG) {
        char tempData[1201];
        strncpy(tempData, sendData, 1200);
        tempData[1201] ='\0';
        printf("%s\n\n\n\n\n", tempData);
    }

    sendto(sock, sendData, dataSize, 0,
            (struct sockaddr *) &server_addr, sizeof (struct sockaddr));
    dataSize = 0;
}

fclose(file);}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:22:53+00:00Added an answer on May 18, 2026 at 8:22 am

    What happens when you change the printing to:

    fprintf(fp, "%.*s", bytesRead, recvData);
    

    There is a guarantee that recvfrom() will not null terminate your messages; you would have to transmit the null terminator yourself.


    I can’t tell what your residual problem is. I have the following to complete programs working back to back. I’ve scrutinized the saved file for NULs with no problem.

    I ran them as:

    ./recv & sleep 1; ./send; kill %1
    

    recv.c

    #include "posixver.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <netinet/in.h> /* sockaddr_in */
    #include <arpa/inet.h>  /* inet_ntoa() */
    #include "stderr.h"
    
    static void receiveFile(int sock, int port, char *filename)
    {
        //Keep reading data from the socket
        FILE *fp = fopen(filename, "ab");
        if (fp == 0)
            err_syserr("failed to open file %s", filename);
    
        printf("\nWaiting for data on port %d\n", port);
        while (1)
        {
            char recvData[BUFSIZ]; // Buffer to store received data
            struct sockaddr_storage addr;
            struct sockaddr_in *client_addr = (struct sockaddr_in *)&addr;
            memset(recvData, 0, sizeof(recvData));
            socklen_t addr_len = sizeof (struct sockaddr_storage);
            int bytesRead = recvfrom(sock, recvData, sizeof(recvData), 0,
                    (struct sockaddr *) &client_addr, &addr_len);
            if (bytesRead < 0)
                err_syserr("Failed to read from socket");
            err_remark("Read %d bytes\n", bytesRead);
            for (int x = 0; x < bytesRead; x++)
            {
                fputc(recvData[x], fp);
            }
            fflush(fp);
    
            // Print out who we're receiving from and what we're receiving
            //char *rem_host = inet_ntoa(client_addr->sin_addr);
            //int   rem_port = ntohs(client_addr->sin_port);
            //printf("Receiving %d bytes from %s:%d\n", bytesRead, rem_host ? rem_host : "<unknown>", rem_port);
        }
        fclose(fp);
    }
    
    int main(int argc, char **argv)
    {
        int fd;
        struct sockaddr_storage addr;
        struct sockaddr_in *server_addr = (struct sockaddr_in *)&addr;
    
        memset(&addr, 0, sizeof(addr));
        server_addr->sin_family = AF_INET;
        server_addr->sin_addr.s_addr = htonl(INADDR_ANY);
        server_addr->sin_port = htons(5190);
    
        err_setarg0(argv[0]);
        if (argc > 1)
            err_usage("");
        if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
            err_syserr("Failed to open DGRAM socket");
        if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) != 0)
            err_syserr("Failed to bind DGRAM socket");
        receiveFile(fd, 5190, "dummy.text");
    
        return(0);
    }
    

    send.c

    #include "posixver.h"
    #include <stdio.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <stdlib.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include "stderr.h"
    
    #define bzero(b,len) (memset((b), '\0', (len)), (void)0)
    
    enum { DEBUG = 1 };
    
    static void sendFile(int sock, const char *filename, char *address, int port)
    {
        // Announce who we're sending data to
        if (DEBUG)
            printf("\nSending %s to %s:%d\n", filename, address, port);
    
        // Open file
        FILE * file = fopen(filename, "rb");
        if (file == 0)
            err_syserr("Failed to open file %s", filename);
    
        // Get size of the file
        fseek(file, 0, SEEK_END);
        int filesize = ftell(file);
        rewind(file);
    
        int curPos = 0;
        int dataSize = 0;
    
        while (curPos < filesize)
        {
            struct sockaddr_in server_addr; 
            struct hostent *recvr;
            char sendData[BUFSIZ]; // stores message to be sent
            memset(sendData, 0, BUFSIZ);
    
            int byte, i;
            for (i = 0; i < BUFSIZ; i++){
                if ((filesize - curPos) > 0) {
                    byte = fgetc(file);
                    sendData[i] = byte;
                    curPos++;
                    dataSize++;
                }
                else
                    break;
            }
    
            recvr = gethostbyname(address);
            server_addr.sin_family = AF_INET;
            server_addr.sin_port = htons(port);
            server_addr.sin_addr = *((struct in_addr *) recvr->h_addr_list[0]);
            bzero(&(server_addr.sin_zero), 8);
    
            if(DEBUG) {
                char tempData[1201];
                strncpy(tempData, sendData, 1200);
                tempData[1201] ='\0';
                printf("SEND:\n%s\n\n\n", tempData);
            }
    
            if (sendto(sock, sendData, dataSize, 0,
                    (struct sockaddr *) &server_addr, sizeof (struct sockaddr)) < 0)
                err_syserr("Failed to send %d bytes\n", dataSize);
            dataSize = 0;
        }
    
        fclose(file);
    }
    
    int main(int argc, char **argv)
    {
        int fd;
    
        err_setarg0(argv[0]);
        if (argc > 1)
            err_usage("");
        if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
            err_syserr("Failed to open DGRAM socket");
        sendFile(fd, "/etc/passwd", "localhost", 5190);
    
        return(0);
    }
    

    posixver.h

    #ifndef JLSS_ID_POSIXVER_H
    #define JLSS_ID_POSIXVER_H
    
    /*
    ** Include this file before including system headers.  By default, with
    ** C99 support from the compiler, it requests POSIX 2001 support.  With
    ** C89 support only, it requests POSIX 1997 support.  Override the
    ** default behaviour by setting either _XOPEN_SOURCE or _POSIX_C_SOURCE.
    */
    
    /* _XOPEN_SOURCE 700 is loosely equivalent to _POSIX_C_SOURCE 200809L */
    /* _XOPEN_SOURCE 600 is loosely equivalent to _POSIX_C_SOURCE 200112L */
    /* _XOPEN_SOURCE 500 is loosely equivalent to _POSIX_C_SOURCE 199506L */
    
    #if !defined(_XOPEN_SOURCE) && !defined(_POSIX_C_SOURCE)
    #if __STDC_VERSION__ >= 199901L
    #define _XOPEN_SOURCE 600   /* SUS v3, POSIX 1003.1 2004 (POSIX 2001 + Corrigenda) */
    #else
    #define _XOPEN_SOURCE 500   /* SUS v2, POSIX 1003.1 1997 */
    #endif /* __STDC_VERSION__ */
    #endif /* !_XOPEN_SOURCE && !_POSIX_C_SOURCE */
    
    #endif /* JLSS_ID_POSIXVER_H */
    

    stderr.c and stderr.h

    Actually, not standard at all, except in my code. The functions used have the declarations:

    extern void err_setarg0(const char *argv0);
    extern void err_error(const char *format, ...) PRINTFLIKE(1,2) NORETURN();
    extern void err_remark(const char *format, ...) PRINTFLIKE(1,2);
    extern void err_syserr(const char *format, ...) PRINTFLIKE(1,2) NORETURN();
    extern void err_usage(const char *usestr) NORETURN();
    

    The first records the program name. The second reports an error message and exits; the third reports a message and returns; the fourth reports an error message and adds error information from ‘errno’ and ‘strerror()’ if there is any to use; the last reports on how to use the program – in this case, the programs accept no arguments. The full source code (quite large) is available from the IIUG Software site as part of the SQLCMD package available there, and various other programs that I’ve also submitted there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.