Networking and Sockets (1)
来源:互联网 发布:冒险岛2 账号数据异常 编辑:程序博客网 时间:2024/06/04 17:57
Basic System calls For Connected Sockets
#include <sys/socket.h>
int socket(int domain, int type, int protocol); // type SOCK_STREAM, SOCK_DAGRAM
server
int bind(int socket_fd, const struct sockaddr* sa, socklen_t sa_len);
int listen(int socket_fd, int backlog /*maximum connnection queue length, SOMAXCONN*/);
int accept(int socket_fd, struct sockaddr* sa, socklen_t *sa_len);
client
int connect(int socket_fd, const struct sockaddr* sa, socklen_t sa_len);
Byte Order (Little vs. Big Endian)
#include <arpa/inet.h>
convert 16-bit/32-bit value from host to network byte order: (short/long)
uint16_t htons(uint16_t hostnum);
uint16_t htonl(uint32_t hostnum);
convert 16-bit/32-bit value from network to host byte order: (short/long)
uint16_t ntohs(uint16_t netnum);
uint16_t ntohl(uint32_t netnum);
Socket Address
#include <sys/socket.h>
struct sockaddr --> abstract type of address
struct sockaddr_storage --> guarantee to have enough space
AF_UNIX
#include <sys/un.h>
struct sockaddr_un
{
sa_family_t sun_family;
char sun_path[X];
};
AF_INET
#include <netinet/in.h>
struct sockaddr_in
{
sa_family_t sin_family;
in_port_t sin_port;
struct in_addr sin_addr;
};
struct in_addr
{
in_addr_t s_addr;
};
-------------------------
sa.sin_port = htons(80)
sa.sin_addr.sin_addr.s_addr = htols((216UL<<24)+(109UL<<16)+(125UL<<8)+70UL); // 216.109.125.70
-------------------------
#include <arpa/inet.h>
convert dotted ipv4 address string to integer:
in_addr_t inet_addr(const char* cp);
convert integer ipv4 address to dotted string:
char* inet_ntoa(struct in_addr in);
more general functions:
inet_pton
inet_ntop
Socket Options
#include <sys/socket.h>
int setsockopt(
int socket_fd,
int level,
int option,
cont void* value,
socklen_t value_len
);
int getsockopt(...); // same argments as setsockopt
standardized options for level SOL_SOCKET, which applies to the socket level itself.
bool true 1, false 0.
SO_ACCEPTCONN get,bool Socket is accepting connections.
SO_BROADCAST set/get,bool Sending of broadcast messages is allowed if supported.
SO_DEBUG set/get,bool Debug information is recorded.
SO_DONTROUTE set/get,bool Sent message bypass the standard routing facilities and go directly to the network interface according to the destination address.
SO_ERROR get,int Socket error status, which is cleared after it's received.
SO_KEEPALIVE set/get,bool Keep the connection active by periodically sending message. If there's no response, the socket is disconnected.
SO_LINGAR set/get,X If on, cause a close to block if there are any unsent messages unitl they're sent or the linger time expired.
struct linger{
int l_onoff;
int l_linger;
}
SO_OOBINLINE set/get,bool Received out-of-band data stays inline.
SO_RCVBUF set/get,int Receive buffer size.
SO_RCVLOWAT set/get,int Receive low-water-mark. Blocking receive operations block until the lesser of this amount and the requested amount are received.
SO_RCVTIMEO set/get,X Use a timeval structure for the maximum to wait for a blocking recevie operation to complete. Zero time (the default) means infinite.
SO_REUSEADDR seg/get,bool bind allows resue of local address.
SO_SNDBUF set/get,int Send buffer size.
SO_SNDLOWAT set/get,int Send low-water-mark. Nonblocking send operations send no data unlesss the lesser of this amount and the requested amount can be sent immediatly.
SO_SNDTIMEO set/get,X Use a timeval structure for the maximum time to wait for a blocking send operation to complete. Zero time (the default) means infinite.
SO_TYPE set/get,int Socket type (e.g. SOCK_STREAM).
- Networking and Sockets (1)
- Networking and Sockets (3)
- Networking and Sockets (2)
- Lecture 21: Sockets & Networking
- Java Networking and Proxies
- Java networking and proxies
- TN2277 Networking and Multitasking
- Windows Networking and Internet
- Server Push and Server Sockets
- Nodes, Sockets, Cores and FLOPS
- Using Sockets and Socket Streams
- Home Networking Technologies and Standards
- Cases on Telecommunications And Networking
- Home Networking Technologies and Standards
- Satellite Networking: Principles and Protocols
- VMware vCloud Networking and Security
- Sockets编程读书笔记1
- TCP/IP Sockets-1
- HNC理论概要*
- Ubuntu下配置USB转串口及串口工具配置
- 浅淡EditPolicy和GEF的执行流程
- VS2010 MVC+EXTJS4 combo下拉框省、市、区三级联动例子
- 开启yuntable学习之路
- Networking and Sockets (1)
- SEH转化为C++异常
- 自定义ListView、GridView后,选中状态高亮
- 连线(Connection)是怎么添加进Connection Layer的呢
- 目标文件格式分析工具: ar,nm,objdump,objcopy,readelf
- 2011-11-17日450之现代教育技术课后感想
- ConstrainedLayoutEditPolicy与ContainerEditPolicy的区别
- css之图片定位
- Handle是怎么被添加时Handle Layer的?