为了正常的体验网站,请在浏览器设置里面开启Javascript功能!
首页 > VC中网络编程的常用函数及UDP,TCP协议编程步骤

VC中网络编程的常用函数及UDP,TCP协议编程步骤

2018-04-10 15页 doc 46KB 17阅读

用户头像

is_769014

暂无简介

举报
VC中网络编程的常用函数及UDP,TCP协议编程步骤VC中网络编程的常用函数及UDP,TCP协议编程步骤 1.SOCKET类型 SOCKET是socket套接字类型,在WINSOCK2.H中有如下定义: typedef unsigned int u_int; typedef u_int SOCKET; 可知套接字实际上就是一个无符号整型,它将被Socket环境管理和使用。套接字将被创建、设置、用来发送和接收数据,最后会被关闭。 2.WORD类型、MAKEWORD、LOBYTE和HIBYTE宏 WORD类型是一个16位的无符号整型,在WTYPES.H中被定义为: ...
VC中网络编程的常用函数及UDP,TCP协议编程步骤
VC中网络编程的常用函数及UDP,TCP编程步骤 1.SOCKET类型 SOCKET是socket套接字类型,在WINSOCK2.H中有如下定义: typedef unsigned int u_int; typedef u_int SOCKET; 可知套接字实际上就是一个无符号整型,它将被Socket环境管理和使用。套接字将被创建、设置、用来发送和接收数据,最后会被关闭。 2.WORD类型、MAKEWORD、LOBYTE和HIBYTE宏 WORD类型是一个16位的无符号整型,在WTYPES.H中被定义为: typedef unsigned short WORD; 其目的是提供两个字节的存储,在Socket中这两个字节可以示主版本号和副版本号。使用MAKEWORD宏可以给一个WORD类型赋值。例如要表示主版本号2,副版本号0,可以使用以下代码: WORD wVersionRequested; wVersionRequested = MAKEWORD( 2, 0 ); 注意低位内存存储主版本号2,高位内存存储副版本号0,其值为0x0002。使用宏LOBYTE可以读取WORD的低位字节,HIBYTE可以读取高位字节。 3. WSADATA类型和LPWSADATA类型 WSADATA类型是一个结构,描述了Socket库的一些相关信息,其结构定义如下: typedef struct WSAData { WORD wVersion; WORD wHighVersion; char szDescription[WSADESCRIPTION_LEN+1]; char szSystemStatus[WSASYS_STATUS_LEN+1]; unsigned short iMaxSockets; unsigned short iMaxUdpDg; char FAR * lpVendorInfo; } WSADATA; typedef WSADATA FAR *LPWSADATA; 值得注意的就是wVersion字段,存储了Socket的版本类型。LPWSADATA是WSADATA的指针类型。它们通过Socket的初始化函数WSAStartup读取出来。 4. WSAStartup函数 WSAStartup函数被用来初始化Socket环境,它的定义如下: int PASCAL FAR WSAStartup(WORD wVersionRequired, LPWSADATA lpWSAData); 其返回值为整型,调用方式为PASCAL(即标准类型,PASCAL等于__stdcall),参数有两个,第一个参数为WORD类型,指明了Socket的版本号,第二个参数为WSADATA类型members from key minority outreach, promote a comprehensive strictly administering the party into the branches, put it into every party member, give full play to each party organization fight fortress function, each the exemplary vanguard role of party members. Is to promote the inner-party education extend from the concentrated education to regular education, in the regular education efforts, real party ideology in the all party members and the party's construction through system. Is to boost the spirit of all party members, education guide all members of the party in their respective work positions, responsible and dependable officer, make contributions. Therefore, we must from the height of political and global, fully aware of "two" learning the importance of education, put "two" education as a major political task and practice well. All members to active participation, really into, in "learn, and do, and modified, and promoting" upper and lower really Kung Fu, effective put thought and action unified to XI General Secretary of important indicates spirit up, unified to Central and provincial of decision deployment up, solid do learning education the work, effective do good accountability, caught in place, see effectiveness, condensed up officer venture, and promoted water the work of powerful power. Second, accurately grasp the "two" educational focus ... Marxist positions, knowing that good talk show a strong belief in pursuit, history as spirit, sincere feelings for the people, pragmatic style of thinking, enhance the scientific level of the work, and to better promote our work. In terms of learning methods, units of the party organization, the party branch in accordance with the implementation plan, organization members planned and subtopics to learn to combine 错误~文档中没有指定样式的文字。 的指针。 若返回值为0,则初始化成功,若不为0则失败。 5.WSACleanup函数 这是Socket环境的退出函数。返回值为0表示成功,SOCKET_ERROR表示失败。 6.socket函数 socket的创建函数,其定义为: SOCKET PASCAL FAR socket (int af, int type, int protocol); 第一个参数为int af,代表网络地址族,目前只有一种取值是有效的,即AF_INET,代表internet地址族; 第二个参数为int type,代表网络协议类型,SOCK_DGRAM代表UDP协议,SOCK_STREAM代表TCP协议; 第三个参数为int protocol,指定网络地址族的特殊协议,目前无用,赋值0即可。 返回值为SOCKET,若返回INVALID_SOCKET则失败。 7.setsockopt函数(一般是没有用的) 这个函数用来设置Socket的属性,若不能正确设置socket属性,则数据的发送和接收会失败。定义如下: int PASCAL FAR setsockopt (SOCKET s, int level, int optname, const char FAR * optval, int optlen); 其返回值为int类型,0代表成功,SOCKET_ERROR代表有错误发生。 第一个参数SOCKET s,代表要设置的套接字; 第二个参数int level,代表要设置的属性所处的层次,层次包含以下取值:SOL_SOCKET代表套接字层次;IPPROTO_TCP代表TCP协议层次,IPPROTO_IP代表IP协议层次(后面两个我都没有用过); 第三个参数int optname,代表设置参数的名称,SO_BROADCAST代表允许发送广播数据的属性,其它属性可参考MSDN; 第四个参数const char FAR * optval,代表指向存储参数数值的指针,注意这里可能要使用reinterpret_cast类型转换; 第五个参数int optlen,代表存储参数数值变量的长度。 8.sockaddr_in、in_addr类型,inet_addr、inet_ntoa函数 sockaddr_in定义了socket发送和接收数据包的地址,定义: struct sockaddr_in { short sin_family; u_short sin_port; struct in_addr sin_addr; char sin_zero[8]; }; 其中in_addr的定义如下: struct in_addr { members from key minority outreach, promote a comprehensive strictly administering the party into the branches, put it into every party member, give full play to each party organization fight fortress function, each the exemplary vanguard role of party members. Is to promote the inner-party education extend from the concentrated education to regular education, in the regular education efforts, real party ideology in the all party members and the party's construction through system. Is to boost the spirit of all party members, education guide all members of the party in their respective work positions, responsible and dependable officer, make contributions. Therefore, we must from the height of political and global, fully aware of "two" learning the importance of education, put "two" education as a major political task and practice well. All members to active participation, really into, in "learn, and do, and modified, and promoting" upper and lower really Kung Fu, effective put thought and action unified to XI General Secretary of important indicates spirit up, unified to Central and provincial of decision deployment up, solid do learning education the work, effective do good accountability, caught in place, see effectiveness, condensed up officer venture, and promoted water the work of powerful power. Second, accurately grasp the "two" educational focus ... Marxist positions, knowing that good talk show a strong belief in pursuit, history as spirit, sincere feelings for the people, pragmatic style of thinking, enhance the scientific level of the work, and to better promote our work. In terms of learning methods, units of the party organization, the party branch in accordance with the implementation plan, organization members planned and subtopics to learn to combine union { struct { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b; struct { u_short s_w1,s_w2; } S_un_w; u_long S_addr; } S_un; 首先阐述in_addr的含义,很显然它是一个存储ip地址的联合体(忘记union含义的请看c++书),有三种表达方式: 第一种用四个字节来表示IP地址的四个数字; 第二种用两个双字节来表示IP地址; 第三种用一个长整型来表示IP地址。 给in_addr赋值的一种最简单是使用inet_addr函数,它可以把一个代表IP地址的字符串赋值转换为in_addr类型,如 addrto.sin_addr.s_addr=inet_addr("192.168.0.2"); 其反函数是inet_ntoa,可以把一个in_addr类型转换为一个字符串。 sockaddr_in的含义比in_addr的含义要广泛,其各个字段的含义和取值如下: 第一个字段short sin_family,代表网络地址族,如前所述,只能取值AF_INET; 第二个字段u_short sin_port,代表IP地址端口,由程序员指定; 第三个字段struct in_addr sin_addr,代表IP地址; 第四个字段char sin_zero[8],是为了保证sockaddr_in与SOCKADDR类型的长度相等而填充进来的字段。 9.sockaddr类型 sockaddr类型是用来表示Socket地址的类型,同上面的sockaddr_in类型相比,sockaddr的适用范围更广,因为sockaddr_in只适用于TCP/IP地址。Sockaddr的定义如下: struct sockaddr { u_short sa_family; char sa_data[14]; }; 可知sockaddr有16个字节,而sockaddr_in也有16个字节,所以sockaddr_in是可以强制类型转换为sockaddr的。事实上也往往使用这种方法。 10. Sleep函数 线程挂起函数,表示线程挂起一段时间。Sleep(1000)表示挂起一秒。定义于WINBASE.H头文件中。WINBASE.H又被包含于WINDOWS.H中,然后WINDOWS.H被WINSOCK2.H包含。 11. sendto,send函数 在Socket中有两套发送和接收函数,一是sendto和recvfrom;二是send和recv。前一套在函数参数中要指明地址(UDP协议);而后一套需要先将套接字和一个地址绑定,然后直接发送和接收,不需绑定地址。 sendto的定义如下: members from key minority outreach, promote a comprehensive strictly administering the party into the branches, put it into every party member, give full play to each party organization fight fortress function, each the exemplary vanguard role of party members. Is to promote the inner-party education extend from the concentrated education to regular education, in the regular education efforts, real party ideology in the all party members and the party's construction through system. Is to boost the spirit of all party members, education guide all members of the party in their respective work positions, responsible and dependable officer, make contributions. Therefore, we must from the height of political and global, fully aware of "two" learning the importance of education, put "two" education as a major political task and practice well. All members to active participation, really into, in "learn, and do, and modified, and promoting" upper and lower really Kung Fu, effective put thought and action unified to XI General Secretary of important indicates spirit up, unified to Central and provincial of decision deployment up, solid do learning education the work, effective do good accountability, caught in place, see effectiveness, condensed up officer venture, and promoted water the work of powerful power. Second, accurately grasp the "two" educational focus ... Marxist positions, knowing that good talk show a strong belief in pursuit, history as spirit, sincere feelings for the people, pragmatic style of thinking, enhance the scientific level of the work, and to better promote our work. In terms of learning methods, units of the party organization, the party branch in accordance with the implementation plan, organization members planned and subtopics to learn to combine 错误~文档中没有指定样式的文字。 int PASCAL FAR sendto (SOCKET s, const char FAR * buf, int len, int flags, const struct sockaddr FAR *to, int tolen); 第一个参数就是套接字; 第二个参数是要传送的数据指针; 第三个参数是要传送的数据长度(字节数); 第四个参数是传送方式的标识,如果不需要特殊则可以设置为0,其它值请参考MSDN; 第五个参数是目标地址,注意这里使用的是sockaddr的指针; 第六个参数是地址的长度; 返回值为整型,如果成功,则返回发送的字节数,失败则返回SOCKET_ERROR。 send函数有四个参数与sendto函数的前四个参数是一样的。 12.recvfrom,recv函数 int WSAAPI recvfrom(IN SOCKET s,char FAR * buf,int len,int flags,sockaddr FAR * from,int * fromlen ); 第一个参数就是套接字; 第二个参数是要接收消息的char数组的指针; 第三个参数是要接收的数据长度(字节数); 第四个参数是接收方式的标识,如果不需要特殊要求则可以设置为0; 第五个参数是接收地址的结构指针,存放接收到对方的的地址信息; 第六个参数是地址的长度的指针(&); 返回值为整型,如果成功,则返回接收的字节数,失败则返回SOCKET_ERROR。 recv函数有四个参数与recvfrom函数的前四个参数是一样的。 13. WSAGetLastError函数 该函数用来在Socket相关API失败后读取错误码,根据这些错误码可以对照查出错误原因。 14. closesocket 关闭套接字,其参数为SOCKET类型。成功返回0,失败返回SOCKET_ERROR。 15.bind函数 bind函数用来将一个套接字绑定到一个IP地址。一般只在服务方(即数据发送方)调用,很多函数会隐式的调用bind函数。 16.listen函数 从服务方监听客户方的连接。同一个套接字可以多次监听。 17.connect和accept函数 connect是客户方连接服务方的函数,而accept是服务方同意客户方连接的函数。这两个配套函数分别在各自的程序中被成功调用后就可以收发数据了。 总结以上,写一个UDP发送程序的步骤如下: members from key minority outreach, promote a comprehensive strictly administering the party into the branches, put it into every party member, give full play to each party organization fight fortress function, each the exemplary vanguard role of party members. Is to promote the inner-party education extend from the concentrated education to regular education, in the regular education efforts, real party ideology in the all party members and the party's construction through system. Is to boost the spirit of all party members, education guide all members of the party in their respective work positions, responsible and dependable officer, make contributions. Therefore, we must from the height of political and global, fully aware of "two" learning the importance of education, put "two" education as a major political task and practice well. All members to active participation, really into, in "learn, and do, and modified, and promoting" upper and lower really Kung Fu, effective put thought and action unified to XI General Secretary of important indicates spirit up, unified to Central and provincial of decision deployment up, solid do learning education the work, effective do good accountability, caught in place, see effectiveness, condensed up officer venture, and promoted water the work of powerful power. Second, accurately grasp the "two" educational focus ... Marxist positions, knowing that good talk show a strong belief in pursuit, history as spirit, sincere feelings for the people, pragmatic style of thinking, enhance the scientific level of the work, and to better promote our work. In terms of learning methods, units of the party organization, the party branch in accordance with the implementation plan, organization members planned and subtopics to learn to combine 1. 用WSAStartup函数初始化Socket环境; 2. 用socket函数创建一个套接字; 3. 用setsockopt函数设置套接字的属性,例如设置为广播类型;很多时候该步骤可以省略; 4.创建一个sockaddr_in,并指定其IP地址和端口号; 5. 用sendto函数向指定地址发送数据,不需要绑定,即使绑定了,其地址也会被sendto中的参数覆盖;若使用send函数则会出错,因为send是面向连接的,而UDP是非连接的,只能使用sendto发送数据; 6. 用closesocket函数关闭套接字; 7. 用WSACleanup函数关闭Socket环境。 与之类似,一个UDP接收程序的步骤如下,注意接收方一定要bind套接字: 1.用WSAStartup函数初始化Socket环境; 2.用socket函数创建一个套接字; 3.用setsockopt函数设置套接字的属性,例如设置为广播类型; 4.创建一个sockaddr_in,并指定其IP地址和端口号; 5.用bind函数将套接字与接收的地址绑定起来,然后调用recvfrom函数或者recv接收数据; 注意这里一定要绑定,因为接收报文的套接字必须在网络上有一个绑定的名称才能保证正确接收数据; 6.用closesocket函数关闭套接字; 7.用WSACleanup函数关闭Socket环境。 TCP与UDP最大的不同之处在于TCP是一个面向连接的协议,在进行数据收发之前TCP必须进行连接,并且在收发的时候必须保持该连接。 发送方的步骤如下(省略了Socket环境的初始化、关闭等内容): 1. 用socket函数创建一个套接字sock; 2. 用bind将sock绑定到本地地址; 3. 用listen侦听sock套接字; 4. 用accept函数接收客户方的连接,返回客户方套接字clientSocket; 5. 在客户方套接字clientSocket上使用send发送数据; 6. 用closesocket函数关闭套接字sock和clientSocket; 而接收方的步骤如下: 1. 用socket函数创建一个套接字sock; 2. 创建一个指向服务方的远程地址; 3. 用connect将sock连接到服务方,使用远程地址; 4. 在套接字上使用recv接收数据; 5. 用closesocket函数关闭套接字sock; members from key minority outreach, promote a comprehensive strictly administering the party into the branches, put it into every party member, give full play to each party organization fight fortress function, each the exemplary vanguard role of party members. Is to promote the inner-party education extend from the concentrated education to regular education, in the regular education efforts, real party ideology in the all party members and the party's construction through system. Is to boost the spirit of all party members, education guide all members of the party in their respective work positions, responsible and dependable officer, make contributions. Therefore, we must from the height of political and global, fully aware of "two" learning the importance of education, put "two" education as a major political task and practice well. All members to active participation, really into, in "learn, and do, and modified, and promoting" upper and lower really Kung Fu, effective put thought and action unified to XI General Secretary of important indicates spirit up, unified to Central and provincial of decision deployment up, solid do learning education the work, effective do good accountability, caught in place, see effectiveness, condensed up officer venture, and promoted water the work of powerful power. Second, accurately grasp the "two" educational focus ... Marxist positions, knowing that good talk show a strong belief in pursuit, history as spirit, sincere feelings for the people, pragmatic style of thinking, enhance the scientific level of the work, and to better promote our work. In terms of learning methods, units of the party organization, the party branch in accordance with the implementation plan, organization members planned and subtopics to learn to combine 错误~文档中没有指定样式的文字。 在这里套接字使用了最原始的套接字,并在很多地方直接使用了SDK函数,而尽量避免了MFC等代码框架,这是为了方便他人掌握技术的最基本内涵。 其实在具体的编程中,当然是怎么方便怎么来,Socket和多线程以及界面等功能都有大量方便可用的代码库,复用这些代码库会比自己动手写方便很多。但是,掌握了基本原理再使用这些库,事半功倍 。当然要编好网络程序还要对多线程及其同步有所熟悉,并应用到实际编程,以上只是最基本的。现在大家可以自己动手编写简单的网络程序了(有时要添加库文件,如ws2_32.lib) members from key minority outreach, promote a comprehensive strictly administering the party into the branches, put it into every party member, give full play to each party organization fight fortress function, each the exemplary vanguard role of party members. Is to promote the inner-party education extend from the concentrated education to regular education, in the regular education efforts, real party ideology in the all party members and the party's construction through system. Is to boost the spirit of all party members, education guide all members of the party in their respective work positions, responsible and dependable officer, make contributions. Therefore, we must from the height of political and global, fully aware of "two" learning the importance of education, put "two" education as a major political task and practice well. All members to active participation, really into, in "learn, and do, and modified, and promoting" upper and lower really Kung Fu, effective put thought and action unified to XI General Secretary of important indicates spirit up, unified to Central and provincial of decision deployment up, solid do learning education the work, effective do good accountability, caught in place, see effectiveness, condensed up officer venture, and promoted water the work of powerful power. Second, accurately grasp the "two" educational focus ... Marxist positions, knowing that good talk show a strong belief in pursuit, history as spirit, sincere feelings for the people, pragmatic style of thinking, enhance the scientific level of the work, and to better promote our work. In terms of learning methods, units of the party organization, the party branch in accordance with the implementation plan, organization members planned and subtopics to learn to combine
/
本文档为【VC中网络编程的常用函数及UDP,TCP协议编程步骤】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索