Hello mark. thank you for your reply. i have actually asked
this question in a C forum, and was told (rudely!) i should ask
these questions in a more appropriate forum... hmm.. sorry if i
am a bit lost. but anyways i really appreciate it that you had
the decency to help out. i hope you do not mind if i ask you
another question. i have changed my code to be able to read the
first message from the server. then i send my message to the
server. when i try to read the server's reply, using read(),
the program hangs forever.
The new code is below and the faulty read() is way below and is
commented.
If there is a more suitable place to post these messages in the
future, please advise me of the whereabouts. thanx again.
Fouad
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#define closesocket close
#define PROTOPORT 143 /* default protocol port
number */
extern int errno;
char localhost[] = "localhost"; /* default host
name */
main(int argc, char *argv[])
{
struct hostent *ptrh; /* pointer to a host table
entry */
struct protoent *ptrp; /* point to a protocol table
entry */
struct sockaddr_in sad; /* structure to hold server's
address */
int sd; /* socket
descriptor */
int port; /* protocol port
number */
char *host; /* pointer to host
name */
int n; /* number of characters
read */
char buf[1000]; /* buffer for data from the
server */
char *pp = " ";
char b2[48];
memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr
structure */
sad.sin_family = AF_INET; /* set family to
Internet */
/* Check command-line argument for protocol port and
extract */
/* port number if on is specified. Otherwise, use the
default */
/* port value biven by constant
PROTOPORT */
if (argc > 2) port = atoi(argv[2]);
else port = PROTOPORT;
if (port > 0) sad.sin_port = htons((u_short)port);
else
{ fprintf( stderr,"bad port number %s\n", argv[2]);
exit(1);
}
if (argc > 1 ) host = argv[1];
else host = localhost;
ptrh = gethostbyname(host);
if( ((char *)ptrh) == NULL)
{ fprintf( stderr, "invalid host: %s\n", host);
exit(1);
}
memcpy(&sad.sin_addr, ptrh->h_addr, ptrh->h_length);
if ( ((int)(ptrp = getprotobyname("tcp"))) == 0)
{ fprintf( stderr, "cannot map \"tcp\" to protocol
number\n");
exit(1);
}
sd = socket(PF_INET, SOCK_STREAM, ptrp->p_proto);
if (sd < 0)
{ fprintf( stderr, "socket creation failed\n");
exit(1);
}
if (connect(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0)
{ fprintf( stderr, "connect failed\n");
exit(1);
}
n = read(sd, buf, sizeof(buf), 0);
fprintf( stderr, "SERVER: %s", buf);
while(n > 0)
{
//scanf("%s", &pp);
// fprintf(stderr, "Sending %s.", pp);
n = write (sd, "log login [EMAIL PROTECTED] sadf",
28);
if (n < 0)
{
perror("While calling write()");
}
printf("\nWaiting to read\n");
// the read that hangs forever.
n = read(sd, buf, sizeof(buf), 0);
printf("N is %d", n);
fprintf( stderr, "SERVER: %s\n", buf);
n = 1;
}
printf("Closing and exiting!\n");
closesocket(sd);
exit(0);
}
________________________________________________
Get your own "800" number
Voicemail, fax, email, and a lot more
http://www.ureach.com/reg/tag