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