Server programming example

Source: Internet
Author: User

Int fd_set_nonblock (int fd)

{

Int RV;


Rv = fcntl (FD, f_getfl, 0 );

If (RV <0 ){

Printf ("fcntl error \ n ");

Return-1;

}

Rv = fcntl (FD, f_setfl, RV | o_nonblock );

If (RV <0 ){

Printf ("fcntl error \ n ");

Return-1;

}

Return 0;

}

Int init_server_socket ()

{

Int FD;

Struct sockaddr_in ADDR;

Int ret;

Int v_on = 1;


FD = socket (pf_inet, sock_stream, 0 );

If (FD <0 ){

Printf ("server socket create failed \ n ");

Return-1;

}


Ret = setsockopt (FD, sol_socket, so_reuseaddr, & v_on, sizeof (v_on ));

If (Ret! = 0 ){

Printf ("setsockopt error \ n ");

Goto out_close_socket;

}


Memset (& ADDR, 0, sizeof (struct sockaddr_in ));

ADDR. sin_family = af_inet;

ADDR. sin_addr.s_addr = inaddr_any;

ADDR. sin_port = htons (40000 );

Ret = BIND (FD, (struct sockaddr *) & ADDR, sizeof (ADDR ));

If (Ret <0 ){

Printf ("server socket BIND error \ n ");

Goto out_close_socket;

}


Ret = listen (FD, 20 );

If (Ret <0 ){

Printf ("server socket listen error \ n ");

Goto out_close_socket;

}


Ret = fd_set_nonblock (FD );

If (Ret! = 0 ){

Printf ("fd_set_nonblock () failed \ n ");

Goto out_close_socket;

}

Return FD;

Out_close_socket:

Close (FD );

Return-1;

}

Int main (INT argc, char * argv [])

{

Int listenfd;

Int RV;


Listenfd = init_server_socket ();

If (listenfd <0 ){

Exit (1 );

}

While (1 ){

Fd_set readfds;

Struct timeval T;

Int connfd;

Structsockaddr_in clientaddr;

Socklen_t addrlen;

Int R;


Fd_zero (& readfds );

If (listenfd> = 0) fd_set (listenfd, & readfds );

T. TV _sec = 60;

T. TV _usec = 0;

R = select (listenfd + 1, & readfds, null, null, & T );

If (r = 0 ){

Continue;

}

If (r <0 ){

If (errno! = Eintr)

Printf ("signal caught \ n ");

Continue;

}

If (! Fd_isset (listenfd, & readfds ))

Continue;


Memset (& clientaddr, 0, sizeof (struct sockaddr_in ));

Addrlen = sizeof (struct sockaddr_in );

Connfd = accept (listenfd, (struct sockaddr *) & clientaddr, & addrlen );

If (connfd <0 ){

Continue;

}

**********/

}

Close (listenfd );

Return 0;

}


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.