Linux Network Programming memo

Source: Internet
Author: User
Tags set socket

0. Related header files

# Include<Sys/socket. h>
# Include<Netinet/in. h>
# Include<ARPA/inet. h> # Include
<Sys/epoll. h>
# Include<Fcntl. h> # Include<Unistd. h> # include <sys/time. h> # Include
<Stdio. h>
# Include<Stdlib. h> # Include<Memory. h> # Include<Assert. h> # Include<Pthread. h> # Include
<Errno. h>

1. Socket Initialization
S = socket (af_inet, sock_dgram,
Ipproto_udp );
 If(S =-1)Return-1; S = socket (af_inet,
Sock_stream, ipproto_tcp );
 If(S =-1)Return-1;StructSockaddr_in
Staddr;
Memset (& staddr, 0,Sizeof(Staddr ));
Staddr. sin_family = af_inet;
Staddr. sin_addr.s_addr = inet_addr (pszip );
Staddr. sin_port = ntohs (ushport );
 If(BIND (S ,(Struct
Sockaddr *) & staddr,
Sizeof(Staddr) =
-1)
{
Return-2;
}2. Non-blocking
// Set non-blockingVoidSetnonblock (IntSock_fd)
{
IntOpts;
Opts =
Fcntl (sock_fd, f_getfl );
If(OPTs
<0
)
{
Printf ("Err: fcntl
F_getfl ");
Exit (1
);
}
Opts = opts | o_nonblock;
If(
Fcntl (sock_fd, f_setfl, opts) <0)
{
Printf ("Err: fcntl f_setfl ");
Exit (1 );
}
}3. Set port Reuse
Bind multiple socket objects to the same port // set socket port Reuse
 If(Setsockopt (S, sol_socket, so_reuseaddr, & flags,Sizeof(Flags) =-1)
{
Return-1;
}4. Customize various Buffer Sizes
// Reset Recv Buf Len
 If(Setsockopt (S, sol_socket, so_rcvbuf ,(Char*) & Ircvbuflen,Sizeof(Ircvbuflen) =
-1)
{
Return-1;
} // Reset send Buf Len
 If(Setsockopt (S, sol_socket, so_sndbuf ,(Char*) & Isendbuflen,Sizeof(Isendbuflen) =
-1)
{
Return-1;
}5. Set TCP nodelay (disable the Nagle algorithm)1) disable the Nagle algorithm when real-time requirements are high. 2) The Nagle algorithm is used to reduce the number of packets.IntFlag = 1;
IntResult =
Setsockopt (sock,/* socket affected
*/
Ipproto_tcp,/* Set option at TCP level
*/
Tcp_nodelay,/* Name of Option
*/
(Char*) & Flag,/* The cast is
Historical
Cruft
*/
Sizeof(Int);/* Length of Option
Value */6. Set so_lingerWhen the linger parameter is set to a positive integer N (the maximum value of N is 65,535), it will be blocked for up to n seconds after the close method is called. In these n seconds, the system will try to Send unsent packets out; if more than n seconds are passed, if there are still unsent packets, all these packets will be discarded; the close method returns immediately. If you set the linger to 0, the function is the same as if you disable the so_linger option. The so_linger option is used to change this default setting. Use the following structure:

StructLinger {
IntRochelle Onoff;/* 0 = OFF, nozero
= On */
IntRochelle linger;/* Linger time */
};

StructLinger so_linger;
So_linger.l_onoff = true;
So_linger.l_linger =
30;
IntResult
=
Setsockopt (S,
Sol_socket,
So_linger,
& So_linger,
Sizeof
So_linger );

 7. Set so_keepaliveThe client socket sends a packet to the server through idle connections at intervals (about two hours. This packet does not have any other function, just to check whether the server is still active

Keepalive = 1;
Setsockopt (listenfd, sol_socket, so_keepalive,
( Void*) & Keepalive, Sizeof(Keepalive ));

 8. Server Initialization

Listen_fd = socket (af_inet, sock_stream, 0); memset (& svraddr, 0, Sizeof(Sockaddr_in)
);
Inet_aton ("127.0.0.1", & svraddr. sin_addr); svraddr. sin_family =
Af_inet;
Svraddr. sin_port = htons (12345 ); IntRET;
Ret = BIND (listen_fd, (sockaddr *) & svraddr, Sizeof(Sockaddr_in ));
If(Ret! = 0)
{
Perror ("Bind
Fail ");
Exit (-3 );
} Listen (listen_fd, 20 ); 9. Accept
// Be sure to initialize the length; otherwise, accept errors may occur.

Clnlen =Sizeof(Sockaddr_in );

Memset (& clnaddr, 0,Sizeof(Sockaddr_in)
); Conn_fd = accept (listen_fd, (sockaddr *) & clnaddr, & clnlen );
If(
Conn_fd <0
)
{
Perror ("conn_fd <0 ");
Break;
}

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.