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!


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

	
		he=gethostbyname(szServer);
		
		if (he==NULL)
		{
			perror("gethostbyname");
			
			exit(1);
		}
	
		if ((*p_sockfd = socket(AF_INET, SOCK_STREAM, 0) ) <0)
		{
		   
			perror("socketfd");
			
			exit(1);
		}
		
		fdtcp=*p_sockfd;
		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)
		{
			perror("connect");
			exit(1);
		}
		
		else 
		printf( " network is  set\n");
	
	}


/************************SET FD HANDLE*******************************************/
int
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);
       	   break;
       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);

         delay(180);
        PtAppSetFdMode(NULL,fdtcp, Pt_FD_READ);
         break;
      default:  /* do nothing */
      	;
   } 
   

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

   return( Pt_CONTINUE );
}

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

	{
		
		  int i;
		  struct timespec start, stop;
          double accum;
       a = -1;
     rval = -1;
     sockfd=0;
     p_sockfd = &sockfd;
	 address = "192.168.0.254";
	 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.