1825756 Members
2332 Online
109687 Solutions
New Discussion

USING PSEUDO TERMINALS

 
CA1490051
Frequent Advisor

USING PSEUDO TERMINALS

Hi All,

I have a small requirement in one of our projects to read and write from pseudo terminals.

for this i have written 2 programs
1) master.cpp
2) slave.cpp

First master opens a pseudo terminal using the function openpty();
and uses select call to wait until data appears on the slave.
Once we run slave program and enter something into the slave device, the same thing is read from master.
And the master has to send some text to the slave.

I am able to write into slave and read from master.
But, after reading in master i am not able to write back to slave.

I am attaching both my master.cpp and my slave.cpp is as below.

#include
#include
#include
#include
#include
#include

#include
#include
#include
using namespace std;
#define SIZE 25
int main ()
{

char buffer[205];
int slavept;
slavept=open("/dev/pts/6", O_RDWR|O_NOCTTY ); // Use the slave device opened by master.cpp
fd_set readfd, errfd;
int selret;
struct timeval time;

//grantpt( slavept ) ;
//unlockpt( slavept );
time.tv_sec = 2;

while (1)
{
//cout <<"ENTER WIZARD COMMAND ==>"<< endl;
//cin >> buffer;
int num=write(slavept,buffer,SIZE);
//sleep(20);

FD_ZERO(&readfd);
FD_SET(slavept, &readfd);
//sleep (10);
selret = select(slavept+1, &readfd, NULL, NULL, NULL);
cout << "SelRet =" << selret << endl;

//
if (selret > 0 )
{
read( slavept, buffer, SIZE );
cout << "DATA FROM MASTER IS =>"< }

}
close( slavept );
return 0;
}



Can any one have a look at it and please help me out to get rid of this blockage

Thanks in Advance
Vikram