Can I Setup a fd handler for a socket?

I just tried to setup a fd handler for a socket with PtAppAddFd(). The connection is ok since the server can received the data from the client when I use one ‘send()’ command. But it failed when I put the send() command in a fd handler function. So I am wondering if the fd handler function cannot be used for a socket??

Many thanks!!

This doesn’t sound right to me. How did it fail? Did the send() command return an error, and/or set errno?

Well, the problem seems because of the File discriptor (fdtcp in my code below). The string cannot be send out. and the fd function cannot be set to read mode after I use PtAppSetFdMode. I don’t know why, below is my test code, please help.

Many thanks!

connect_to_robot (char* szServer, short  nPort )
		struct hostent *he, *gethostbyref();
		struct sockaddr_in myname;
		struct sockaddr_in ;

		if (he==NULL)
		if ((*p_sockfd = socket(AF_INET, SOCK_STREAM, 0) ) <0)
		myname.sin_family = AF_INET;  // host byte order
		myname.sin_port = htons( nPort);
		myname.sin_addr = *((struct in_addr *)he->h_addr);
		memset(&(myname.sin_zero), '\0', 8 );
		if (connect (*p_sockfd, (struct sockaddr *) &myname,
			sizeof(struct sockaddr)) == -1)
		printf( " network is  set\n");

/************************SET FD HANDLE*******************************************/
fd_handler_function( int fdtcp, void *data, unsigned mode )
  switch( mode )
      case Pt_FD_READ:
        read(fdtcp, buffer_r,sizeof(buffer_r));
       PtTextModifyText(ABW_text_status, 0,0,-1,buffer_r,300);
       PtAppSetFdMode(NULL, fdtcp, Pt_FD_WRITE);
       case Pt_FD_WRITE:
       if( (send(fdtcp, "BEEP 3\r", 7,0) ) == -1)
         PtTextModifyText(ABW_text_status, 0,0,-1,"ERROR2",7);
    PtTextModifyText(ABW_text_status, 0,0,-1,"test",7);

        PtAppSetFdMode(NULL,fdtcp, Pt_FD_READ);
      default:  /* do nothing */

   /* eliminate 'unreferenced' warnings */
   data = data;

   return( Pt_CONTINUE );

client( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )

		  int i;
		  struct timespec start, stop;
          double accum;
       a = -1;
     rval = -1;
     p_sockfd = &sockfd;
	 address = "";
	 port = 2300;
     connect_to_robot(address, port);
    //write(fdtcp, "BEEP 3\r", 7);
         if( (PtAppAddFd( NULL, fdtcp, Pt_FD_WRITE, fd_handler_function, NULL ) ) == -1) 
		     	PtTextModifyText(ABW_text_status, 0,0,-1,"ERROR",7);

Why not check the return value of PtAppSetFdMode() ?

You should also set the socket to NON BLOCK mode because you may get notify after a single byte comes in and read will block until it receives sizeof(buffer_r) data, which may never happen, blocking the read() forever. Further more if it takes a significant amount of time to do the read(), this may render your application unresponsive.