Let's take a look at the conflict between my thread and the port number.
Source: Internet
Author: User
Let's take a look at the conflict between my thread and port number-General Linux technology-Linux technology and application information. The following is a detailed description. Why does the server quit when a client is disconnected ..
Int main (int argc, char ** argv)
{
// Socket
Int listenfd; // listen to socket
// Open socket IPV4 TCP
Listenfd = socket (AF_INET, SOCK_STREAM, 0 );
// Define the address structure
Struct sockaddr_in servaddr;
Memset (& servaddr, 0, sizeof (servaddr ));
Servaddr. sin_family = AF_INET;
// Host to network short network byte order-size end
Servaddr. sin_port = htons (9999 );
Servaddr. sin_addr.s_addr = htonl (INADDR_ANY );
// Bind the address
If (bind (listenfd, (struct sockaddr *) & servaddr, sizeof (servaddr) =-1)
{
Printf ("bind error \ n ");
Close (listenfd );
Exit (0 );
}
// Listen
Listen (listenfd, 5); // passive
// Communication
Int clientfd;
Struct sockaddr_in peeraddr;
Int socklen = sizeof (peeraddr );
Memset (& peeraddr, 0, socklen );
// Accept the client
Pthread_t tid1;
While (1)
{
Clientfd = accept (listenfd, (struct sockaddr *) & peeraddr, & socklen );
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