UNIX Environment Advanced Programming--TCP/IP Network programming

Source: Internet
Author: User
Tags comments error code socket

Common Network Information retrieval function

GetHostName ()

Getppername ()

GetSockName ()

GetHostByName ()

GETHOSTBYADDR ()

Getprotobyname ()

Getprotobynumber ()

Getservbyname ()

Getservbyport ()

Network Property settings

Header file:

#include <sys/types.h>

#include <sys/socket.h>

Get a set of interface options

int getsockopt (

int SOCKFD,

int level,//option defined hierarchy. The level of support is only Sol_socket and Ipproto_tcp and IPPROTO_IP

int optname,//need to get the set interface options.

void *optval,//Pointer, point to the buffer that holds the value of the option you have obtained.

socklen_t *optlen//pointer pointing to the length value of the optval buffer.

);

return value:

No error: returns 0.

Error: Return SOCKET_ERROR error, the application can get the appropriate error code through WSAGETLASTERROR ().

Comments:

The getsockopt () function is used to get the current value of the option for any type, any state set interface, and to deposit the result in optval. There are options at different protocol layers, but often at the highest "set interface" level, setting options affects the operation of the socket, such as the blocking of the operation, the way the packet is chosen, and the transmission of out-of-band data. The value of the selected option is placed in the optval buffer. The number of Optlen pointed at initially contains the length of the buffer, which is set to the length of the actual value when the call returns. For the So_linger option, the equivalent size of the linger structure is the size of an integer for the other options. If a setsockopt () call is not made, getsockopt () returns the system default value.

Set options for the socket interface

int setsockopt (

int SOCKFD,//Identifies a description of a set of interfaces.

int level,//Option defined hierarchy, currently only supports Sol_socket and ipproto_tcp levels.

int optname,//options to set.

const void *optval,//pointer to the buffer that holds the option value.

socklen_t optlen//optval buffer length.

);

return value:

No error: returns 0.

Error: Return SOCKET_ERROR error, the application can get the appropriate error code through WSAGETLASTERROR ().

Comments:

The setsockopt () function is used for setting option values for any type, any state set interface. Although there are options on different protocol tiers, this function defines only the highest options on the "set interface" level. Options affect the operation of the socket interface, such as whether the expedited data is received in the normal data stream, whether the broadcast data can be sent from the socket, and so on. Options for two sets of interfaces: One is a Boolean option that allows or disables one attribute, and the other is shaping or structural options. Allows a boolean option to point the optval to a number other than 0, and to prevent an option optval point to an integer equal to zero. For Boolean options, Optlen should equal sizeof (int), and for other options, optval points to the number of plastic or structure that contains the desired option, while Optlen is the number of plastic or the length of the structure. The So_linger option is used to control actions that have queued data to be sent on the socket and that the closesocket () call has been executed.

System IO and server model

There are mainly four I/O models under Unix/linux

Blocking I/O: (Pipe size 64K)

Simple, low efficiency, most commonly used

Read blocking

Read,readv,recv,recvfrom,recvmsg

Buffer not readable data

Write blocking

Write,writev,send,sendmag

Write buffer is less than the amount of data to write (UDP exception, no buffer concept)

Other:

Accept,connect

Non-blocking I/O:

Prevents the process from blocking on I/O operations

If the requested I/O cannot be completed immediately, an error is returned.

Setting non-blocking requires looping to test whether the file descriptor has data. (poling) is more CPU intensive.

Implementation: (Using the Fcntl () function)

#include <unistd.h>

#include <fcntl.h>

int fcntl (int fd,//File descriptor

int cmd,//Operation command

Long ARG//flock structure pointer

return value:

Success is returned to 0,

Error returns-1 and sets the errno.

Cases:

struct FLCOK

{

short int l_type;

short int l_whence;

off_t L_start;

off_t L_len;

pid_t L_pid;

}

There are three states of L_type:

F_rdlck to establish a lock for reading

F_wrlck to create a lock for writing

F_unlck the lock that was established prior to deleting

There are also three ways of L_whence:

Seek_set starts at the beginning of the file as a lock.

Seek_cur the location of the current file read/write location as the starting position of the lock

Seek_end at the end of the file as the starting position of the lock.

I/O multiplexing:

Allows simultaneous control over multiple I/O

Thought:

Construct a table about the file descriptor;

Call a function and get one of these descriptors to return when ready for I/O;

When returned, tells the process which descriptor is ready and can do I/O.

Signal-driven I/O:

Asynchronous Communication model

I/O multiplexing Concurrent Server process

#include <sys/time.h>

#include <sys/types.h>

int select (int n,//a collection of all monitored file descriptors)

Fd_set *readfds,//A collection of all the file descriptors to read

Fd_set *writefds,//The collection of all file descriptors to be written

Fd_set *exceptfds,//Other file descriptors to notify us

struct timeval *timeout)/timeout setting.

Timeout Optional: NULL: block until the file descriptor is ready or error, 0: Only detects the state of the file descriptor set, and then returns immediately, not 0: At a specified time, if nothing happens, the timeout returns.

When we call a SELECT, the process blocks to the time that a file can be read or a file can be written or timed out.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/unix/

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.