Unix Programming - Socket Accept Problems

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > January 2004 > Socket Accept Problems





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 Socket Accept Problems
Matt Churchyard

2004-01-23, 5:21 pm

Hi,

I am having problems accepting connections on a socket.
The accept call returns a 'Bad Address' error which as far as i can see
means "The addr parameter is not in a writable part of the user address
space."

I cannot understand why this could be as the addr structure is allocated
using
malloc which (if i am thinking correctly?) will not return an address
outside the user range.

Heres the code thats causing the problem

===============================
// create space for client address
client.address = (struct sockaddr_in *)malloc(sizeof(struct
sockaddr_in));

if( client.address == NULL )
{
syslog(LOG_ERR, "Couldn't allocate memory for client address
structure");
return;
}

// wait for clients
NEXTCLIENT:
while((client.socket = accept(server.socket, (struct sockaddr
*)client.address, &client_len)))
{
// check socket
if( client.socket < 0 )
{
perror("Socket Failed");

goto NEXTCLIENT;
}
===============================

Any help is greatly appreciated

Thanks,
Matt

--

Regards,
Matt Churchyard
_______________________________
Project Development Manager
Userve Internet
matt@userve.net
http://www.userve.net/


Nils O. =?iso-8859-1?Q?Sel=E5sdal?=

2004-01-23, 5:21 pm

In article <3fba0eda$1@news.userve.net>, Matt Churchyard wrote:
quote:

> Hi,
>
> I am having problems accepting connections on a socket.
> The accept call returns a 'Bad Address' error which as far as i can see
> means "The addr parameter is not in a writable part of the user address
> space."


Which might happen if you don't initialize the client_len
quote:

>
> I cannot understand why this could be as the addr structure is allocated
> using
> malloc which (if i am thinking correctly?) will not return an address
> outside the user range.
>
> Heres the code thats causing the problem
>
>===============================
> // create space for client address
> client.address = (struct sockaddr_in *)malloc(sizeof(struct
> sockaddr_in));
>
> if( client.address == NULL )
> {
> syslog(LOG_ERR, "Couldn't allocate memory for client address
> structure");
> return;
> }
>
> // wait for clients
> NEXTCLIENT:
> while((client.socket = accept(server.socket, (struct sockaddr
> *)client.address, &client_len)))
> {
> // check socket
> if( client.socket < 0 )
> {
> perror("Socket Failed");
>
> goto NEXTCLIENT;
> }


Is the server.socket a valid socket, on which you've successfully called
bind(2) and listen(2) ?
And do you set client_len to the correct (sizeof(struct sockaddr_in))value
on each iteration of the loop ?



--
Vennlig hilsen/Best Regards
Nils Olav Selåsdal <NOS at Utel.no>
System Engineer
UtelSystems a/s

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com