read() error - Bad Address

G

GVK

Hi,
I'm facing another problem with my network program. I'm writing a basic server
that handles multiple clients. Whenever a client is connected, a new thread is
created to handle it.
The socket descriptor of the client and it's sockaddr_in structure are passed
as arguments to the function that handles the client. Now, the following code is
giving me an error. It's the function that handles the client:
--------------------------------------------------------------------------------
void *
handle_client(void *arg)
{
pthread_detach(pthread_self());
int clientfd,temp;
char *mesg;
struct sockaddr_in client;
sock_arg *func_arg = (sock_arg *)arg;
mesg = (char *)malloc(100 * sizeof(mesg));
/*
sock_arg is my data type.
struct
{
int sockfd; //socket descriptor
struct sockaddr_in server; // info. of client connected
}
*/

clientfd = func_arg->sockfd;
client = func_arg->server;

/* printing welcome message to the client - works fine */
mesg = "welcome to network-sort server\n";
write(clientfd,mesg,strlen(mesg));

/* this block of code gives the error */
if( read(clientfd,mesg,1024) < 0 )
{
printf("read() error in %s: %s\n",__FUNCTION__,strerror(errno));
exit(1);
}

temp = atoi(mesg);
if( temp == S_OK )
printf("server said: OK\n");
else if( temp == S_NOK )
printf("server said: NOT OK\n");
else
printf("unrecognized code - call the sys admin\n");

return;
}

Here's the sample output for a session: ( the above code is in monitor.c )

SERVER:
[project@voors implementation]$ ./monitor
server started on 0.0.0.0 - listening on port 9999
connection from 172.16.14.52:2067
creating a new thread to handle the client
read() error in handle_client: Bad address

CLIENT:
[project@voors project]$ telnet 172.16.14.52 9999
Trying 172.16.14.52...
Connected to 172.16.14.52.
Escape character is '^]'.
welcome to network-sort server
1 /* input */
Connection closed by foreign host.

--------------------------------------------------------------------------------
I've no idea what's going wrong. This is the first time I'm using threads. Any
help is appreciated.

regards,
GVK
 
M

Mark A. Odell

SERVER:
[project@voors implementation]$ ./monitor
server started on 0.0.0.0 - listening on port 9999
connection from 172.16.14.52:2067
creating a new thread to handle the client
read() error in handle_client: Bad address

Since networking and read() are not part of C how can we help you? Did you
try comp.unix.programmer?
 
J

Joerg Schoen

GVK said:
mesg = "welcome to network-sort server\n";
write(clientfd,mesg,strlen(mesg));

/* this block of code gives the error */
if( read(clientfd,mesg,1024) < 0 )

mesg points still to "welcome to network-sort server\n" which
is put by your compiler in READ-ONLY memory. Besides that, there
is not space for 1024 bytes there.

You need something like
mesg = (char *)malloc(1024);

before the read line
{
printf("read() error in %s: %s\n",__FUNCTION__,strerror(errno));
exit(1);
}

Regards, Joerg
 
K

Kenneth Brody

GVK wrote:
[...]
char *mesg; [...]
mesg = "welcome to network-sort server\n";
write(clientfd,mesg,strlen(mesg));

/* this block of code gives the error */
if( read(clientfd,mesg,1024) < 0 )
{
printf("read() error in %s: %s\n",__FUNCTION__,strerror(errno));
exit(1);
} [...]
read() error in handle_client: Bad address
[...]

Two things:

The pointer mesg is pointing to a buffer of only 32 bytes,
containing "welcome to network-sort server\n\0".

This buffer may be in read-only memory.

Neither one of these allows you to store 1024 bytes into it.

--

+---------+----------------------------------+-----------------------------+
| Kenneth | kenbrody at spamcop.net | "The opinions expressed |
| J. | http://www.hvcomputer.com | herein are not necessarily |
| Brody | http://www.fptech.com | those of fP Technologies." |
+---------+----------------------------------+-----------------------------+
 
C

Christopher Benson-Manica

Joerg Schoen said:
mesg points still to "welcome to network-sort server\n" which
is put by your compiler in READ-ONLY memory.

The compiler is certainly likely to store the string literal in
read-only memory, but is not required to. It doesn't change the fact
that attempting to modify a string literal results in undefined
behavior.
You need something like
mesg = (char *)malloc(1024);
^^^^^^^^

Either you've been listening to Trollsdale or are sadly misinformed.
For numerous reasons (rehashed here regularly), casting malloc()'s
return value is a bad idea.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top