Subject: Bad address

From: Fouad Kassab <[email-masked]>
Date: Wed, 2 Oct 2002 22:10:23 -0400

Hello,
I have just started in a process of writing a client that will 
talk to an IMAP4 server. The below code is giving me an error 
(Bad address) when i call read() for the first time.
I am not sure why. i can use telnet easily to talk to the imap4 
server "aineldelb.com" at the port 143.
I can not do it with the code below. I am using TCP for the 
connection. The code below is fairly simple and straight 
forward. All i want to do with it really is just to get the 
welcome message from the server. the rest of it is not 
important. you will find the read() under a line 
of //////////////////////////////s

Thank you for your help in advance.

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#define BUF_LEN 48

main(int argc, char *argv[])
{

 int csd;
 struct sockaddr_in server;
 struct hostent *server_host;

 int server_len;
 int string_size;

 short server_port;
 int out_cnt, in_cnt;
 char client_send_string[BUF_LEN];
 char *server_reversed_string = " ";
 int ret_code;
 int  terminate = 0;

 char *host_name = "aineldelb.com";
 server_host = gethostbyname(host_name);
 server_port = 143;

 csd = socket(PF_INET, SOCK_STREAM, 0);
 if(csd < 0){
  perror("While calling socket()");
  exit(1);

 }

 server.sin_family = AF_INET;
 memcpy(&server.sin_addr, server_host->h_addr_list[0],
server_host ->h_length);
 server.sin_port = htons(server_port);
 if(connect(csd, (void *)&server, sizeof(server)) == -1)
 {
  perror("While calling connect()");
  exit(1);
 }
////////////////////////// THIS IS THE READ THAT IS GIVING ME 
PROBLEMS
 in_cnt = read(csd, server_reversed_string, BUF_LEN);
 if(in_cnt < 0)
 {
  perror("While calling read()");
  exit(1);

 }

}

________________________________________________
Get your own "800" number
Voicemail, fax, email, and a lot more
http://www.ureach.com/reg/tag
-- 
------------------------------------------------------------------
 For information about this mailing list, and its archives, see: 
 http://www.washington.edu/imap/c-client-list.html
------------------------------------------------------------------

---

From: Mark Crispin <[email-masked]>
Date: Wed, 2 Oct 2002 19:17:27 -0700 (PDT)

The problem is not an IMAP problem, or a c-client problem.  It is a problem in
your C code.  It would be better if you ask such questions in a C programming
forum.

The following clues may help you:

The reason why you got a "Bad address" error is that you tried to write into a
write-protected area of memory.  The read() call reads from the network, but
writes into memory.  One of the arguments to the read() call in your program
is incorrect, and is the cause of the attempt to write into a write-protected
area of memory.

C does not dynamically allocate space for strings ala LISP or BASIC.  In C,
you must do so yourself.  Also, the concept of "pointers" is very important in
C.