LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-15-2004, 06:50 AM   #1
sunny_krishna
LQ Newbie
 
Registered: Apr 2004
Location: Bangalore
Distribution: PCQ LInux
Posts: 1

Rep: Reputation: 0
Arrow 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...
 
Old 04-15-2004, 08:20 AM   #2
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
No

The two process that you will run (client and server) will have a separate address space. So you can't send an address of a function from the client and run it on the server. (if this is what you want to do).

If you want to call a function from the client in the server you should try a remote procedure call mechanism (RPC - simple, CORBA - complex).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
passing parameters to functions in shell script kushalkoolwal Programming 1 09-28-2005 02:40 PM
Converting php5 socket functions to php3 socket functions mrobertson Programming 0 06-23-2005 09:11 AM
passing passing variable in Java as reference djgerbavore Programming 3 11-10-2004 02:18 PM
passing value to functions in C pantera Programming 1 06-10-2004 09:08 AM
pointers to functions/member functions champ Programming 2 03-28-2003 06:22 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration