Reputation: 111
This is a structure define by me....
typedef enum
{
TCP = 1,
UDP
}protocol;
typedef enum
{
DLL_Operation = 1,
MT_Operation,
Fork_Operation,
IPC_Operation
}msgc;
struct f
{
int seqNo;
protocol p;
msgc m;
protocol q;
int PayLoadSize;
void (*payload_ptr)();
};
Now am taking the address of a function in function pointer and sending it to client side from server...
This is my server side code..
struct f f2;
if(f2.m == 1)
{
f2.payload_ptr = &DLL;
f2.payload_ptr();
}
else if(f2.m == 2)
{
f2.payload_ptr = &MT;
f2.payload_ptr();
}
else if(f2.m == 3)
{
f2.payload_ptr = &Fork;
f2.payload_ptr();
}
else
{
f2.payload_ptr = &IPC;
f2.payload_ptr();
}
printf("Address is: %d\n",f2.payload_ptr);
if(f2.q == 1)
{
if(write(newsockfd, &f2, sizeof(f2)) < 0)
{
printf("\nERROR writing to socket");
exit(0);
}
close(sockfd);
close(newsockfd);
}
This is my client side code for receiving..
struct f f1;
if(f1.q = 1)
{
/* Reading data sent by server */
n = read(sockfd, &f1, sizeof(f1));
if(n < 0)
{
printf("\nERROR reading socket");
exit(0);
}
printf("Address is: %d\n",f1.payload_ptr);
f1.payload_ptr();
close(sockfd);
}
Now the problem is when i print the address of that pointer... it is printing same on both side.... but when i call that function pointer to execute.... it it giving Segmentation fault
Upvotes: 1
Views: 107
Reputation: 10460
It seems like you have been trying to do Remote Procedure Call. And you have been doing it wrong. Lets begin with Remote Procedure Call
definition.
In distributed computing a remote procedure call (RPC) is when a computer program causes a procedure (subroutine) to execute in another
address space
(commonly on another computer on a shared network), which is coded as if it were a normal (local) procedure call, without the programmer explicitly coding the details for the remote interaction.
As, it is mentioned that in RPC, a computer program executes a procedure in different address space. Two processes (or program
) can have different address space even if they are running over same machine.
For example, consider web browser
and text editor
programs running in your computer. They are different programs (or processes
) and will be having independent and different virtual address spaces. Refering an address (not in shared memory space
) of variable/function of your text editor in your web browser program doesn't make any sense and even that address may not even exit in your web browser application. Accessing such an address can lead to Segmentation Fault.
Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to you"
.
You have been sending an address of function
from server to client. However, both the programs (client
and server
programs) will have completely different and independent virtual address space. A function having a particular address in one program (or process
) will have different address in second program even if same function is defined in both the programs. There wouldn't be any relationship between vitual address space mapping of functions between two different processes.
But i have a possible solution for your problem if you are trying to do Remote Procedure Call
. You can transer a string between your server and client program. The string should specify the name of the function to execute remotely.
You can compare string received to figure out which function to call.
Server Side Program:
#include <stdio.h>
#define TO_STR(x) #x
void func1()
{
printf("func1\n");
}
void func2()
{
printf("func2\n");
}
int main()
{
sendFunctionName(TO_STR(func1));
return 0;
}
Client Side Program:
#include <stdio.h>
#define TO_STR(x) #x
void func1()
{
printf("func1\n");
}
void func2()
{
printf("func2\n");
}
int main()
{
char functionName[1000];
if (receiveFunctionName(functionName) < 0)
return -1;
if (!strcmp(functionName, TO_STR(func1)))
func1();
else if (!strcmp(functionName, TO_STR(func2)))
func2();
else
printf("Undefined Function\n");
return 0;
}
Upvotes: 1