1

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

Milan Shah
  • 111
  • 1
  • 10
  • Can you please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us? What is `DLL`? What is `MT`? Etc. And where do the crash happens? Please use a debugger to locate it within your code. Lastly, if you define nice enumerations, why not use them instead of [magic numbers](https://en.wikipedia.org/wiki/Magic_number_(programming))? – Some programmer dude Oct 28 '16 at 09:33
  • 7
    So you are sending the *address* of a function to another machine and expecting it to be a valid call? – Weather Vane Oct 28 '16 at 09:34
  • 2
    Another thing, on systems with virtual memory or where programs can be relocated, you do know that memory addresses (which is what pointers are) are *per process*. Sending pointers to other processes will not work very well. – Some programmer dude Oct 28 '16 at 09:35
  • DLL means doubly link list and MT means multithreading operations.... this are the functions made in differenct file and included in server side...... i want to take that function and want to execute on client side.. – Milan Shah Oct 28 '16 at 09:40
  • 1
    Are you looking for some way to do [RPC (Remote Procedure Call)](https://en.wikipedia.org/wiki/Remote_procedure_call)? Then that's not the way to do it. Use an existing RPC framework. – Some programmer dude Oct 28 '16 at 09:44
  • 3
    This is probably an [XY Problem](http://xyproblem.info/). – Jabberwocky Oct 28 '16 at 09:45
  • [Shared memory](https://en.wikipedia.org/wiki/Shared_memory) can help. – LPs Oct 28 '16 at 09:45

1 Answers1

1

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;
}
Community
  • 1
  • 1
abhiarora
  • 9,743
  • 5
  • 32
  • 57
  • if we add functions to client side also then we can easily call them.....functions only need to add at server side – Milan Shah Oct 28 '16 at 12:17
  • 1
    If you want to achieve it in C, then you have to transfer the function definition to client side (Source file having function definition) and then execute them at server side after compiling and forking a new process for function execution. – abhiarora Oct 28 '16 at 13:05
  • 1
    Because you were sending the address of a function from server to client. And any function didn't exist in your client program's virtual address space or you may not have appropriate right access for that address. That's why it is showing segmentation fault. – abhiarora Oct 29 '16 at 09:18
  • it is working well .... the function is printing but after printing the function.... it is giving segmentation fault... – Milan Shah Oct 29 '16 at 10:01