error

error

Post by Bill Cunni » Wed, 28 Apr 2010 06:17:48



I usually fix my errors but this one I can't trace. This is an attempt
(my first) at sending a string to the echo port and I want it to send it
back to me. I don't know if yahoo has an echo port open but I might be able
to find one somewhere. After a connect() I should use a recv() shouldn't I?
I'm just sending but the code won't compile for some reason it doesn't like
strlen().

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>

struct addrinfo ad, *adp;
int main()
{
int sock, c, a, conn, se;
const char *msg("hello world\n");
memset(&ad, '\0', sizeof(int));
ad.ai_socktype = SOCK_STREAM;
ad.ai_family = AF_INET;
a = getaddrinfo("www.yahoo.com", "7", &ad, &adp);
if (a != 0) {
fprintf(stderr, "Error \%s\n", gai_strerror(a));
exit(1);
}
c = socket(sock, adp->ai_family, adp->ai_socktype);
if (c == -1) {
perror("socket error");
exit(1);
}
conn = connect(sock, adp->ai_addr, adp->ai_addrlen);
if (conn == -1) {
perror("connect error");
exit(1);
}
se = send(sock, msg, strlen(msg), 0);
if (se <= 0) {
perror("send error");
exit(1);
}
return 0;
}

c.c: In function `main':
c.c:12: error: syntax error before string constant
c.c:31: warning: passing arg 1 of `strlen' from incompatible pointer type

Opinions or help?

Bill
 
 
 

error

Post by Bill Cunni » Wed, 28 Apr 2010 06:22:08


HUMmm. Should this code look like this maybe? Notice I passed a point this
time. Though send() wants a size_t.

 
 
 

error

Post by Ian Collin » Wed, 28 Apr 2010 06:26:20


A joke, I assume?


What does that say?

--
Ian Collins
 
 
 

error

Post by John Gordo » Wed, 28 Apr 2010 06:30:16

In <4bd602ef$0$12421$ XXXX@XXXXX.COM > "Bill Cunningham" < XXXX@XXXXX.COM > writes:


That's not how to declare a string constant. Try this instead:

const char *msg = "hello world\n";

--
John Gordon A is for Amy, who fell down the stairs
XXXX@XXXXX.COM B is for Basil, *** ed by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
 
 
 

error

Post by Bill Cunni » Wed, 28 Apr 2010 06:36:15


No I've been working alot with perror() and puts() lately. I didn't
think about the parenthesis. <blush> This is what I hate about asking for
help. It always turns out to be something embarrassing.

Bill
 
 
 

error

Post by Keith Thom » Fri, 30 Apr 2010 11:23:29

John Gordon < XXXX@XXXXX.COM > writes:

Both declarations are valid in C++; only the second is valid in C.

(And he needs #include <stdlib.h> for exit().)

--
Keith Thompson (The_Other_Keith) XXXX@XXXXX.COM < http://www.yqcomputer.com/ ~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"