��ǰλ�ã���ҳ > Ƕ��ʽ��ѵ > Ƕ��ʽѧϰ > ��ʦ���� > ����TCP/UDP��Socket���

����TCP/UDP��Socket��� ʱ�䣺2019-08-08      ��Դ����ɳ���ģ�����ʦ

---- socket������



---- �ӿڼ�飺











---- �������£�



>> socket() : creating  a socket 

A socket is an abstraction of a communication endpoint. Just as they would use file descriptors to access files, applications use socket descriptors to access sockets. To create a socket, we call the socket() function.

ԭ�ͣ�int socket(int domain, int type, int protocol);

����ֵ�� returns file (socket) descriptor if OK, -1 on error.

domain��AF_INET, AF_INET6, AF_UNIX, AF_UNSPEC (address format)



The protocol argument is usually zero, to select the default protocol for the given domain and socket type. The default protocol for a SOCK_STREAM socket in the AF_INET communication domain is TCP(Transmission Control Protocol). The default protocol for a SOCK_DGRAM socket in the AF_INET communication domain is UDP(User Datagram Protocol).

NOTE: UDP -- ���ݱ���datagram���������ӵģ�no logical connection exist between peers for them to communicate. A datagram is a self-contained(������) message. �����ڣ�analogous�����ʼ�������Է��Ͷ���ʼ������Dz��ܱ�֤�ʼ��Ƿ񵽴���ʼ������˳��ÿһ���ʼ��������˽����ߵĵ�ַ��

TCP -- �ֽ��� A byte stream(SOCK_STREAM), in contrast, �ڴ�������֮ǰ����Ҫ�������ӣ��������ӵ�ͨ�������ڴ�绰���㵽��������������source and destination��

Communication on a socket is bidirectional. We can disable I/O on a socket with the shutdown function.

>> shutdown()   

ԭ�ͣ�int shutdown(int sockfd, int how);

����ֵ�� returns 0 if OK, -1 on error.

The shutdown() system call closes one or both channels of the socket sockfd, depending on the value of how, which is specified as one of the following:

how:  SHUT_RD, then reading from the socket is disabled.  SHUT_WR, then we can't use the socket for transmitting data. We can use SHUT_RDWR to disable both data transmission and reception.

shutdown() differs from close() in another important respect: it closes the socket channels regardless of whether there are other file descriptors referring to the socket. For example, sockfd refers to a connected stream socket. If we make the following calls, then the connection remains open, and we can still perform I/O on the connection via the file descriptor fd2:

1.    fd = dup(sockfd);

2.    close(sockfd);

However, if we make the following sequence of calls, then both channels of the connection are closed, and I/O can no longer be performed via fd2:

1.    fd2 = dup(sockfd);

2.    shutdown(sockfd,SHUT_RDWR);

Note that shutdown() doesn't close the file descriptor, even if how is specified as SHUT_RDWR. To close the file descriptor, we must additionally call close().

>> bind() : binding a socket to an address    

The bind() system call binds a socket to an address.

ԭ�ͣ�int bind(int sockfd, const struct sockaddr * addr, socklen_t addrlen);

����ֵ��returns 0 on success, or -1 on error.

The sockfd argument is a file descriptor obtained from a previous call to socket(). The addr argument is a pointer to a structure specifying the address to which this socket is to be bound. The type of structure passed in this argument depends on the socket domain. The addrlen argument specifies the size of the address structure.

Typically, we bind a server's socket to a well-known address - that is, a fixed address that is known in advance to client applications that need to communicate with that server.

>> listen() : listening for incoming connections    

ԭ�ͣ�int listen(int sockfd, int backlog); // returns 0 on success, or -1 on error.

The listen() system call marks the stream socket referred to by the file descriptor sockfd as passive. The socket will subsequently be used to accept connections from other(active) sockets.

The client may call connect() before the server calls accept(). This could happen, for example, because the server is busy handling some other clients. This results in a pending connection, as illustrated in Figure 56-2.


The kernel must record some information about each pending connection request so that a subsequent accept() can be processed. The backlog argument allows us to limit the number of such pending connections. Further connection requests block until a pending connection is accepted(via accept()), and thus removed from the queue of pending connections.

>> accept() : accepting a connection   

The accept() system call accepts an incoming connection on the listening stream socket referred to by the file descriptor sockfd. If there are no pending connections when accept() is called, the call blocks until a connection request arrives when the sockfd in block mode. If sockfd is in nonblocking mode, accept() will return -1 and set errno to either EAGAIN or EWOULDBLOCK.

ԭ�ͣ�int accept(int sockfd, struct sockaddr * restrict addr, socklen_t * restrict len);

����ֵ��return file(socket) descriptor if OK, -1 on error.


