Passing Functions through Socket
Can we pass Address of a Function say &Disp() into a Client Socket and Read that address in the Server Socket and Run the Function....Or what the other Way of doing it..
Consider /*Client.c*/
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#define PORT 5000
void Disp(); // Function Prototype
main()
{
int Sid, a=10;
struct sockaddr_in Cli;
Sid=Socket(AF_INET,SOCK_STREAM,0);
Cli.sin_family=AF_INET;
Cli.sin_port=htons(port);
Cli.sin_addr.s_addr=inet_addr("192.160.10.10.");
connect(Sid,(struct sockaddr *)&Cli,sizeof(Cli));
send(Sid,&a,sizeof(a),0);
//In caseof structure passing the address of Structure works
//Can i pass the address of Function Disp and invoke that in Server
//send(Sid,&Disp,sizeof(Disp),0);
close(Sid);
} /*Main Ends Here*/
void Disp()
{
printf("Hello World\n");
} /*Function Ends Here */
/*Server.c*/
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#define PORT 5000
void Disp(); // Function Prototype
main()
{
int Sid,b;
struct sockaddr_in Ser;
Sid=Socket(AF_INET,SOCK_STREAM,0);
Ser.sin_family=AF_INET;
Ser.sin_port=htons(port);
Ser.sin_addr.s_addr=INADDR_ANY;
bind(Sid,(struct sockaddr *)&Ser,sizeof(Ser));
listen(Sid,5);
while(1) {
newDesc=accept(Sid,0,0);
recv(Sid,&b,sizeof(b),0);
printf("Value of Clients A = %d\n",b);
// In caseof structure address if received it works well
// Can i read as Below the address of Function Disp and invoke that in this Server.c
/* recv(Sid,&Disp,sizeof(Disp),0); <--- This is the Problem
Disp(); <----- This is the Problem
*/
close(Sid);
} /*Main Ends Here*/
//Segmentation Fault is the Result...
|