unix / linux socket API reference_3

sendmsg (transmit data via socket)
Correlation function send, sendto, recv, recvfrom, recvmsg, socket

Header file # include <sys/types.h>
# Include <sys/socket.h>

Defined function int sendmsg (int s, const strcut msghdr * msg, unsigned int flags);

Function Description sendmsg () is used to transmit data from the specified socket the other host. Parameter s has established a good connection for the socket, if using UDP protocol is not subject to connection operation. For connection parameter msg points to the data structure elements, parameter defaults to 0 flags generally, a detailed description please refer to send ().
Structure defined as follows msghdr
struct msghdr
(
void * msg_name; / * Address to send to / receive from. * /
socklen_t msg_namelen; / * Length of addres data * /
strcut iovec * msg_iov; / * Vector of data to send / receive into * /
size_t msg_iovlen; / * Number of elements in the vector * /
void * msg_control; / * Ancillary dat * /
size_t msg_controllen; / * Ancillary data buffer length * /
int msg_flags; / * Flags on received message * /
);

Real success the return value is transmitted back to the number of characters, fails to return -1, because the error stored in errno

Error code EBADF parameter s socket handling code of non-legitimate.
EFAULT parameter a pointer can not access the memory space
ENOTSOCK parameter s is a file descriptor, non-socket.
EINTR interrupted by signals.
EAGAIN The operation would block the process, but the parameter s of the socket is not blocked.
ENOBUFS system memory buffer
ENOMEM core memory
EINVAL the parameters passed to system calls is incorrect.

Example reference sendto ().

sendto (data transfer via socket)
Correlation function send, sendmsg, recv, recvfrom, socket

Header file # include <sys / types.h>
# Include <sys / socket.h>

Defined function int sendto (int s, const void * msg, int len, unsigned int flags, const
struct sockaddr * to, int tolen);

Function Description sendto () is used to transmit data from the socket specified by the other host. Parameter s has been built to connect the socket, if using UDP protocol is not subject to connection operation. For connection parameter msg data point to the contents of the general parameters set flags 0, a detailed description please refer to send (). For transmission parameters to be used to specify the network address, the structure sockaddr see bind (). The results for the sockaddr parameter tolen length.

Real success the return value is transmitted back to the number of characters, fails to return -1, the error stored in errno in the cause.

Error code EBADF parameter s illegal socket handling code.
EFAULT parameter a pointer can not access the memory space.
WNOTSOCK canshu s file descriptor for a non-socket.
EINTR interrupted by signals.
EAGAIN This action will make the process of blocking, but the parameters s, soket blocked for the extra lessons.
ENOBUFS system buffer memory.
EINVAL the parameters passed to system calls is incorrect.

Example # include <sys / types.h>
# Include <sys / socket.h>
# Include <netinet.in.h>
# Include <arpa.inet.h>
# Define PORT 2345 / * use the port * /
main () (
int sockfd, len;
struct sockaddr_in addr;
char buffer [256];
/ * Set up socket * /
if (sockfd = socket (AF_INET, SOCK_DGRAM, 0)) <0) (
perror ("socket");
exit (1);
)
/ * Fill in sockaddr_in structure * /
bzero (& addr, sizeof (addr));
addr.sin_family = AF_INET;
addr.sin_port = htons (PORT);
addr.sin_addr = hton1 (INADDR_ANY);
if (bind (sockfd, & addr, sizeof (addr)) <0) (
perror ("connect");
exit (1);
)
while (1) (
bezro (buffer, sizeof (buffer));
len = recvfrom (socket, buffer, sizeof (buffer), 0, & addr & addr_len);
/ * Display the client side network address * /
printf ("receive from% s \ n", inet_ntoa (addr.sin_addr));
/ * The string returned to the client side * /
sendto (sockfd, buffer, len, 0, & addr, addr_len); "
)
)

Please refer to the implementation of recvfrom ()

setprotoent (open network protocol for data files)
Correlation function getprotobyname, getprotobynumber, endprotoent

Header file # include <netdb.h>

Define function void setprotoent (int stayopen);

Function Description setprotoent () is used to open the / etc / protocols, if the parameter stayopen value is 1, then the next getprotobyname () or getprotobynumber () will not automatically close the file.

setservent (open host network services, data files)
Correlation function getservent, getservbyname, getservbyport, endservent

Header file # include <netdb.h>

Define function void setservent (int stayopen);

Function Description setservent () is used to open the / etc / services, if the parameter stayopen value is 1, then the next getservbyname () or getservbyport () will make up automatically close the file.

setsockopt (Set socket state)
Correlation function getsockopt

Header file # include <sys/types.h>
# Include <sys/socket.h>

Defined function int setsockopt (int s, int level, int optname, const void * optval,, socklen_toptlen);

Function Description setsockopt () s used to set the parameters specified by the socket state. To set the parameter level on behalf of the network layer, usually set to SOL_SOCKET to access the socket layer. To set the optname parameter representative of the options are the following values:
SO_DEBUG turned on or off debugging mode
SO_REUSEADDR allows the bind () process the local address can be reused
SO_TYPE return socket shape.
SO_ERROR return socket error has occurred because
SO_DONTROUTE send packets do not use routing equipment to transmit.
SO_BROADCAST use of radio transmission
SO_SNDBUF set buffer zones, sent
SO_RCVBUF set to receive buffer zones
SO_KEEPALIVE on a regular basis to determine whether the connection has been terminated.
SO_OOBINLINE when received OOB data will be immediately sent to the standard input device
SO_LINGER to ensure safe and reliable transmission of data out.

To set the parameters representative of the value optval, optlen parameter was the length of the optval.

