码迷,mamicode.com
首页 > Windows程序 > 详细

windows套接字相关函数

时间:2016-04-18 15:14:52      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:

windows套接字相关函数


作者:vpoet
mail:18200268879@163.com

我们学习TCP/IP协议无非是利用这些协议进行通信开发,然而假设让我们自己来直接依据协议规则和协议格式来
进行网络开发无疑是一件十分痛苦的事情,显然为了减轻程序猿的开发负担,windows提供给我们一套网络开发
的API,这个API族就叫做套接字库。

可是套接字和TCP/IP协议究竟是什么关系呢。

我们暂且能够这样理解,如图:
技术分享



那么OK,理解不了我们也暂且这样理解吧。

接下来我们讲讲使用套接字编程主要用到API




1:socket
MSDN这样描写叙述:

The Windows Sockets socket function creates a socket that is bound to a specific service provider.

SOCKET socket(
  int af,       
  int type,     
  int protocol  
);

Parameters

af
[in] Address family specification.
type
[in] Type specification for the new socket.

The following are the only two type specifications supported for Windows Sockets 1.1:

Type Explanation
SOCK_STREAM Provides sequenced, reliable, two-way, connection-based byte streams with an OOB data transmission mechanism. Uses TCP for the Internet address family.
SOCK_DGRAM Supports datagrams, which are connectionless, unreliable buffers of a fixed (typically small) maximum length. Uses UDP for the Internet address family.


In Windows Sockets 2, many new socket types will be introduced and no longer need to be specified, since an application can dynamically discover the attributes of each available transport protocol through the WSAEnumProtocols function. Socket type definitions appear in Winsock2.h, which will be periodically updated as new socket types, address families, and protocols are defined.

protocol
[in] Protocol to be used with the socket that is specific to the indicated address family. 


该函数创建一个套接字,參数af为协议族类型AF_INET为TCP/IP协议族
type为套接字类型,有两种类似SOCK_STREAM为使用TCP进行通信 SOCK_STREAM为使用UDP进行通信
protocol能够为IPPROTO_TCP或IPPROTO_IP,此參数英语type保存一致

举例:SOCKET S;
 S=socket(AF_INET,SOCK_STREAM,IPPOTO_TCP)表示使用TCP协议进行通信






2.bind
顾名思义即绑定,用过套接字编程的都知道,当我们编写服务端程序的时候须要使用该函数
MSDN这样描写叙述:

The Windows Sockets bind function associates a local address with a socket.

int bind(
  SOCKET s,                          
  const struct sockaddr FAR *name,   
  int namelen                        
);

Parameters

s
[in] Descriptor identifying an unbound socket.
name
[in] Address to assign to the socket from the SOCKADDR structure.
namelen
[in] Length of the value in the name parameter.

Return Values

If no error occurs, bind returns zero. Otherwise, it returns SOCKET_ERROR, and a specific error code can be retrieved by calling WSAGetLastError.


该函数将一个本地的地址绑定到一个套接字上,假设绑定不错位,将返回0,否则将返回SOCKET_ERROR
參数s为须要绑定的套接字
參数*name为一个常量sockaddr结构。可是普通情况下我们都使用sockaddr_in结构
其结构定义例如以下:
struct sockaddr_in {
  short int sin_family; /* Address family */
  unsigned short int sin_port; /* Port number */
  struct sin_addr sin_addr; /* Internet address */
  unsigned char sin_zero[8]; /* Same size as struct sockaddr */
  };
  sin_family:指代协议族,在socket编程中仅仅能是AF_INET
  sin_port:存储端口号(使用网络字节顺序)
  sin_addr:存储IP地址。使用in_addr这个数据结构
  sin_zero:是为了让sockaddr与sockaddr_in两个数据结构保持大小同样而保留的空字节。
