Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

About the function inet_ntoa()
I have two programs: one for client and one for server.
In the client.c,
-------------------------------------------------
cin2.sin_family = AF_INET;
inet_aton("130.233.248.203",&(cin2.sin_addr));
cin2.sin_port = htons(PORT);
a1=inet_ntoa(cin2.sin_addr);
printf("The IP value is: %s\n", a1);
//printf("Trying to connect to %s = %s =
%s\n",HOST,inet_ntoa(cin.sin_addr),inet_ntoa(cin2.sin\
_addr));
printf("Trying to connect to %s = %s =
%s\n",HOST,inet_ntoa(cin.sin_addr),a1);
----
a1=130.233.248.203. But after the function printf()is called it
changed to a1=130.233.248.198.
-------------------------------------------------------------
My command line argument is : client 2+3
The output is "Server responded with 2 + 3 = 5"
But when I input client 2*3
The output is: "client: No match."
What is wrong?
--------------------------------------------------------------
Please help me and thanks,

/*for client.c*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>

#define PORT 0x1234           /* define a port number for the service
*/
#define HOST "idefix.hut.fi"
#define bufsize 20


void main (argc,argv)

int argc;
char *argv[];

{ struct sockaddr_in cin,cin2;
struct hostent *hp;
char buffer[bufsize];
char *a1;
int sd;

/* Either by hostname ........ lookup in DNS */

if ((hp = gethostbyname(HOST)) == NULL)
{
perror("gethostbyname: ");
exit(1);
}

memset(&cin,0,sizeof(cin));

cin.sin_family = AF_INET;
cin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
cin.sin_port = htons(PORT);
/* Or by IP numerical address ........ */

memset(&cin2,0,sizeof(cin2));

cin2.sin_family = AF_INET;
inet_aton("130.233.248.203",&(cin2.sin_addr));
cin2.sin_port = htons(PORT);
a1=inet_ntoa(cin2.sin_addr);
printf("The IP value is: %s\n", a1);
//printf("Trying to connect to %s = %s =
%s\n",HOST,inet_ntoa(cin.sin_addr),inet_ntoa(cin2.sin\
_addr));
printf("Trying to connect to %s = %s =
%s\n",HOST,inet_ntoa(cin.sin_addr),a1);
if ((sd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("socket");
exit(1);
}

if (connect(sd,(struct sockaddr *)&cin,sizeof(cin)) == -1)
{
perror("connect");
exit(1);
}
memset(buffer, 0, sizeof(buffer));
sprintf(buffer,"%s",argv[1]);//now we use only two arguments
//sprintf(buffer,"%s + %s",argv[1],argv[3]);
printf("the command line argv[3] is %s:\n", argv[3]);
printf("The input value is : %s\n",buffer);
if (send(sd,buffer,strlen(buffer),0) == -1)
{
perror ("send");
exit(1);
}

if (recv(sd,buffer,bufsize,0) == -1)
{
perror("recv");
exit (1);
}

printf ("Server responded with %s\n",buffer);

close (sd);
}

/*server.c*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>

#define PORT 0x1234
#define bufsize 20
#define queuesize 5
#define true 1
#define false 0


int DoService(char *);

void main ()

{ struct sockaddr_in cin;
struct sockaddr_in sin;
struct hostent *hp;
char buffer[bufsize];
int sd, sd_client, addrlen;

memset(&sin,0,sizeof(sin));       /* Another way to zero memory */
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;          /* Broadcast address */
sin.sin_port = htons(PORT);

if ((sd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("socket");
exit(1);
}
if (bind(sd,(struct sockaddr *)&sin,sizeof(sin)) == -1)  /* Must have
this on server */
{
perror("bind");
exit(1);
}

if (listen(sd,queuesize) == -1)
{
perror("listen");
exit(1);
}

while (true)
{
addrlen=sizeof(struct sockaddr_in);
if ((sd_client = accept(sd,(struct sockaddr *)&cin,&addrlen)) ==
-1)
//if ((sd_client = accept(sd,(struct sockaddr *)&cin,&addrlen))
== -1)
{
printf("the value of sd_client= %d\n",sd_client);
perror("accept");
exit(1);
}

printf("server: got connection from %s\n",
inet_ntoa(cin.sin_addr));
memset(buffer,0,sizeof(buffer));
printf("The former value is: %s\n", buffer);
if  (recv(sd_client,buffer,sizeof(buffer),0)
 == -1)
//printf("The received value is : %s\n", buffer);
{
perror("recv");
exit(1);
}
printf("The received value is : %s\n", buffer);

if (!DoService(buffer)){

break;
}

if  (send(sd_client,buffer,strlen(buffer),0)
 == -1)
{
perror("send");
exit(1);
}

close (sd_client);
}

close (sd);
printf("Server closing down...\n");
}

int DoService(buffer)

char *buffer;

/* This is the protocol section. Here we must */
/* check that the incoming data are sensible  */

{ int a=0,b=0;
char c;

printf("Received: %s\n",buffer);
// memset(buffer, 0,sizeof(buffer));
sscanf(buffer,"%d%c%d\n",&a,&c,&b);

if (a > 0 && b> 0)
{
switch(c){
case '+':
sprintf(buffer,"%d + %d = %d",a,b,a+b);
return true;
case '-':
sprintf(buffer,"%d - %d = %d",a,b,a-b);
return true;
case '*':
sprintf(buffer,"%d * %d = %d",a,b,a*b);
return true;
case '/':
sprintf(buffer,"%d / %d = %d",a,b,a/b);
return true;
default:
return true;
}
}
else
{
if (strncmp("halt",buffer,4) == 0)
{
sprintf(buffer,"Server closing down!");
return false;
}
else
{
sprintf(buffer,"Invalid protocol");
return true;
}
}
}

Report this thread to moderator Post Follow-up to this message
Old Post
learning_C++
09-30-04 01:07 AM


Re: About the function inet_ntoa()
learning_c@hotmail.com (learning_C++) writes:

> I have two programs: one for client and one for server.
> In the client.c,
> -------------------------------------------------
> cin2.sin_family = AF_INET;
>  inet_aton("130.233.248.203",&(cin2.sin_addr));
>  cin2.sin_port = htons(PORT);
>  a1=inet_ntoa(cin2.sin_addr);
>  printf("The IP value is: %s\n", a1);
>  //printf("Trying to connect to %s = %s =
> %s\n",HOST,inet_ntoa(cin.sin_addr),inet_ntoa(cin2.sin\
> _addr));
>  printf("Trying to connect to %s = %s =
> %s\n",HOST,inet_ntoa(cin.sin_addr),a1);
> ----
> a1=130.233.248.203. But after the function printf()is called it
> changed to a1=130.233.248.198.

$ man inet_ntoa
[...]
The inet_ntoa() function converts the Internet host address in given in
network  byte  order to a string in standard numbers-and-dots notation.
The string is returned in a statically allocated buffer,  which  subse-
quent calls will overwrite.

> -------------------------------------------------------------
> My command line argument is : client 2+3
> The output is "Server responded with 2 + 3 = 5"
> But when I input client 2*3
> The output is: "client: No match."
> What is wrong?

You are using tcsh, which although by many considered wrong, is your
choice.  Remember the special meaning of * in the shell?  Apparently
you have no files matching the pattern 2*3, in which case tcsh prints
this message.  The solution is to quote the argument, "2*3".  I'm
pretty sure this is covered in the tcsh man page, but I don't have it
installed so I can't check.

You should really get used to reading man pages.  It's often much
quicker than posting a question here and waiting for an answer.

--
Måns Rullgård
mru@mru.ath.cx

Report this thread to moderator Post Follow-up to this message
Old Post
Måns Rullgård
09-30-04 01:07 AM


Re: About the function inet_ntoa()
On Wed, 29 Sep 2004 12:57:21 -0700, learning_C++ wrote:

> I have two programs: one for client and one for server.
> In the client.c,
> -------------------------------------------------
> cin2.sin_family = AF_INET;
>  inet_aton("130.233.248.203",&(cin2.sin_addr));
>  cin2.sin_port = htons(PORT);
>  a1=inet_ntoa(cin2.sin_addr);
>  printf("The IP value is: %s\n", a1);
>  //printf("Trying to connect to %s = %s =
> %s\n",HOST,inet_ntoa(cin.sin_addr),inet_ntoa(cin2.sin\
> _addr));
>  printf("Trying to connect to %s = %s =
> %s\n",HOST,inet_ntoa(cin.sin_addr),a1);
> ----
> a1=130.233.248.203. But after the function printf()is called it
> changed to a1=130.233.248.198.

inet_ntoa is not reentrant.
consider;
a1=inet_ntoa(cin2.sin_addr);
Now a1 points to some internal buffer the inet_ntoa knows about.
later ...
a2=inet_ntoa(cin.sin_addr);
Now the internal buffer is overwritten by this new value
That means a1 still points to the same buffer, which is now changed.

Check if you have the inet_ntoa_r function which allows you to provide
a buffer.
Or copy the string returned from inet_ntoa so subsequent calls doesn't
over write it.


Report this thread to moderator Post Follow-up to this message
Old Post
Nils O. Selåsdal
10-01-04 01:59 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:41 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.