[uClinux-dev] directly call

Phil Wilshire philwil at earthlink.net
Sun Mar 9 08:29:39 EST 2003


HI ChangDa,
I can think of many reasons why this is NOT A GOOD IDEA.

I have only taken a quick look at the code but ..

If it works at all it will only work on MMUless systems.
And ptr will possibly only be valid in the context of the server executable.

If you want to send some data to the client to process the 
"b" routine be present in both executables and all the data that it operates on be transferred through the socket.

You need to think carefully about this design.


regards

Phil Wilshire


ChangDa Tong wrote:
> 
> here is the code , by using socket , the server side tell client the
> address of its function "b"
> then the client call it directly.(Up to now , there is only one client
> and server, later i will
> using lock to control race condition, for the server contail global
> variable)
> Without considering concurrency problem, Is there other problems
> introduce by this way("direct call") that I havn't not known?(besides
> this malloc problem)
> //---------------snap of
> client.c ----------------------------------------------------
> int (*test)(void);
> int main( )
> {
>  un_sock = socket(AF_UNIX, SOCK_STREAM, 0);
>  if(un_sock == -1)
>   perror("open socket fail");
> 
>  sckt.sun_family = AF_UNIX;
>         strncpy(sckt.sun_path, GR_NAMED_SOCKET, sizeof(sckt.sun_path));
> 
>  size = sizeof(sckt);
>  ret = connect(un_sock, (struct sockaddr *) &sckt, size);
>  if(ret == 0)
>   fprintf(stderr, "connect succeed\n");
>  else
>   fprintf(stderr, "connect fail");
> 
>  size = write(un_sock, "iii", 3);
>  bzero(buf,16);
>  fprintf(stderr, "read sock\n");
> /
>  size = read(un_sock, (void *)&address, 4);
>  fprintf(stderr, "%X\n",address);
> 
>  test = address;
>  (*test)();
> }
> //------------------------------here is
> server.c----------------------------
> #define GR_NAMED_SOCKET "/var/.nano-X"
> 
> char *ptr= NULL;
> int un_sock;
> 
> int b(void)
> {
>  fprintf(stderr, "Hello\n");
>  if(ptr){
>   fprintf(stderr, "%s: free ptr once", __FUNCTION__);
>   free(ptr);
>   ptr = NULL;
>  }
>  return 0;
> }
> 
> void
> GsAcceptClient(void)
> {
>         unsigned int i, address;
>         struct sockaddr_un sckt;
>  char buf[16];
>         socklen_t size = sizeof(sckt);
> 
>         if((i = accept(un_sock, (struct sockaddr *) &sckt, &size))
> == -1) {
>                 fprintf(stderr,"accept fail");
>                 return;
>         }
>  address = b;
>  fprintf(stderr, "return address to client\n");
>         write(i, (void *)&address, 4);
> }
> 
> int main()
> {
>  struct sockaddr_un sckt;
> 
>  ptr = malloc(20);
>  un_sock = socket(AF_UNIX, SOCK_STREAM, 0);
>  if(un_sock == -1)
>   perror("open socket fail");
> 
>  sckt.sun_family = AF_UNIX;
>         strncpy(sckt.sun_path, GR_NAMED_SOCKET, sizeof(sckt.sun_path));
> 
>  fprintf(stderr, "Address:%p", b);
> 
>  if(bind(un_sock, (struct sockaddr *) &sckt, SUN_LEN(&sckt)) < 0)
>                 return -1;
> 
>         /* Start listening on the socket: */
>         if(listen(un_sock, 5) == -1)
>                 return -1;
>  GsAcceptClient();
>  sleep(2);
>  if(ptr){
>   printf("S:ptr not null!\n");
>   free(ptr);
>   ptr = NULL;
>  }
>  fprintf(stderr, "S:exit\n");
> }
> 
> _______________________________________________
> uClinux-dev mailing list
> uClinux-dev at uclinux.org
> http://mailman.uclinux.org/mailman/listinfo/uclinux-dev
> This message was resent by uclinux-dev at uclinux.org

-- 
SDCS -- System Design & Consulting Services LLC, http://www.sysdcs.com
** Embedded Linux Training **  email me for details  
630 Springhouse Sq., Leesburg VA 20175 t: 703 669 9766 f: 703 669 9768



More information about the uClinux-dev mailing list