Help solve this program exit problem

Source: Internet
Author: User
Help solve the problem of program exit-Linux general technology-Linux technology and application information. The following is a detailed description. Server:
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include

# Define MYPORT 4000
# Define BACKLOG 2

Char bye [] = "exit ";

Struct
{
Int sock_fd;
Int client_fd;
Pthread_t sen_tidp;
Pthread_t rev_tidp;
} Pid;

Void * rev_msg (void * arg)
{
Int * sock_fd;
Int num, OK;
Char buf [1024];

Sock_fd = (int *) arg;

Pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL );
Pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL );

OK = 0;
Do
{
Num = read (* sock_fd, buf, 1023 );
Buf [num] = '\ 0 ';
Printf ("it said: % s \ n", buf );
If (strncasecmp (buf, bye, 4) = 0)
OK = 1;
} While (! OK );
Sleep (2 );
Pthread_cancel (pid. sen_tidp );
Return (void *) 0;
}

Void * sent_msg (void * arg)
{
Int * sock_fd;
Int num, OK;
Char buf [1024] = "Welcome! ";

Sock_fd = (int *) arg;
Pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL );
Pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL );

Num = strlen (buf );
Write (* sock_fd, buf, num );
OK = 0;
Do
{
Scanf ("% s", & buf );
Num = strlen (buf );
Write (* sock_fd, buf, num );
If (strncasecmp (buf, bye, 4) = 0)
OK = 1;
} While (! OK );

Sleep (2 );
Pthread_cancel (pid. rev_tidp );
Return (void *) 0;
}

Int main (int argc, char * argv [])
{
Int sock_fd, client_fd;
Int num;
Struct sockaddr_in serv_addr, client_addr;

Pthread_t sen_tidp, rev_tidp;

Serv_addr.sin_family = AF_INET;
Serv_addr.sin_port = htons (MYPORT );
Serv_addr.sin_addr.s_addr = htonl (INADDR_ANY );
Bzero (& (serv_addr.sin_zero), 8 );

Sock_fd = socket (AF_INET, SOCK_STREAM, 0 );
Bind (sock_fd, (struct sockaddr *) & serv_addr, sizeof (struct sockaddr_in ));
Listen (sock_fd, BACKLOG );
Num = sizeof (struct sockaddr_in );
Client_fd = accept (sock_fd, (struct sockaddr *) & client_addr, & num );

Pthread_create (& sen_tidp, NULL, sent_msg, (void *) & client_fd );
Pthread_create (& rev_tidp, NULL, rev_msg, (void *) & client_fd );

Pid. sock_fd = sock_fd;
Pid. client_fd = client_fd;
Pid. sen_tidp = sen_tidp;
Pid. rev_tidp = rev_tidp;

Pthread_join (sen_tidp, NULL );
Pthread_join (rev_tidp, NULL );

Close (client_fd );
Close (sock_fd );
Exit (0 );
}

Client:
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include

# Define SERV_PORT 4000

Volatile pthread_t rev_tidp, sen_tidp;

Char bye [] = "exit ";

Void * sen_msg (void * arg)
{
Int * sock_fd;
Int num;
Char buf [1024];

Sock_fd = (int *) arg;
Pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL );
Pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL );
Sleep (2 );
Do
{

Scanf ("% s", & buf );

Num = strlen (buf );
Write (* sock_fd, buf, num );
} While (strncasecmp (buf, bye, 4 )! = 0 );
Pthread_cancel (rev_tidp );
Pthread_exit (void *) 0 );
}

Void * rev_msg (void * arg)
{
Int * sock_fd;
Int num;
Char buf [1024];

Sock_fd = (int *) arg;

Pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL );
Pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL );

Do
{
Num = read (* sock_fd, buf, 1023 );
Buf [num] = '\ 0 ';
Printf ("it said: % s \ n", buf );
} While (strncasecmp (buf, bye, 4 )! = 0 );

Sleep (2 );
Pthread_cancel (sen_tidp );
Pthread_exit (void *) 0 );
}

Int main (int argc, char * argv [])
{
Int sock_fd;
Int nbyte, nbyte1, num;
Struct sockaddr_in serv_addr;
Struct hostent * host;

Host = gethostbyname ("localhost ");

Serv_addr.sin_family = AF_INET;
Serv_addr.sin_port = htons (SERV_PORT );
Serv_addr.sin_addr = * (struct in_addr *) host-> h_addr );
Bzero (& (serv_addr.sin_zero), 8 );

Sock_fd = socket (AF_INET, SOCK_STREAM, 0 );
Connect (sock_fd, (struct sockaddr *) & serv_addr, sizeof (struct sockaddr_in ));

Pthread_create (& rev_tidp, NULL, rev_msg, (void *) & sock_fd );
Pthread_create (& sen_tidp, NULL, sen_msg, (void *) & sock_fd );

Pthread_join (rev_tidp, NULL );
Sleep (2 );
Pthread_join (sen_tidp, NULL );


Close (sock_fd );
Exit (0 );
}

Why is a program always unable to exit normally when exiting?
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.