Unix Programming - Beginner: Need some help with BSD sockets...

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > December 2006 > Beginner: Need some help with BSD sockets...





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 Beginner: Need some help with BSD sockets...
antonioetayo@gmail.com

2006-12-13, 7:22 pm

Heres the code:
http://www.pastebin.co.uk/7303

When I try to compile in debian using g++ I get this error:

network_module_server.cpp: In function `int main()':
network_module_server.cpp:54: error: invalid conversion from `int*' to
`
socklen_t*'
network_module_server.cpp:61: error: parse error before `,' token

I have no idea what the heck is going on. I'm following this tutorial
almost to the letter:
http://beej.us/guide/bgnet/output/html/syscalls.html

Check on the accept() section. And yeah, I know theres a lot possibly
unnecessary comments in the code and whatnot but thats my way of
keeping notes while I learn.

Thanks for any help you can provide.

PS: If you know of a better tutorial/book on the subject, please
enlighten me. This one kinda sucks.

Jens Thoms Toerring

2006-12-13, 7:22 pm

antonioetayo@gmail.com wrote:
> Heres the code:
> http://www.pastebin.co.uk/7303


> When I try to compile in debian using g++ I get this error:


> network_module_server.cpp: In function `int main()':
> network_module_server.cpp:54: error: invalid conversion from `int*' to
> `socklen_t*'


accept(2) takes a pointer to a socklen_t type variable as its third
argument (see the man page) but you pass it a pointer to an int.
Change the type of your variable 'address_length' from type int to
socklen_t and this problem should go away (or cast it to socklen_t
if you're reasonable sure that socklen_t has the same properties as
an int). The tutorial you mention seems to use C, not C++, and C
tends to be a bit more forgiving when it comes to type checking.

> network_module_server.cpp:61: error: parse error before `,' token


The faulty line is

bytes_received = recv(sock_accept, void buffer, 1024, 0);

I would guess the problem is the 'void' in front of buffer.
Was that meant to be a cast like '(void *)'? And there's at
least another problem: 'buffer' is defined as a single int,
but the way you seem to intend to use it it would need to
be an array of (at least) 1024 chars (that's how many bytes
you tell recv() it can accept). If you want to read only a
single int the third argument would have to be 'sizeof buffer'
and the second one '(void *) &buffer'.

Another problem is that you are not guaranteed to get as many
bytes as you are prepared to receive - there's only an upper
limit. That's why you normally have a loop around it to keep
receiving until you have gotten as much as you need. But you
don't test how much you got and instead later print the content
of 'buffer' as if you would have gotten everything you expected.

> PS: If you know of a better tutorial/book on the subject, please
> enlighten me. This one kinda sucks.


Well, there's always W. Richard Stevens "Unix Network Programming",
Vol. I, ISBN 0-13-490012-X, if you want really high quality material;-)

Another comments after a very short look at your code: in line 38
you use

memset(&(my_addr.sin_zero), '\0', 8);

I would make that

memset(&my_addr.sin_zero, 0, sizeof my_addr.sin_zero);

in order to make sure the correct number of bytes gets zeroed
out - on different platforms it may have a value differing from
8. And I would pass it 0 as the second argument because memset(3)
expects an int there.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
antonioetayo@gmail.com

2006-12-16, 7:26 pm

Thanks for the response. It compiled nicely after I followed your
advice. I wonder if it'll actually work once I'm done with the client
>_>


I'll look into that book you mentioned. Thanks again =)
Jens Thoms Toerring wrote:
> antonioetayo@gmail.com wrote:
>
>
>
> accept(2) takes a pointer to a socklen_t type variable as its third
> argument (see the man page) but you pass it a pointer to an int.
> Change the type of your variable 'address_length' from type int to
> socklen_t and this problem should go away (or cast it to socklen_t
> if you're reasonable sure that socklen_t has the same properties as
> an int). The tutorial you mention seems to use C, not C++, and C
> tends to be a bit more forgiving when it comes to type checking.
>
>
> The faulty line is
>
> bytes_received = recv(sock_accept, void buffer, 1024, 0);
>
> I would guess the problem is the 'void' in front of buffer.
> Was that meant to be a cast like '(void *)'? And there's at
> least another problem: 'buffer' is defined as a single int,
> but the way you seem to intend to use it it would need to
> be an array of (at least) 1024 chars (that's how many bytes
> you tell recv() it can accept). If you want to read only a
> single int the third argument would have to be 'sizeof buffer'
> and the second one '(void *) &buffer'.
>
> Another problem is that you are not guaranteed to get as many
> bytes as you are prepared to receive - there's only an upper
> limit. That's why you normally have a loop around it to keep
> receiving until you have gotten as much as you need. But you
> don't test how much you got and instead later print the content
> of 'buffer' as if you would have gotten everything you expected.
>
>
> Well, there's always W. Richard Stevens "Unix Network Programming",
> Vol. I, ISBN 0-13-490012-X, if you want really high quality material;-)
>
> Another comments after a very short look at your code: in line 38
> you use
>
> memset(&(my_addr.sin_zero), '\0', 8);
>
> I would make that
>
> memset(&my_addr.sin_zero, 0, sizeof my_addr.sin_zero);
>
> in order to make sure the correct number of bytes gets zeroed
> out - on different platforms it may have a value differing from
> 8. And I would pass it 0 as the second argument because memset(3)
> expects an int there.
> Regards, Jens
> --
> \ Jens Thoms Toerring ___ jt@toerring.de
> \__________________________ http://toerring.de


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com