1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
int mainfd=0;
char ch[2] = {NULL};
void *write(void *)
{
char temp;
while(1)
{
temp = getchar();
ch[0] = temp; ch[1] = '\0';
if(temp == '~')
{
printf("connection closed.\r\n");
close(mainfd);
pthread_exit(NULL);
}
check=write(mainfd, ch, 1);
ch[0]='\0';
}
}
void *read(void *)
{
char outputbuffer[10000]= {0};
while(1)
{
outputbuffer[0]='\0';
int charnumber=read(mainfd, &outputbuffer, sizeof(outputbuffer));
outputbuffer[charnumber] = '\0';
printf("%s",outputbuffer);
outputbuffer[0] = '\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;
options.c_iflag &= ~(ISTRIP|ICRNL);
options.c_oflag &= ~OPOST;
options.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
options.c_cc[VMIN] = 1;
options.c_cc[VTIME] = 0;
//
tcsetattr(mainfd, TCSAFLUSH, &options);
pthread_t threads[2];
pthread_create(&threads[0], NULL, write, NULL);
pthread_create(&threads[1], NULL, read, NULL);
pthread_exit(NULL);
}
|