#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
/*the port number */
#define PORT 6100
/* the struct used for sending the
operands*/
struct {
int operand1;
int operand2;
} operands;
/* the structure used for storing the
results*/
struct {
int sum;
int mult;
} results;
main( int n, char *v[])
{
int length,recv;
int sock;
struct sockaddr_in to;
struct hostent *hp;
/*testing on the right number of parameters*/
if( n<3){
printf("incorrect parameters number");
exit(1);
}
/*getting the address of the server*/
if((hp=gethostbyname(v[1]))==NULL){
perror("host name error");
exit(2);
}
/* allocating the memory for the structure to*/
bzero((char*)&to,sizeof(to));
/*creation of the socket*/
sock = socket(AF_INET,SOCK_DGRAM,0);
if(sock < 0){
perror("can not create the socket");
exit(3);
}
/*filling
the structure to*/
to.sin_port
= htons(PORT);
to.sin_family =
AF_INET;
bcopy(hp->h_addr,&to.sin_addr,hp->h_length);
/*casting the operands to integers*/
operands.operand1 = atoi(v[2]);
operands.operand2 = atoi(v[3]);
/*sending the structure operands to the server*/
if(sendto(sock,(char*)&operands ,8, 0, (struct sockaddr
*)&to,sizeof(to))==-1)
{
perror("sendto");
exit(4);
}
length = sizeof(to);
/*allocating memory for the structure results and receving the results*/
bzero((char *)&results,sizeof(results));
recv = recvfrom(sock,(char*)&results,8,0,(struct
sockaddr*)&to,&length);
/*printing the results*/
printf("***********************************************\n");
printf("the sum is:%d\nthe multiplication is:
%d\n",results.sum, results.mult);
printf("***********************************************\n");
}