From: Bill Cunningham on 26 Apr 2010 17:17 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
From: Bill Cunningham on 26 Apr 2010 17:22 Bill Cunningham wrote: HUMmm. Should this code look like this maybe? Notice I passed a point this time. Though send() wants a size_t. > 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
From: Ian Collins on 26 Apr 2010 17:26 On 04/27/10 09:17 AM, Bill Cunningham wrote: > I usually fix my errors but this one I can't trace. A joke, I assume? > const char *msg("hello world\n"); What does that say? -- Ian Collins
From: John Gordon on 26 Apr 2010 17:30 In <4bd602ef$0$12421$bbae4d71(a)news.suddenlink.net> "Bill Cunningham" <nospam(a)nspam.invalid> writes: > const char *msg("hello world\n"); 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 gordon(a)panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies"
From: Bill Cunningham on 26 Apr 2010 17:36 Ian Collins wrote: > On 04/27/10 09:17 AM, Bill Cunningham wrote: >> I usually fix my errors but this one I can't trace. > > A joke, I assume? 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
|
Next
|
Last
Pages: 1 2 Prev: how does a pts for child process get set? Next: Recursion crash in STL on linux |