How to free the memory and address,value
Server:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <iostream>
#include <netdb.h>
using namespace std;
int oper(int , int ,int );
void error(const char *msg)
{
cout<<msg<<endl;
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno;
int mynetid,mysecid;
int clientid,clientsecid;
int num,ss;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2)
{
cout<<"ERROR, no port provided\n";
exit(1);
}
int yes=1;
if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1)
{
cout<<"setsockopt";
exit(1);
}
if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
cout<<"ERROR on binding";
listen(sockfd,5);
clilen = sizeof(cli_addr);
socckfd = accept(sockfd,(struct sockaddr *) &cli_addr,&clilen);
if (newsockfd < 0)
cout<<"ERROR on accept";
recv(newsockfd,&mynetid,4,0);
clientid=mynetid;
cout<<"First value is :\t"<<clientid<<endl;
recv(newsockfd,&mysecid,4,0);
clientsecid=mysecid;
cout<<"Second value is :\t"<<clientsecid<<endl;
recv(newsockfd,&num,4,0);
ss=num;
int mm;
mm=oper(clientid,clientsecid,ss);
cout<<"Result was sent :\t"<<mm<<endl;
send(newsockfd,&mm,4,0);
//bzero((char *) &serv_addr, sizeof(serv_addr));
close(newsockfd);
close(sockfd);
return 0;
}
int oper(int a,int b,int c)
{
int aaa,cliid,clisecid,opt;
cliid=a;clisecid=b;opt=c;
aaa=0;
switch(opt)
{
case 1:
aaa=cliid+clisecid;
return aaa;
break;
case 2:
aaa=cliid-clisecid;
return aaa;
break;
case 3:
aaa=cliid*clisecid;
return aaa;
break;
case 4:
aaa=cliid/clisecid;
return aaa;
break;
default:
cout<<"bye";
}
return 0;
}