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 ().
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of unix / linux socket API reference_3

  • Hibernate object status and operation of

    Hibernate objects of three kinds of status, are as follows: the instantaneous state (Transient), persistent state (Persistent), detached state (Detached). In a persistent state of the object, also known as PO (Persistence Object), transient objects a ...

  • Java development under Linux environment Linux and Windows comparison

    Just me at this series of articles to write essays, when someone asked: Java under Linux at the development of what advantages, if the installation is the same JDK, install Eclipse, and Windows that has developed under the What's the difference? Ask t

  • Performance Optimization of Java

    Java in the mid nineties after the impressive win at the same time, but also attracted some criticism. Won the admiration of the major Java are cross-platform interoperability, the so-called "Write Once, Run Anywhere". However, because of Java

  • Hibernate performance optimization techniques

    Deal with large amount of data at the time, there will be many data buffer stored in the Session level cache, this cache will be great too serious display performance, so in the use of Hibernate handle large data volume, you can use session.clear ( ) ...

  • javascript how to avoid memory leak

    Procedures are likely to happen any memory "leak" (ie, application of system memory and not released after the completion of job), and for the use of unmanaged language (unmanaged languages) (such as C language) of the developers, memory allocat

  • js closure on

    Understanding of JavaScript closures To become advanced JavaScript programmers, it is necessary to understand the closure. In this paper, ECMA 262 specification explain the closure of the internal working mechanism for JavaScript programmers understanding

  • Ruby On Rails (1) hellowolrd

    1. What is Ruby On Rails Ruby On Rails is a Ruby-based Web development framework. Ruby is an object-oriented scripting programming language. 2.Ruby has the following advantages: Explanation-based implementation, convenient and quick Ruby is the interprete

  • Understanding of JavaScript closures

    Understanding of JavaScript closures To become advanced JavaScript programmers, it is necessary to understand the closure. In this paper, ECMA 262 specification explain the closure of the internal working mechanism for JavaScript programmers understanding

  • Hibernate's cache management

    Carried ] [http ://www.cnblogs.com/eflylab/archive/2007/01/11/617276.html Cache is the cache, it is often to improve system performance are the most important means to serve as a reservoir of data and the role of a buffer. Cache for the substantial r ...

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries