For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > October 2007 > Linux SystemLinux Systems Programming Tutorial









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Linux SystemLinux Systems Programming Tutorial
arsenic

2007-10-16, 7:12 pm

does someone konw Linux Systems Programming Tutorial

(Part 4- Programming with Sockets)
by Chris Brown?


One of the best tutorials i ever read.


I have one problem with a tcp/ip socket exapamle.
first of all it seems not all libarys necesary are #included but after
adding <sting.h> it compiles perfekt.
but when executed a "segmetation falt (core dump)" retournes in the shell..

i could not find wher the mistake is in the code.. and i ame alomost
sure that ate autor can program very well.
so i am by the fault.

i compiled it with gcc under ubuntu both the latest versiones..

does someone know how to fix this?

the code is:

________________________________________
_______________________________________

1 /* Client for square service */
2
3 #include <stdio.h>
4 #include <sys/types.h>
5 #include <sys/socket.h>
6 #include <netinet/in.h>
7 #include <netdb.h>
8
9 int tcp_connect(char *host, char *service)
10 {
11 struct hostent *host_info;
12 struct servent *serv_info;
13 struct sockaddr_in server;
14 int sock;
15
16 /* Create a socket */
17 sock = socket(AF_INET, SOCK_STREAM, 0);
18
19 /* Look up the host's IP address */
20 host_info = gethostbyname(host);
21
22 /* Look up the service's port */
23 serv_info = getservbyname(service, "tcp");
24
25 /* Copy these into a socket address */
26 server.sin_family = AF_INET;
27 memcpy((char *)&server.sin_addr, host_info->h_addr,
host_info->h_length);
28 server.sin_port = serv_info->s_port;
29
30 /* Now make the connection */
31 connect(sock, (struct sockaddr *)&server, sizeof server);
32 return sock;
33 }
34
35 int main(int argc, char *argv[])
36 {
37 int fdnet, x, y;
38
39 /* Connect to service */
40 fdnet = tcp_connect(argv[1], "square");
41 printf("fdnet = %d\n", fdnet);
42
43 x = 5;
44 /* Conduct brief dialogue with server */
45 write(fdnet, &x, sizeof x);
46 read (fdnet, &y, sizeof y);
47
48 printf("%d squared is %d\n", x, y);
49 }


________________________________________
________________________________________
____


thanks

Ralf Becker

2007-10-16, 7:12 pm

arsenic <274417503@web.de> wrote:
> first of all it seems not all libarys necesary are #included but after
> adding <string.h> it compiles perfekt.

<string.h> declares the prototype for memcpy
> but when executed a "segmetation falt (core dump)" retournes in the shell..

This isn't a surprise. Your code does not test if either of the
functions return without error. gethostbyname() and getservbyname() return
NULL if an error occured. Insert the necessary tests and your program
won't SEGFAULT but will print an useful error message [perror()].

Hints: - Pass the servers name as first commandline argument to your client
- service square isn't usually defined in /etc/services
> sure that ate autor can program very well.

???

Regards Ralf
--
Ralf Becker ralf@akk.org
Arbeitskreis Kultur und Kommunikation / Universitaet Karlsruhe
Paulckeplatz 1 76131 Karlsruhe
Tel 0721/96403-22 Fax 0721/608-4019
--------------------
Joachim Schmitz

2007-10-17, 4:23 am

"Ralf Becker" <ralf@akk.org> schrieb im Newsbeitrag
news:ff391o$q06$1@news2.rz.uni-karlsruhe.de...
> arsenic <274417503@web.de> wrote:
> <string.h> declares the prototype for memcpy
> This isn't a surprise. Your code does not test if either of the
> functions return without error. gethostbyname() and getservbyname() return
> NULL if an error occured. Insert the necessary tests and your program
> won't SEGFAULT but will print an useful error message [perror()].

herror(), not perror() as the error number is stored in h_errno, not errno.
herror() is marked obselete in the man-page, but I don't quite understand
why, as this and the also marke obsolete hstrerror() would be the only
functions available to translate a h_errno into a usable error message and
as per the same man-page gethostbyname() sets h_errno and not errno, so the
only alternative would be to write a replacement for them...
getservbyname() doesn't seem to set errno nor h_errno

Bye, Jojo


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com