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 7878293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:37:05+00:00 2026-06-03T03:37:05+00:00

GOAL: I’m making a UDP socket to send and receive data. I am testing

  • 0

GOAL: I’m making a UDP socket to send and receive data.
I am testing this on my laptop, so I have a server running on the background that listens to incoming messages and echoing this back.

PROBLEM: I am seeing that the server receives one string, and when it echoes back, the client reads the string 2 times instead of ONE and adds gibberish.How to solve this?

Output from the code is: HelloHello09[btw i als get some questionsmarks that are upside down in front of the 09 and behind it, but I cannot paste it, lolz]

Code:

#define BUFLEN 5
#define PORT 12345

#import <Foundation/Foundation.h>

#define srvr_IP "127.0.0.1"

void errorSig(char *); 

int main (int argc, const char * argv[])
{
int sockSend = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in si_other;

char buf[BUFLEN] = "Hello";
char bufrec[BUFLEN]; 

@autoreleasepool {

    si_other.sin_family = AF_INET; 
    si_other.sin_port = htons(PORT);
    inet_pton(AF_INET, srvr_IP, &si_other.sin_addr); 
    memset(&si_other.sin_zero, 0, sizeof(si_other.sin_zero));

    int size = sizeof(si_other); 
    sendto(sockSend, buf, BUFLEN, 0,
           (struct sockaddr *)&si_other, size);

    recvfrom(sockSend, bufrec, BUFLEN, 0, (struct sockaddr *)&si_other, (unsigned int*)&size); 
    NSString *test = [[NSString alloc]initWithUTF8String:bufrec];
    NSLog(@" data is: %@", test); 

    close(sockSend); 
    }
return 0;
}
  • 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-06-03T03:37:06+00:00Added an answer on June 3, 2026 at 3:37 am

    I’ve just seen that you did in fact define BUFLEN as 5 (sorry I missed that when I wrote my comment above). As I suspected, you have a NULL termination issue. The length of a string in C is one more than the number of characters you want to store, to make room for the NULL terminator indicating the end of the string.

    Change your BUFLEN definition to 6 and you should find it works much better.

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

Sidebar

Related Questions

Goal: Show data from server in wxPython GUI on client Newcomer to Twisted. I
Goal: retrieve an element of data from within a PHP object by number. This
goal: I have the string 1234432144 I want to only replace the first 2
Goal : After user saves data to my mysql DB, a JSON teaser of
Goal: json_encode a PHP object that has private properties Send the encoded object as
Goal: Get values which are unique in $resultSecond So I have two objects. $result
Goal: Rolling/Running total for all statements at the end of each month. Code: select
Goal I have a generic class GenericClass<T> and I want to pool instances. I'm
Goal: Display customized information based on end user's position. If you have 2 user:
Goal : I have a view which displays multiple nodes in full view including

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.