By turner
I’m trying to connect to CISCO router using termios. So I decided to have two threads one for reading data and the other one for writing data.
And here’s my code :
int mainfd=0;
char ch[2] = {NULL};
void *write(void *)
{
char temp;
while(1)
{
temp = getchar();
ch[0] = temp; ch[1] = '';
if(temp == '~')
{
printf("connection closed.rn");
close(mainfd);
pthread_exit(NULL);
}
check=write(mainfd, ch, 1);
ch[0]='';
}
}
void *read(void *)
{
char outputbuffer[10000]= {0};
while(1)
{
outputbuffer[0]='';
int charnumber=read(mainfd, &outputbuffer, sizeof(outputbuffer));
outputbuffer[charnumber] = '';
printf("%s",outputbuffer);
outputbuffer[0] = '';
}
}
int main(int argc,char *argv[])
{
//////////////////
struct termios old = {0};
if (tcgetattr(0, &old) < 0)
perror("tcsetattr()");
old.c_lflag &= ~ICANON;
old.c_lflag &= ~ECHO;
old.c_cc[VMIN] = 1;
old.c_cc[VTIME] = 0;
if (tcsetattr(0, TCSANOW, &old) < 0)
perror("tcsetattr ICANON");
//////////////////
struct termios options;
static int portnum=atoi(argv[1]);
mainfd = open_port(portnum);
fcntl(mainfd, F_SETFL, FNDELAY);
tcgetattr(mainfd, &options);
cfsetspeed(&options, speed);
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB;
options.c_cflag |= CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag &= ~CRTSCTS;
...read more
Source: FULL ARTICLE at The UNIX and Linux Forums