Sendto() function giving errors . How to debug them?
/*
** 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 wi开发者_如何转开发ll 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*, ...)’
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)
// ...
- There's no need to dynamically allocated such a little buffer, whose size is predictable at compile-time.
s
is astring
object, not a pointer tochar*
.- Even if it was a pointer to
char*
: it's very unsuitable to usesprintf
, since the source string may contain format codes ('%'). Imagine what happens if it contains '%s'. - How do you know the string won't be longer than 19 characters? You can't know this at compile time.
- 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.
Your port is way out of range. Port numbers for TCP and UDP are 16-bit integers, i.e. the max is 65535.
Change those lines to:
char *mess = (char *)malloc (20 * sizeof (char));
sprintf(mess, s.c_str());
The first one needs an explicit cast, and in the second line you have to explicitly produce a C-style string from a C++ string.
EDIT:
Keep in mind that the c_str() method of std::string only provides a pointer to an internal structure of the object. If you destroy the string, that pointer is no longer valid, so take care to use strdup() or similar if necessary.
EDIT2:
If you really want to do it properly, you should really use strdup() instead of sprintf():
mess = strdup(s.c_str());
Don't forget to free() the mess pointer when you are done with it.
精彩评论