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

  • Home
  • SEARCH
  • 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 3434286
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:40:47+00:00 2026-05-18T07:40:47+00:00

/* ** talker.c — a datagram client demo */ #include <stdio.h> #include <stdlib.h> #include

  • 0
    /*
** talker.c -- a datagram "client" demo
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fstream>
#include <iostream>
#include <string>


using namespace std ;
#define SERVERPORT "3200775" // the port users will be connecting to

int main()
{ string s;

ifstream f1 ("queries1.txt");
if (f1.is_open()) 

{
while (!f1.eof()) 
{
getline(f1,s);
cout<<s<<endl;
        }

}
    int sockfd;
    //char ch [] = "hello";

struct addrinfo hints, *servinfo, *p;
int rv;
int numbytes;

// if (argc != 3) {
// fprintf(stderr,"usage: talker hostname message\n");
// exit(1);
//}

memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;

if ((rv = getaddrinfo("nunki.usc.edu", SERVERPORT, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}

// loop through all the results and make a socket
for(p = servinfo; p != NULL; p = p->ai_next) 
{
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) 
{
perror("talker: socket");
continue;
}

break;
}

if (p == NULL) 
{
fprintf(stderr, "talker: failed to bind socket\n");
return 2;
}

//if ((numbytes = sendto(sockfd,ch, strlen(ch), 0,
// p->ai_addr, p->ai_addrlen)) == -1) {
//perror("talker: sendto");
//exit(1);

//for (f=0 ;f<15; f++ )
 //   {

    char* mess = malloc(20*sizeof(char));
    sprintf(mess,s);
         if ((numbytes = sendto(sockfd,mess, s.length(), 0, p->ai_addr, p->ai_addrlen)) == -1) 
{ cout<<s; 
  perror("talker: sendto");
  exit(1);

} 
printf("talker: sent %d bytes to \n", numbytes);
cout<<endl;
//}

freeaddrinfo(servinfo);

//printf("talker: sent %d bytes to \n", numbytes);

close(sockfd);
return 0;
}

Sorry for the sloppy way of coding . I am getting errors in this . How do i debug it ?

The errors are these

test.cpp:84: error: invalid conversion from ‘void*’ to ‘char*’
test.cpp:85: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘2’ to ‘int sprintf(char*, const char*, ...)’
  • 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-18T07:40:48+00:00Added an answer on May 18, 2026 at 7:40 am

    First corrections to your code:

    char* mess = malloc(20*sizeof(char));
    sprintf(mess,s);
    if ((numbytes = sendto(sockfd,mess, s.length(), 0, p->ai_addr, p->ai_addrlen)) == -1) 
        // ...
    
    1. There’s no need to dynamically allocated such a little buffer, whose size is predictable at compile-time.
    2. s is a string object, not a pointer to char*.
    3. Even if it was a pointer to char*: it’s very unsuitable to use sprintf, since the source string may contain format codes (‘%’). Imagine what happens if it contains ‘%s’.
    4. How do you know the string won’t be longer than 19 characters? You can’t know this at compile time.
    5. Anyway, sprintf should be used if you want to do the string formatting. There’s no need to use it if you just need the string as-is.

    This shame list can be continued. Simply speaking you should rewrite it this way:

    if ((numbytes = sendto(sockfd, (char*) s.c_str(), s.length(), 0, p->ai_addr, p->ai_addrlen)) == -1) 
        // ...
    

    Regarding your specific problem. If we assume the actual problem is not from the mentioned list – there’s usually complementary sockets functions that can give you the reach error information.

    For instance, on Windows there’s a WSAGetLastError function that can be used immediately after you get the error.

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

Sidebar

Related Questions

I am writing a server program in C wherein every time a client connects,
Suppose you had 2 classes, Listener and Talker. Talker has an event Talking, and
Currently i am using ODP.Net.Is Oracle Instant client something to replace this or an
I have a function that should be passed a string with two numbers (as
I talked to a few friends who say that Drupal is amazing, and it
I talked to a friend of mine and he told me that it's possible
I talked to the Team Lead at Snap-On Tools once, and she told they
I talked my team into turning on compiler warnings again. Some how all warnings
So this post talked about how to actually implement url rewriting in an ASP.NET
Has anyone successfully talked profibus from a .NET application? If you did, what device/card

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.