[uClinux-dev] directly call

ChangDa Tong tongcd at neusoft.com
Sat Mar 8 23:18:24 EST 2003


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");
}






More information about the uClinux-dev mailing list