The key point to understand about accept() is that it creates a new socket, and this new socket that is connected to the peer socket that performed the connect(). This new socket descriptor has the same socket type and address family as the  original socket(sockfd). A file descriptor for the connected socket is returned as the function result of the accept() call. The listening socket(sockfd) remains open, and can be used to accept further connections. A typical server application creates one listening socket, binds it to a well-known address, and then handles all client requests by accepting connections via that socket.

The remaining��ʣ��ģ� arguments to accept() return the address of the peer socket.(�ͻ���)

If we don't care about the client's identity, we can set the addr and len parameters to NULL. Otherwise, before calling accept, we need to set the addr (ָ��һ��buffer) parameter to a buffer large enough to hold the address and set the integer pointed to by len to the size of the buffer in bytes. On return, accept will fill in the client's address in the buffer and update the integer pointed to by len to reflect the size of the address.

>> connect() : connecting to a peer socket  

ԭ�ͣ�int connect(int sockfd, const struct sockaddr * addr, socklen_t addrlen);

����ֵ�� returns 0 on success, or -1 on error.

The connect() system call connects the active socket referred to by the file descriptor sockfd to the listening socket whose address is specified by addr and addrlen.

>> send() : TCP���͵����ݷ��� 

ԭ�ͣ�int send(int sockfd, const void * msg, int len, int flags);

ÿ��TCP�׽ӿڶ���һ�����ͻ����������Ĵ�С������SO_SNDBUF���ѡ�����ı䡣����send�����Ĺ���ʵ�����ں˽��û����ݣ�msg��������TCP�׽ӿڵķ��ͻ������Ĺ��̡���len���ڷ��ͻ������Ĵ�С���򷵻�-1. ���򣬲鿴������ʣ��ռ��Ƿ����ɵ���Ҫ���͵�len���ȣ���������򿽱�һ���֣������ؿ������ȣ�ָ���Ƿ�����send����Ϊ����send����һ���ȴ��������ݿ������������ŷ��أ��������send����ֵ�ض���len��ȣ����������������ȴ����ͣ���ʣ��ռ�󿽱��������������ڿ��������г��ִ����򷵻�-1.���ڴ����ԭ�򣬲鿴errno��ֵ��



sockfd -- ���Ͷ��׽��������� ���Ǽ�����������

msg -- ���������ݵĻ����� ���������ݵ�len���ȿ�����socket�ķ��ͻ�������

len -- ���������ݵ��ֽڳ��ȡ�

flags -- һ���������Ϊ0.

>> recv() : TCP���͵����ݽ��� 

ԭ�ͣ�int recv(int sockfd, void *buf, int len, unsigned int flags);



sockfd -- ���ն��׽������������Ǽ�����������

buf -- �������ݵĻ���ַ����socket�Ľ��ջ������е����ݿ�����buf�У�

len -- ���յ����ݵ��ֽ���

flags -- һ���������Ϊ0.

>> sendto() : UDP���͵����ݷ��� 

ԭ�ͣ�int sendto(int sockfd, const void * msg, int len, unsigned int flags, const struct sockaddr * dst_addr, int addrlen);




For sendto(), the dest_addr and addrlen arguments specify the socket to which the datagram is to be sent. These arguments are employed in the same manner as the corresponding arguments to connect(). The dest_addr argument is an address structure suitable for this communication domain. It is initialized with the address of the destination socket. The addrlen argument specifies the size of addr.

>> recvfrom() : UDP���͵����ݽ��� 

ԭ�ͣ�int recvfrom(int sockfd, void * buf, size_t len, int flags, struct sockaddr * src_addr, int * addrlen);


sockfd -- ���ն��׽���������

buf -- ���ڽ������ݵ�Ӧ�û�������ַ��

len -- ָ����������С��

flags -- ͨ��Ϊ0��

src_addr -- ������Դ�˵ĵ�ַ��IP address��Port number��.

fromlen -- ��Ϊ����ʱ��fromlen��������Ϊsizeof��struct sockaddr��.

For recvfrom(), the src_addr and addrlen arguments return the address of the remote socket used to send the datagram. (These arguments are analogous to the addr and addrlen arguments of accept(), which return the address of a connecting peer socket.) Prior to the call(�ڵ���֮ǰ), addrlen should be initialized to the size of the structure pointed to by src_addr���ṹ�Ĵ�С��; upon return���ڷ���ʱ��, it contains the number of bytes actually written to this structure

��һƪ��ARM I2C���ο���


ǰ̨ר�ߣ�010-82525158 ��ҵ��ѵǢ̸ר�ߣ�010-82525379 ԺУ����Ǣ̸ר�ߣ�010-82525379 Copyright © 2004-2018 ��������Զ���Ƽ���չ���޹�˾ ��Ȩ���� ����ICP��16055225�ţ�������������11010802025203��