`
AquariusM
  • 浏览: 147926 次
  • 性别: Icon_minigender_1
  • 来自: 南阳
社区版块
存档分类
最新评论

2010年12月06号之-----Win32 socket编程学习

阅读更多

几个基本函数的解释:

 

fd_set Structure:

 

The fd_set structure is used by various Windows Sockets functions and service providers, such as the select function, to place sockets into a "set" for various purposes, such as testing a given socket for readability using the readfds parameter of the select function.

fd_set结构简单的来说就是用来存储各种不同类型的Windows Socket的一个数据结构。

原型是:

 

typedef struct fd_set {
  u_int fd_count;
  SOCKET fd_array[FD_SETSIZE];
} fd_set;

 

 

select Function:

 

The select function determines the status of one ore more sockets,waiting if necessary, to perform synchronous I/O.

select 函数用来判断一个或更多个socket的状态,来决定是否有必要来执行同步I/O操作。

函数原型是:

int select(
  __in          int nfds,
  __in_out      fd_set* readfds,
  __in_out      fd_set* writefds,
  __in_out      fd_set* exceptfds,
  __in          const struct timeval* timeout
);

Return Value:

The select function returns the total number of socket handles that are ready and contained in the fd_set structures, zero if the time limit expired, or SOCKET_ERROR if an error occurred. If the return value is SOCKET_ERROR, WSAGetLastError can be used to retrive a specific error code.

 

 

Remarks

The select function is used to determine the status of one or more sockets. For each socket, the caller can request information on read, write, or error status. The set of sockets for which a given status is requested is indicated by an fd_set structure. The sockets contained within the fd_set structures must be associated with a single service provider. For the purpose of this restriction, sockets are considered to be from the same service provider if the WSAPROTOCOL_INFO structures describing their protocols have the same providerId value. Upon return, the structures are updated to reflect the subset of these sockets that meet the specified condition. The select function returns the number of sockets meeting the conditions. A set of macros is provided for manipulating an fd_set structure. These macros are compatible with those used in the Berkeley software, but the underlying representation is completely different.

The parameter readfds identifies the sockets that are to be checked for readability. If the socket is currently in the listen state, it will be marked as readable if an incoming connection request has been received such that an accept is guaranteed to complete without blocking. For other sockets, readability means that queued data is available for reading such that a call to recv, WSARecv, WSARecvFrom, or recvfrom is guaranteed not to block.

For connection-oriented sockets, readability can also indicate that a request to close the socket has been received from the peer. If the virtual circuit was closed gracefully, and all data was received, then a recv will return immediately with zero bytes read. If the virtual circuit was reset, then a recv will complete immediately with an error code such as WSAECONNRESET. The presence of OOB data will be checked if the socket option SO_OOBINLINE has been enabled (see setsockopt).

The parameter writefds identifies the sockets that are to be checked for writability. If a socket is processing a connect call (nonblocking), a socket is writeable if the connection establishment successfully completes. If the socket is not processing a connect call, writability means a send, sendto, or WSASendto are guaranteed to succeed. However, they can block on a blocking socket if the len parameter exceeds the amount of outgoing system buffer space available. It is not specified how long these guarantees can be assumed to be valid, particularly in a multithreaded environment.

The parameter exceptfds identifies the sockets that are to be checked for the presence of OOB data or any exceptional error conditions.

Note  Out-of-band data will only be reported in this way if the option SO_OOBINLINE is FALSE. If a socket is processing a connect call (nonblocking), failure of the connect attempt is indicated in exceptfds (application must then call getsockopt SO_ERROR to determine the error value to describe why the failure occurred). This document does not define which other errors will be included.

Any two of the parameters, readfds, writefds, or exceptfds, can be given as null. At least one must be non-null, and any non-null descriptor set must contain at least one handle to a socket.

 

timeval Structure:

The timeval structure is used to specify time values. It is associated with the Berkeley Software Distribution (BSD) Time.h header file.

 

typedef struct timeval {
  long tv_sec;
  long tv_usec;
} timeval;

这个也比较常用,第一个类型tv_sec秒值,第二个usec毫秒值。

 

Send Function :

The send function sends data on a connected socket.

 

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

Return Value

If no error occurs, send returns the total number of bytes sent, which can be less than the number requested to be sent in the len parameter. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.

 

如果没有错误发生,就返回被传递的总字节数。

 

分享到:
评论

相关推荐

    Socket网络编程-文件上传

    Socket网络编程-文件上传Socket网络编程-文件上传Socket网络编程-文件上传Socket网络编程-文件上传Socket网络编程-文件上传Socket网络编程-文件上传Socket网络编程-文件上传Socket网络编程-文件上传Socket网络编程-...

    Win32_网络编程 socket vs2008

    在IT领域,网络编程是构建分布式系统和网络应用程序的基础,而Windows平台上的网络编程往往涉及到Win32 API和套接字(socket)技术。Visual Studio 2008是一款广泛使用的开发工具,它支持多种编程语言,包括C++,...

    win-socket-demo_socket编程_socket_windows_DEMO_

    标题“win-socket-demo_socket编程_socket_windows_DEMO_”指的是一个Windows平台上的Socket编程示例项目,主要涉及网络通信的基础知识,尤其是如何在Windows环境下使用Socket API进行客户端和服务器端的交互。...

    Win32项目 C++利用socket实现C/S客户端间群聊和私聊

    在本文中,我们将深入探讨如何在Win32平台上利用C++和Socket技术构建一个C/S架构的聊天应用程序,实现客户端间的群聊和私聊功能。首先,我们需要了解几个关键概念和关键技术。 1. **Win32 API**:Windows操作系统...

    华中科技大学-计算机通信与网络(Socket编程)-内含源文件和说明书.zip

    这个资源主要涵盖了华中科技大学计算机通信与网络课程中的Socket编程部分,它包括了学习资料、源代码示例以及相关说明文档。Socket编程是网络编程的核心技术,主要用于实现不同计算机之间的通信,通过创建套接字接口...

    Java多线程-Socket编程

    Java 多线程-Socket 编程 Java 多线程-Socket 编程是指在 Java 语言中使用多线程技术来实现网络编程,特别是使用 Socket 编程来实现客户端和服务器端的通信。在 Java 中,多线程可以使用 Thread 类和 Runnable 接口...

    socket测试工具sokit-1.3-win32-enu

    Socket测试工具Sokit是一款强大的网络通信工具,专为开发者设计,用于进行Socket级别的通信测试。在Windows 32位平台上运行的版本是sokit-1.3-win32-enu。这款工具支持多种功能,包括作为服务器端、客户端以及转发器...

    Linux网络编程-网络基础-socket编程-高并发服务器.pdf

    【Linux网络编程-网络基础-socket编程-高并发服务器】 在深入探讨Linux下的网络编程之前,我们首先要理解网络通信的基础概念——协议。协议是数据传输和解释的规则,它确保了不同设备之间的通信能顺利进行。例如,...

    HP-Socket文档

    HP-Socket 提供了大量的 Demo 示例如:PUSH 模型示例、PULL 模型示例、PACK 模型示例、性能测试示例以及其它编程语言示例,以便让使用者能方便快速地学习和使用 HP-Socket。 HP-Socket 的特点 * 高性能:HP-Socket...

    Python程序设计:TCP-Socket编程.pptx

    TCP-Socket编程 UDP-Socket编程 知识点:TCP-Socket编程 TCP-Socket编程 Socket含义 socket 的原意是“插座”,在计算机通信领域,socket 被翻译为“套接字”,它是计算机之间进行通信的一种约定或一种方式。通过 ...

    socket调试工具socket-1.0-win32-chs.zip

    Socket调试工具socket-1.0-win32-chs.zip是一个专为Windows 32位操作系统设计的TCP/IP套接字调试软件。这个压缩包包含了该工具的中文版本,方便国内用户使用。在IT行业中,特别是在网络编程领域,理解和熟练使用...

    hp-socket-dev

    "hp-socket-dev"是一个与HP(Hewlett-Packard)相关的网络库资源,主要涉及的是Socket编程。在IT行业中,Socket编程是网络通信的基础,它允许不同计算机间的进程通过网络进行通信。在这个资源包中,我们可以期待找到...

    lua socket 支持lua SOCKET编程

    Lua Socket是Lua编程语言的一个扩展库,专门用于网络通信,它提供了一套完整的TCP、UDP以及伪套接字(如管道和文件描述符)接口,让Lua程序员能够轻松地进行Socket编程。在本文中,我们将深入探讨Lua Socket的使用、...

    Win32 Socket API网络编程入门演示程序

    **Win32 Socket API网络编程入门演示程序** 在IT领域,网络编程是构建分布式系统、实现客户端-服务器通信的关键技术。对于Windows操作系统,Win32 Socket API(也称为Winsock)是开发网络应用程序的主要接口。这个...

    01-Linux网络编程-网络基础-socket编程-高并发服务器

    01_Linux网络编程-网络基础-socket编程-高并发服务器

    基于win32控制台的SOCKET通信

    在Windows操作系统环境下,使用Win32 API进行SOCKET通信是一种常见的网络编程方式。本项目提供了一个简单的基于Win32控制台的SOCKET通信实例,它展示了如何创建客户端和服务端进行双向数据交换的基础步骤。下面将...

    网络socket编程集锦

    网络socket编程集锦,程序经过Linux平台PC上用 GNU 的gcc 成功编译过,适合新手学习借鉴。

    Delphi-Cross-Socket-master.zip

    Delphi是一种强大的面向对象的编程环境,主要用于开发Windows和跨平台的应用程序。在这个"Delphi-Cross-Socket-master.zip"压缩包中,我们找到了一个针对Delphi的开源库,特别关注于跨平台的网络通信功能。这个库被...

    pywin32-228-cp27-cp27m-win_amd64.zip

    这个版本号228意味着它是PyWin32的第228次更新,通常会包含错误修复和新功能。 安装PyWin32时,需要注意以下几点: 1. **平台兼容性**:确保你的操作系统是64位Windows,并且你的Python环境也是64位的。如果使用32...

    HP-Socket下载

     为了让使用者能方便快速地学习和使用 HP-Socket,迅速掌握框架的设计思想和使用方法,特此精心制作了大量 Demo 示例(如:PUSH 模型示例、PULL 模型示例、性能测试示例以及其它编程语言示例)。HP-Socket 目前运行...

Global site tag (gtag.js) - Google Analytics