当中sin_addr定义
typedef struct in_addr {
  union {
  struct{ unsigned char s_b1,s_b2, s_b3,s_b4;} S_un_b;
  struct{ unsigned short s_w1, s_w2;} S_un_w;
  unsigned long S_addr;
  } S_un;
  } IN_ADDR;

一般我们这样用
addrs.sin_addr.S_addr=inet_addr("192.168.1.110")
函数inet_addr能够把字符串直接转换成in_addr类型
也能够使用inet_ntoa()将in_addr类型转换为字符串类型
參数namelen即为name的长度 即sizeof(name)
举个典型的样例:
SOCKET s;
s=socket(AF_INET,SOCK_STREAM,IPPOTO_TCP);
sockaddr_in addrbindto;
addrbindto.sin_family=AF_INET;
addrbindto.port=htons(8001) htons是将u_short类型转换为网络字节序,htonl是将u_long类型转换为网络字节序
addrbindto.sin_addr_S_addr=inet_addr("192.168.1.110")
bind(s,(
const struct sockaddr
)&addrbindto,sizeof(addrbindto))


3.listen

The Windows Sockets listen function places a socket a state where it is listening for an incoming connection.

int listen(
  SOCKET s,    
  int backlog  
);

Parameters

s
[in] Descriptor identifying a bound, unconnected socket.
backlog
[in] Maximum length of the queue of pending connections. If set to SOMAXCONN, the underlying service provider responsible for socket s will set the backlog to a maximum reasonable value. There is no standard provision to obtain the actual backlog value.

Return Values

If no error occurs, listen returns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.

该函数在指定的套接字上监听连接请求,參数s为监听的套接字,backlog表示表示同意的最大连接数
举例:
listen(s,5);




4.connect

The Windows Sockets connect function establishes a connection to a specified socket.

int connect(
  SOCKET s,                          
  const struct sockaddr FAR *name,  
  int namelen                        
);

Parameters

s
[in] Descriptor identifying an unconnected socket.
name
[in] Name of the socket to which the connection should be established.
namelen
[in] Length of name
该函数对一个指定的套接字建立连接
參数s为想要建立连接的套接字
name为sockaddr类型的指针
namelen即为sizeof(name)

举例:
SOCKET s;
sockaddr_in conaddr;
s=socket(AF_INET,SOCK_STREAM,IPPOTO_TCP);
conaddr.sin_family=AF_INET;
conaddr.port=htons(8001) htons是将u_short类型转换为网络字节序,htonl是将u_long类型转换为网络字节序
conaddr.sin_addr_S_addr=inet_addr("192.168.1.110")
connect(s,(const struct sockaddr)&conaddr,sizeof(conaddr))



5.send

The Windows Sockets send function sends data on a connected socket.

int send(
  SOCKET s,              
  const char FAR *buf,  
  int len,               
  int flags              
);

Parameters

s
[in] Descriptor identifying a connected socket.
buf
[in] Buffer containing the data to be transmitted.
len
[in] Length of the data in buf.
flags
[in] Indicator specifying the way in which the call is made. 

该函数用于向已连接的套接字发送数据
參数s为一个连接到的套接字
參数buf为发送数据的缓存
參数len为发送缓存数据的长度

举例:
char buf[20]="hello123";
send(s,(const char *)buf,sizeof(buf))



5.recv

The Windows Sockets recv function receives data from a connected socket.

int recv(
  SOCKET s,       
  char FAR *buf,  
  int len,        
  int flags       
);

Parameters

s
[in] Descriptor identifying a connected socket.
buf
[out] Buffer for the incoming data.
len
[in] Length of buf

该函数为接受函数:
參数与send类型,不在赘述



6. recvfrom,sendto
这两个函数功能与send与recv类似,仅仅是它们用于UDP通信中



OK,主要函数先介绍到这儿,等下篇博文我们亲自己主动手来写一个基于TCP的简单CS通信小Demo

windows套接字相关函数

标签:

原文地址:http://www.cnblogs.com/bhlsheji/p/5404355.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!