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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:20:05+00:00 2026-06-16T07:20:05+00:00

I am having troubles receiving the string I am sending with a UDP packet

  • 0

I am having troubles receiving the string I am sending with a UDP packet when this string is dynamically created (when i set its value equal to the one of another variable in my code). This problem does not present itself when, differently, I set a constant value to the string when this one is created.
Here is the method i use for sending the string:

- (void) sendUDPMessage {
 int sockfd;
 struct sockaddr_in server_addr;
 char myBroad[]="255.255.255.255";
 struct hostent *hptr = gethostbyname(myBroad);
 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
 bzero(&server_addr, sizeof(struct in_addr));
 server_addr.sin_family = AF_INET;
 server_addr.sin_port = htons(15000);
 server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
 memcpy(&server_addr.sin_addr, hptr->h_addr_list[0], sizeof(struct in_addr));
 int opt = 1;
 setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &opt, sizeof(int));
 NSString *string = [NSString stringWithFormat:@"%@ %@",user.name,user.surname];
 sendto(sockfd, &string, sizeof(string), 0, (struct sockaddr*)&server_addr, sizeof server_addr);
 NSLog(@"message sent : %@",string);
}

And this is the method for receiving the udp packet:

- (void) server {
 int serverfd;
 struct sockaddr_in server_addr;
 struct sockaddr_in client_addr;
 serverfd = socket(AF_INET, SOCK_DGRAM, 0);
 bzero(&server_addr, sizeof(struct sockaddr_in));
 server_addr.sin_family = AF_INET;
 server_addr.sin_port = htons(15000);
 server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
 bind(serverfd, (struct sockaddr *) &server_addr, sizeof(struct sockaddr_in));
 socklen_t clisize = sizeof(struct sockaddr_in);
 bzero(&client_addr, clisize);
 while (1) {
    NSString *string=@"";
    NSLog(@"wainting for messages");
    recvfrom(serverfd, &string, 200, 0, (struct sockaddr *) &sin, &clisize);
    NSLog(@"message received : %@",string);
 }
}

If I define NSString * string = @”hello!”; I can correctly receive the content of the string. The same problem is present when i try to send an NSObject.
What am I missing? Thanks!

  • 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-16T07:20:07+00:00Added an answer on June 16, 2026 at 7:20 am

    If your goal is to use sockets for some actual purpose, you should give GCDAsyncSocket (and related classes) a try.

    If this is just playing around to get some insight into how sockets work (and/or if you want to use this in C projects as well) here is a fixed version of your code:

    - (void) sendUDPMessage {
     int sockfd;
     struct sockaddr_in server_addr;
     char myBroad[]="255.255.255.255";
     struct hostent *hptr = gethostbyname(myBroad);
     sockfd = socket(AF_INET, SOCK_DGRAM, 0);
     bzero(&server_addr, sizeof(struct in_addr));
     server_addr.sin_family = AF_INET;
     server_addr.sin_port = htons(15000);
     server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
     memcpy(&server_addr.sin_addr, hptr->h_addr_list[0], sizeof(struct in_addr));
     int opt = 1;
     setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &opt, sizeof(int));
     NSString *string = [NSString stringWithFormat:@"%@ %@",user.name,user.surname];
    
     // sendto(sockfd, &string, sizeof(string), 0, (struct sockaddr*)&server_addr, sizeof server_addr);
     // get a pointer to the raw string bytes in UTF-8 encoding:
     const char *bytes = string.UTF8String;
     // send the bytes, using strlen() to get the number of bytes in the UTF-8 string
     // Note! this may not be equal to string.length, which gives the number of UTF-8 chars.
     sendto(sockfd, bytes, strlen(bytes), 0, (struct sockaddr*)&server_addr, sizeof server_addr);
    
     NSLog(@"message sent : %@",string);
    }
    

    And this is the method for receiving the udp packet:

    - (void) server {
     int serverfd;
     struct sockaddr_in server_addr;
     struct sockaddr_in client_addr;
     serverfd = socket(AF_INET, SOCK_DGRAM, 0);
     bzero(&server_addr, sizeof(struct sockaddr_in));
     server_addr.sin_family = AF_INET;
     server_addr.sin_port = htons(15000);
     server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
     bind(serverfd, (struct sockaddr *) &server_addr, sizeof(struct sockaddr_in));
     socklen_t clisize = sizeof(struct sockaddr_in);
     bzero(&client_addr, clisize);
    
    while (1) {
        NSLog(@"waiting for messages");
        char buf[1024]; // this is a static buffer to receive raw bytes
        int bytesread = recvfrom(serverfd, buf, 1024, 0, (struct sockaddr *) &sin, &clisize);
        if ( bytesread > 0 )
        {
            NSString *string = [[NSString alloc] initWithBytes:buf length:bytesread encoding:NSUTF8StringEncoding];
            NSLog(@"message received : %@",string);
            // for non-arc projects don't forget to release string
        }
    }
    }
    

    (untested)

    Various problems still exist in this code, this is only usable as a quick demo. E.g. that all return values should be checked, all errors should be handled, you should be aware that calls may block (unless you specifically prevent this), you should receive the entire message before creating to an NSString (e.g. if the received packet is bigger than the buffer; you cannot do this in parts naively because UTF-8 has multibyte characters)

    You have been warned!

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

Sidebar

Related Questions

I am having some trouble dissecting a UDP packet. I am receiving the packets
I am having some trouble figuring out why I am only receiving one reply
im having troubles with adding a class to the last option from foreach, its
I'm having troubles trying to find a solution, if any, to this: public class
I am having trouble with this error I am receiving whenever I run my
I'm having trouble sending and receiving an intent. The relevant code is below... I
I'm having trouble overriding a ModelForm save method. This is the error I'm receiving:
Having troubles here finding the right containers to represent a list of tasks on
Having troubles with items property of connected sortables. What I'm trying to do is
I'm having troubles finding a bad query that's really causing high CPU load due

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.