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

Recommended Answers

All 3 Replies

What do you exactly mean when you say this:

result for those paramters is shown on agent's own terminal window....where as i want them on manager's window.

In case you meant what I think you did, you are sending the returned request to yourself(agent). Please check your
a) SOCKET
b) clientaddr
c) Does your client connect? As I don't see any listen() function
d) Any sending error. WSAGetLastError() returns the error code which you can reference Here

i have not used any listen function but it works fine when we simple send any message to manager(client). but i want aboove mentioned paramerts from agent(server) for manager(client)

if you see in agent's code from line 90 onwards program is executing these functions and there i used printf thats y i am getting there result on the same window( agent's window) there i need sendto() function that takes the result to manager(client)

thats what i dont know how send to function will be used here or how it will take the result to client side...

other than this communication between both agent and manager was being carried out... because when manager requested any option (LIKE 1,2, OR 3) that value we get on agent (server) window. and server side goes in that case( of the value manager sent) from here onwards i dont understand how to send the result of that particular function back to client..... i know we have to use SENDTO() function but dont know HOW... ??? :(

i hope u understand what i want.... plzz help!!!

Regards!

You can ditch the last 2 optional parameters of sendto() if your implementing a 1-to-1 connection.

sendto(<Connection Socket>,<SendBuffer>,<BufferLen>, 0, 0, 0);

In this case you are better of using send()

send(<Connection Socket>,<SendBuffer>,<BufferLen>, 0);

Please make sure the Connection Socket is a valid one.
Also you haven't specified addr_len inside the client. You are better off using the sizeof operator in the function call itself.
And are you receiving the client_addr right. check the IP of client using inet_ntoa()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.