hii
i have a server client prog in which server is my 'agent' and client is 'manager'
following is the code for both agent and manager. i want my agent to send what manager is requesting. but it is not working the way i want. the paramters which manager is requesting, agent has to send to manager but in this program result for those paramters is shown on agent's own terminal window....where as i want them on manager's window.
secondly, there result is not correct.. means it is giving ' 0 K ram and 0 no of process.... :(
plz someone check this code and correct me where i am making mistake.... i working on this from past 6,7 hrs but could not do anything....
//code for MANAGER
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/sysinfo.h>
void get();
void snmp();
void getnext();
int sock,bytes_read,addr_len;
struct sockaddr_in server_addr;
struct hostent *host;
char send_data[1024];
int switch_val;
char recv_data[1024];int count;
int main()
{
host= (struct hostent *) gethostbyname((char *)"127.0.0.1");
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
perror("socket");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(7734);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8);
while (1)
{
snmp();
// code to recieve msgs from client
bytes_read = recvfrom(sock,recv_data,1024,0,
(struct sockaddr *)&server_addr,&addr_len);
recv_data[bytes_read] = '\0';
printf("\n(%s , %d) said : ",inet_ntoa(server_addr.sin_addr),ntohs(server_addr.sin_port));
printf("%s", recv_data);
fflush(stdout);
}
}
void snmp()
{
printf(" start sending requests\n");
printf(" select '1' to send get request\n");
scanf( "%d", &switch_val);
//printf("%s\n", switch_val);
switch(switch_val)
{
case 1:
count=0;
if (count==0)
{
get();
}
count++;
break;
default:
printf("OOooPPppssss");
break;
}
}
////// GET FUNCTION //////
void get()
{
printf("\t\t GET FUNCTION\n");
printf(" SEND YOUR REQUEST \n");
printf(" 1. \tSYSTEMS UP TIME\n");
printf(" 2. \tNO OF PROCESSES RUNNING\n");
printf(" 3. \tTOTAL AND FREE RAM\n");
printf(" 4. \tSHARED AND BUFFERED RAM\n");
printf(" 5. \tSWAPPED SPACE");
scanf("%s", &send_data);
sendto(sock, send_data, strlen(send_data), 0,
(struct sockaddr *)&server_addr, sizeof(struct sockaddr));
// code to recieve msgs from manager
bytes_read = recvfrom(sock,recv_data,1024,0,
(struct sockaddr *)&server_addr,&addr_len);
recv_data[bytes_read] = '\0';
printf("\n(%s , %d) said : ",inet_ntoa(server_addr.sin_addr),ntohs(server_addr.sin_port));
printf("%s", recv_data);
fflush(stdout);
}// end of get function
//CODE FOR AGENT
void snmpget();
void systm_time();
void no_of_process();
void sb_ram();
void tf_ram();
void swapd_space();
int sock;char send_data[1024];
int addr_len, bytes_read;
char recv_data[1024];
struct sysinfo sys_info;
struct sockaddr_in server_addr , client_addr;
int days, hours, mins;
int main()
{
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("Socket");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(7734);
bzero(&(server_addr.sin_zero),8);
if (bind(sock,(struct sockaddr *)&server_addr,
sizeof(struct sockaddr)) == -1)
{
perror("Bind");
exit(1);
}
addr_len = sizeof(struct sockaddr);
printf("\t Waiting for manager to requst something");
fflush(stdout);
while (1)
{
snmpget();
sendto(sock, send_data, strlen(send_data), 0,
(struct sockaddr *)&client_addr, sizeof(struct sockaddr));
}
return 0;
}
void snmpget()
{
// code to recieve msgs from client
bytes_read = recvfrom(sock,recv_data,1024,0,
(struct sockaddr *)&client_addr, &addr_len);
recv_data[bytes_read] = '\0';
printf("\n(%s , %d) requested : \n",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
printf("%s", &recv_data);
fflush(stdout);
int i=int(recv_data);
switch(i)
{
case 1:
systm_time();
break;
case 2:
no_of_process();
break;
case 3:
tf_ram();
break;
case 4:
sb_ram();
break;
case 5:
swapd_space();
break;
default:
printf("\tINCORRECT VALUE");
break;
}
}
void systm_time()
{
if(sysinfo(&sys_info) != 0)
perror("sysinfo");
// Uptime
days = sys_info.uptime / 86400;
hours = (sys_info.uptime / 3600) - (days * 24);
mins = (sys_info.uptime / 60) - (days * 1440) - (hours * 60);
printf("Uptime: %ddays, %dhours, %dminutes, %ldseconds\n",
days, hours, mins, sys_info.uptime % 60);
}
void no_of_process()
{
// Number of processes currently running.
printf("Number of processes: %d\n", sys_info.procs);
}
void tf_ram()
{
// Total and free ram.
printf("Total Ram: %ldk\tFree: %ldk\n", sys_info.totalram / 1024,
sys_info.freeram / 1024);
}
void sb_ram()
{
// Shared and buffered ram.
printf("Shared Ram: %ldk\n", sys_info.sharedram / 1024);
printf("Buffered Ram: %ldk\n", sys_info.bufferram / 1024);
}
void swapd_space()
{ // Swap space
printf("Total Swap: %ldk\tFree: %ldk\n", sys_info.totalswap / 1024,
sys_info.freeswap / 1024);
}
// end of function snmpget