About the function inet_ntoa()
From: learning_C++ (learning_c_at_hotmail.com)
Date: 09/29/04
- Next message: Dragan Cvetkovic: "Re: Get Unix Groups"
- Previous message: Jens.Toerring_at_physik.fu-berlin.de: "Re: problem with implementing pipe"
- Next in thread: Måns Rullgård: "Re: About the function inet_ntoa()"
- Reply: Måns Rullgård: "Re: About the function inet_ntoa()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 29 Sep 2004 12:57:21 -0700
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;
}
}
}
- Next message: Dragan Cvetkovic: "Re: Get Unix Groups"
- Previous message: Jens.Toerring_at_physik.fu-berlin.de: "Re: problem with implementing pipe"
- Next in thread: Måns Rullgård: "Re: About the function inet_ntoa()"
- Reply: Måns Rullgård: "Re: About the function inet_ntoa()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
Loading