windows套接字相关函数
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.
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.
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两个数据结构保持大小相同而保留的空字节。
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;
htons是将u_short类型转换为网络字节序,htonl是将u_long类型转换为网络字节序
bind(s,(
const struct sockaddr
)&addrbindto,sizeof(addrbindto))
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表示表示允许的最大连接数
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.
该函数对一个指定的套接字建立连接
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.
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.
windows套接字相关函数的更多相关文章
- 缺少网络连接需要的Windows套接字注册表项(浏览器无法连网)
缺少网络连接需要的Windows套接字注册表项(浏览器无法连网) CreateTime--2018年4月25日14:17:42 Author:Marydon 1.异常信息 此计算机上缺少一个或多个 ...
- 基于Windows的套接字相关函数及示例
链接ws2_32.lib库 头文件#include <winsock2.h> int WSAStartup(WORD wVersionRequested,LPWSADATA lpWSADa ...
- Windows套接字Socket函数
1.使用套接字函数之前,先要加载套接字函数库: #include "Winsock2.h" #pragma comment(lib,"Ws2_32.lib") ...
- windows套接字阻塞模式编程实例
一.阻塞模式套接字服务端和客户端的运行流程如下: 1.1 服务器运行过程如下: 1.服务器启动后,等待客户端的连接请求.2.当收到客户端的请求后,在界面上显示该客户端的IP地址和端口,以及“Hello ...
- Windows进程间通讯(IPC)----套接字
Windows套接字 Windows套接字即socket,通过socket可以实现在不同的进程间通信,甚至这两个进程可以不在同一个计算机中. Winsock使用步骤 服务端 socket初始化 创建套 ...
- 套接字编程(VC_Win32)
简介(源于维基) Berkeley套接字(也作BSD套接字应用程序接口)刚开始是4.2BSD Unix操作系统(于1983发布)的一套应用程序接口.然而,由于AT&T的专利保护着UNIX,所以 ...
- VC远控(一)界面设计及套接字连接测试
首先创建一个MFC项目. 选择基于对话框: 勾选Windows套接字 依次拉上各种不同的控件: Edit Control.Button.Tree.Static Text.List.Progress C ...
- CAsyncSocket create创建套接字失败
解决方法: 在继承CAsyncSocket 类的子类的构造函数中加入以下代码: if (!AfxSocketInit()) AfxMessageBox(IDP_SOCKETS_INIT_FAILED) ...
- C项目实践--网络协议和套接字编程
1.TCP/IP协议 TCP/IP协议是一组包括TCP协议和IP协议,UDP(User Datagram Protocol)协议,ICMP(Internet Control Message Proto ...
随机推荐
- linux中UUID和LABLE
一.UUID 第一次看到UUID这个东西,是在Ubuntu系统中看到/boot/grub/grub.cfg中对kernel的配置:linux /boot/vmlinuz-2.6.31-14-ge ...
- HTTP缓存 1.0 vs 1.1
在“使用ETag跟踪用户”中有一点被忽略了,因为要用这张小图统计统计uv, 所以要求浏览器必须每次都要发送这个图片的请求.这需要服务器对图片的缓存策略做设置. http/1.0 和 http/1.1 ...
- Android学习笔记__2__Android工程目录结构
一.创建Android工程HelloWorld . src 文件夹里的是源文件 . Android2.2 是引用的类库,这些和 java 的都一样 . gen里面的类就是 ADT 自动生成的啦,一般只 ...
- NoSQL 简介
NoSQL(NoSQL = Not Only SQL ),意即"不仅仅是SQL". 在现代的计算系统上每天网络上都会产生庞大的数据量. 这些数据有很大一部分是由关系数据库管理系统( ...
- VS2008快捷键_大全
Ctrl+B,C: 清除全部标签 Ctrl+I: 渐进式搜索 Ctrl+Shift+I: 反向渐进式搜索 Ctrl+F: 查找 Ctrl+Shift+F: 在文件中查找 F3: 查找下一个 Shift ...
- 利用Unicorn和Idaemu辅助解决Geekpwn SecretCode
在前面的些文章里,我提到了怎么交叉编译Unicorn-engine,以及在windows上使用Unicorn python bindings进行分析程序.这一次我介绍下如何使用Unicorn-engi ...
- 适用于CSS2的各种运动的javascript运动框架
<script> window.onload = function() { //var oDiv1 = document.getElementById('box1'); //var oDi ...
- Fix an “Unapproved Caller” SecurityAgent Message in Mac OS X
上午一进公司就被日本分公司的美女呼叫,说mac硬盘加密经常开机后需要输入硬盘加密密码才可以登录,我想应该是硬盘加密后没有给用户添加许可证,所以每次登录系统都要进行验证.于是远程到用户电脑上后,准备在硬 ...
- jquery中的 ajax 以及map遍历
1.语法 $.ajax{ type:'get',//类型 有get post url:'',//路径 data:{name:$('#ma').val(),nameq:$('#maq').val()}, ...
- jsp文件上传
整理了一下jsp的上传下载,由客户端到服务端,包括一些常规的业务 一客户端 先看最基本的情况 1 在表单设置multipart/form-data,通过提交键把数据和附件一次性提交的.服务器更多的是对 ...