Return value success, returns 0 if an error is returned -1, error causes stored in errno.

Annotated EBADF s is not a valid parameter socket handling code
ENOTSOCK parameter s is a file descriptor, non-socket
ENOPROTOOPT parameter optname option specified is not correct.
EFAULT parameters optval pointer can not access the memory space.

Example reference getsockopt ().

shutdown (end socket communication)
Correlation function socket, connect

Header file # include <sys/socket.h>

Defined function int shutdown (int s, int how);

Function Description shutdown () to terminate the argument s specified socket connection. Parameter s is the connection in the socket handling code, arguments how the following situations:
how = 0 terminate the read operation.
how = 1 to terminate the transfer,
how = 2 end to read and transfer operation

Return value back to 0 on success, failure returns -1, error causes stored in errno.

Parameter error code EBADF s is not a valid socket handling code
ENOTSOCK parameter s is a file descriptor, non-socket
ENOTCONN parameter s does not specify the socket connection

socket (to establish a socket communication)
Correlation function accept, bind, connect, listen

Header file # include <sys/types.h>
# Include <sys/socket.h>

Defined function int socket (int domain, int type, int protocol);

Function Description socket () is used to create a new socket, which is registered to the system, notification system to establish a communications port. Parameter domain which specifies the address type to use, complete defined in / usr / include / bits / socket.h, the bottom is a common agreement:
PF_UNIX / PF_LOCAL / AF_UNIX / AF_LOCAL UNIX process communication protocol
PF_INET? AF_INET Ipv4 Network Protocol
PF_INET6/AF_INET6 Ipv6 network protocol
PF_IPX / AF_IPX IPX-Novell agreement
PF_NETLINK / AF_NETLINK core user interface device
PF_X25/AF_X25 ITU-T X.25/ISO-8208 agreement
PF_AX25/AF_AX25 amateur radio AX.25 protocol
PF_ATMPVC / AF_ATMPVC access to raw ATM PVCs
PF_APPLETALK / AF_APPLETALK appletalk (DDP) protocol
PF_PACKET / AF_PACKET primary packet interfaces

Parameter type are the following values:
SOCK_STREAM provides continuous and reliable two-way data flow, that is TCP. Support
OOB mechanism to transfer all the data must be used before connect () to establish a connection state.
SOCK_DGRAM use of non-continuous connection is not reliable packet
SOCK_SEQPACKET provide continuous reliable data packet connection
SOCK_RAW provide raw network protocol access
SOCK_RDM provide reliable packet connections
SOCK_PACKET provides direct communication and network drivers.
protocol is used to specify the socket number used by the transport protocol, usually do not cares for this reference, set to 0.

Successful return value is returned socket handling code, failure to return -1.

Error code EPROTONOSUPPORT parameter domain parameters specified type does not support the specified protocol type or protocol
ENFILE core memory, can not create new socket structure
EMFILE process file table overflow, can not create a new socket
EACCESS insufficient privileges to set up the specified protocol type or protocol
ENOBUFS / ENOMEM memory
EINVAL parameter domain / type / protocol is not legitimate

Sample Reference connect ().

分类:CPP 时间:2010-08-04 人气:184
分享到:
blog comments powered by Disqus

相关文章

  • Linux system call notes 2011-05-10

    * Linux implementation of the system call 1. Unix / Linux operating system introduces the architecture and system calls 1. What is the operating system and system calls Operating system is abstracted from the hardware virtual machine, the virtual mac

  • unix / linux socket API reference_1 2010-08-04

    accept (to accept socket connection) Correlation function socket, bind, listen, connect Header file # include <sys/types.h> # Include <sys/socket.h> Defined function int accept (int s, struct sockaddr * addr, int * addrlen); Function Descripti

  • successfully installed linux system redhat9.0 Summary oracle10g 2010-12-20

    Installed above the redhat9.0 oracle10G toss for a long time, the first system installed in the linux install oracle database above to go a lot of detours, Oh no matter how, it is ultimately successful, while more in-depth knowledge of linux point, n

  • Window XP installation from the hard drive under Linux system 2011-01-04

    Ylmf OS: http://www.ylmf.org/download.html ★ 1. Before installation: From the hard drive to install Fedora 8, where you have to ensure that the image file stored Feora 8 disk as FAT32, because both Linux or DOS, can not recognize NTFS. Also you have

  • ioctl function under linux system 2011-05-18

    Transfer from: http://www.sudu.cn/info/html/edu/20080102/282377.html I said here in the driver ioctl function is in, because I do not know no other occasion to use the ioctl, So defines our scope of discussion. Why write articles it is because a whil

  • Linux System Programming Study Notes (7) memory management 2010-05-01

    Memory management: For a process, the memory is the most basic and most important resources. Memory management, including: memory allocation, memory operations and memory release. 1, the process address space: Linux to virtualization of physical memo

  • unix / linux system to view the process of memory, Linux 2011-07-25

    unix / linux system to view process memory Abstract: This paper describes the time process management concepts and process management tools introduced; paper focuses on the process management tool for the classification description and application ex

  • HowTo: Profile Memory in a Linux System 2010-09-03

    HOWTO: Profile Memory in a Linux System 1. Introduction It's important to determine how your system utilizes it's resources. If your systems performance is unacceptable, it is necessary to determine which resource is slowing the system down. This doc

  • Configured on the Linux system RedHat9.0 SVN + APACHE Service Reference 2010-05-25

    (See a lot of pages with their own mistakes continue to keep the solution finally solved the svn service configured on the linux system problems now with my own reference data problems encountered when installing summarize, for your reference) First,

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.947 (s). 12 q(s)