Chinaunix首页 | 论坛 | 博客
  • 博客访问: 335609
  • 博文数量: 92
  • 博客积分: 2500
  • 博客等级: 少校
  • 技术积分: 960
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-21 19:38
文章分类

全部博文(92)

文章存档

2010年(71)

2009年(21)

我的朋友

分类: 嵌入式

2009-09-15 08:30:27

Socket() functions:
int socket (int domain, int type, int protocol);
domain
parameter specifies the type of socket, usually AF_INET, type can be SOCK_STREAM or SOCK_DGRAM, respectively TCP connections and UDP connections; protocol is usually assigned "0." Socket () call returns an integer socket descriptor, you can call later to use it.
Once the socket call returns a socket descriptor, you should associate the socket with a port on your machine (often when you are in the design of server-side program need to call this function. Then you can listen on the port service request; The client normally doesn’t need this function).


   
Bind() function:
int bind (int sockfd, struct sockaddr * my_addr, int addrlen);
Sockfd is a socket descriptor, my_addr is a pointer which contains local IP address and port number and other information of the type sockaddr; addrlen is often set to sizeof (struct sockaddr).
Finally, for bind function there is one point to illustrate that you can use the following assignment to obtain an IP address and random port number that has not been occupied by:
my_addr.sin_port = 0; / * system randomly select port number not using * /
my_addr.sin_addr.s_addr = INADDR_ANY; / * fill in the local IP address * /
By my_addr.sin_port set to 0, the function will automatically select for you a non-occupied port to use. Similarly, by my_addr.sin_addr.s_addr set to INADDR_ANY, the system will automatically fill in the IP address of the machine. Bind () function is called when a successful return 0; encountered an error when you return "-1" and set errno to the corresponding error number. Also to note that, when you call the function, the general rule, not the port number set to a value of less than 1024, because 1 ~ 1024 is to retain the port number, you can use any one of more than 1024 have not been occupied by the port number.



Connect() function:
Used in conjunction with the remote server to establish a TCP connection, the function prototype is:
int connect (int sockfd, struct sockaddr * serv_addr, int addrlen);
Sockfd is the target server sockt descriptor; serv_addr purpose machine that contains the IP address and port number of the pointer. Encountered an error returns -1, and errno contains the corresponding error code. Client-side programming do not call bind (), because the purpose of this case, only need to know the IP address of the machine, while the client and the server port through which to establish a connection does not need to care about, the kernel will automatically choose a port is not occupied for the client to use.



Listen() function:
Used to monitor whether the service request
In the server-side process, when the socket with a port bundle the future, they need to listen to the port in order to reach the service requests to be addressed.
int listen (int sockfd, int backlog);
Sockfd is a Socket system calling returned socket descriptor; backlog specified in the request allows the maximum number of requests, the incoming connection request will be in the queue waiting for accept() them (see below). Backlog on the queue waiting for service has restricted the number of requests, most of the system default value is 20. When an error is encountered listen to return -1, errno is set to the corresponding error code.


 
accept () function:
Used to connect the port to service request.
When a client attempts to connect with the server listening port, the connection request will be waiting for the server accept () it. By calling the accept () function for the establishment of a connection, accept () function will return a new socket descriptor for the use of this new connection to use. The server can continue to listen on a socket before that, while in the new socket descriptor data on the send () (send) and recv() (receive) operation:
int accept (int sockfd, void * addr, int * addrlen);
sockfd is listening socket descriptor, addr is usually a pointer pointing to sockaddr_in variable, the variable is used to store request for service made to connect the host information (from a port of a host to issue the request); addrten is usually a point to value of sizeof (struct sockaddr_in) integer pointer variable. Returns a -1 when the error occurred and set the appropriate errno value.


 
Send () function:
int send (int sockfd, const void * msg, int len, int flags);
Sockfd is that you want to use to transmit data socket descriptor, msg is a pointer to point to send the data.
Len bytes of data are length. Under normal circumstances flags set to 0 (on the usage of this parameter can refer to man's manual).
char * msg = "Beej was here!"; int len, bytes_sent; ... ...
len = strlen (msg); bytes_sent = send (sockfd, msg, len, 0); ... ...
Send () function returns the number of bytes actually sent out, may be less than you want to send data. Therefore, the need to send () return value measurements. When the send () return value and len does not match, they should deal with this situation.



recv () function:
int recv (int sockfd, void * buf, int len, unsigned int flags);
Sockfd is to accept the data socket descriptor; buf is a buffer to receive data; len is the length of the buffer. Flags are also set to 0. Recv () returns the number of bytes actually received, or when an error occurs, return -1 and set the appropriate errno value.



Sendto () function:
In connectionless datagram socket mode, the local socket, do not establish a connection with the remote machine, so the data should be specified in the destination address to send, sendto () function prototype is:
int sendto (int sockfd, const void * msg, int len, unsigned int flags, const struct sockaddr * to, int tolen);
The function has two more parameters than the send () function, , to is head to destination IP address and port number information, and tolen often assigned to sizeof (struct sockaddr). Sendto function also returns the actual length of data bytes sent, or send an error occurred returns -1.



Recvfrom () function:
int recvfrom (int sockfd, void * buf, int len, unsigned int flags, struct sockaddr * from, int * fromlen);
from is a struct sockaddr type of variable, the variable to save the source machine IP address and port number. fromlen often set to sizeof (struct sockaddr). When the recvfrom () returns, fromlen deposited with the actual number of bytes from the data. Recvfrom () function returns the number of bytes received or when the error returns -1, and set the appropriate errno.
It should be noted is that when you call for the socket packet connect () function, you can use send () and recv () for data transmission, but the socket is still a datagram socket, and use the UDP transport layer service. However, in sending or receiving data the newspaper whom the kernel will automatically add the purpose and the source address information.



Close () function:
- End the data transfer
When all the data manipulation ends, you can call the close() function to release the socket, in order to stop at the socket on any data manipulation: close (sockfd);
You can also call the shutdown () function to close the socket. This function allows you to just stop in one direction of data transmission, and a direction of data transfer to continue. And you can turn off the write operation of a socket in the socket and continue to accept data until all the data read.



 

阅读(1281) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~