Windows 网络通讯开发

一、Windows网络开发API

  由于C++标准库中没有网络库,所以进行网络开发的时候要调用系统API。Windows通讯开发API包括以下几个基本函数及成员类型:

1. Header:Winsock.h 和 Winsock2.h(最新的)

具体Winsock2.h包含两大块内容:Socket Functions和Microsoft Windows-Specific Extension Functions

Socket Functions:

  • socket:An incoming connection is acknowledged and associated with an immediately created socket. The original socket is returned to the listening state.
SOCKET socket(int af,int type,int protocol);
  • bind:Assigns a local name to an unnamed socket.
  • listen:Listens for incoming connections on a specified socket.(一般在服务器端使用)
  • accept:An incoming connection is acknowledged(被确认) and associated with an immediately created socket. The original socket is returned to the listening state.(一般在服务器端使用)
  • connect:Initiates a connection on the specified socket.(一般在客户端使用)
  • send:Sends data to a connected socket.
  • sendto:Sends data to either a connected or unconnected socket.
  • recv:Receives data from a connected or unconnected socket.
  • recvfrom:Receives data from either a connected or unconnected socket.
  • htonl/htons/ntohl/ntohs:

    h代表:host-byte;n代表:network-byte;l代表:32-bit;s代表:16-bit;
  • inet_addr:Converts a character string representing a number in the Internet standard ".'' notation to an Internet address value.
  • inet-ntoa:Converts an Internet address value to an ASCII string in ".'' notation that is, "a.b.c.d''.

Microsoft Windows-Specific Extension





2. Library:Use Ws2_32.lib.

#pragma comment(lib, "ws2_32.lib")

3. 使用方法举例:

#include "stdafx.h"
#include <Winsock2.h>
#include <iostream>
using namespace std;
const int PORT = 10051;
int main(int argc, char* argv[])
{
WSADATA wd;
WSAStartup(0x202,&wd);
SOCKET sock = socket(AF_INET,SOCK_STREAM,0);//侦听套接字
if(sock == INVALID_SOCKET)
{
cout << "socket函数失败:" << WSAGetLastError() << endl;
return -1;
}
sockaddr_in sa = {AF_INET,htons(PORT)};
int n = bind(sock,(sockaddr*)&sa,sizeof(sa));
if(n == SOCKET_ERROR)
{
cout << "bind函数失败:" << WSAGetLastError() << endl;
return -1;
}
listen(sock,5); int nLen = sizeof(sa);
SOCKET socka = accept(sock,(sockaddr*)&sa,&nLen);//如何循环等待多个客户端同时连接进来
if(socka==INVALID_SOCKET)
{
cout << "accept函数失败:" << WSAGetLastError() << endl;
return -1;
}
cout << "有人连接进来:" << inet_ntoa(sa.sin_addr) << "-" << htons (sa.sin_port)<< endl;
char s[256];
while(( n = recv(socka,s,sizeof(s)-1,0)) > 0)
{
s[n] = 0;
cout << s << endl;
}
cout << WSAGetLastError() << endl;
return 0;
}

详情参见:MSDN October 2001->Platform SDK: Windows Sockets

二、CAsyncSocket Class

  • 定义:

    Class CAsyncSocket encapsulates the Windows Socket Functions API, providing an object-oriented abstraction for programmers who want to use Windows Sockets in conjunction with MFC.

    这个类封装了Window 套接字API,是比较底层的通讯类。
  • 主要通讯流程:

    To use a CAsyncSocket object, call its constructor, then call the Create function to create the underlying socket handle (type SOCKET), except on accepted sockets. For a server socket call the Listen member function, and for a client socket call the Connect member function. The server socket should call the Accept function upon receiving a connection request. Use the remaining CAsyncSocket functions to carry out communications between sockets. Upon completion, destroy the CAsyncSocket object if it was created on the heap; the destructor automatically calls the Close function. The SOCKET data type is described in the article Windows Sockets: Background.

详细使用情况参见Microsoft Help Veiwer 2.0

三、CSocket Class

Derives from CAsyncSocket and inherits its encapsulation of the Represents a higher level of abstraction of the Windows Sockets API than that of a CAsyncSocket object.

这个类是继承CAsyncSocket Class 的,它使用比其父类更加便捷。

详细使用情况参见Microsoft Help Veiwer 2.0

Windows 网络通讯开发的更多相关文章

  1. Socket网络通讯开发总结之:Java 与 C进行Socket通讯 + [备忘] Java和C之间的通讯

    Socket网络通讯开发总结之:Java 与 C进行Socket通讯 http://blog.sina.com.cn/s/blog_55934df80100i55l.html (2010-04-08 ...

  2. Socket网络通讯开发总结之:Java 与 C进行Socket通讯(转)

    先交待一下业务应用背景:服务端:移动交费系统:基于C语言的Unix系统客户端:增值服务系统:基于Java的软件系统通迅协议:采用TCP/IP协议,使用TCP以异步方式接入数据传输:基于Socket流的 ...

  3. windows 网络通讯模型Overlapped (转)(未看)

    https://blog.csdn.net/jofranks/article/details/7895316 https://blog.csdn.net/caoshiying/article/deta ...

  4. 《连载 | 物联网框架ServerSuperIO教程》-4.如开发一套设备驱动,同时支持串口和网络通讯。附:将来支持Windows 10 IOT

    1.C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍 <连载 | 物联网框架ServerSuperIO教程>1.4种通讯模式机制. <连载 | 物联网框架Serve ...

  5. 如何在Windows系统上用抓包软件Wireshark截获iPhone等网络通讯数据

    http://www.jb51.net/os/windows/189090.html 今天给大家介绍一种如何在Windows操作系统上使用著名的抓包工具软件Wireshark来截获iPhone.iPa ...

  6. C#利用VUDP.cs开发网络通讯应用例程

    VClassLib-CS项目Github地址:https://github.com/velscode/VClassLib-CS VUDP文档地址:https://github.com/velscode ...

  7. [dotnet core]使用Peach简化Socket网络通讯协议开发

    Peach是基于DotNetty的Socket网络通讯帮助类库,可以帮助开发者简化使用DotNetty,关于DotNetty可参考我之前的这篇文章. Peach内置实现了一个基于文本协议的Comman ...

  8. Windows phone应用开发[17]-xap提交异常处理

    在windows phone 应用提交操作上早在2011年时就写过一篇Windows phone 应用开发[4]-应用发布,那时wp应用提交官方市场的流程繁杂[超过了5步].因为上传和填写应用信息页面 ...

  9. DIOCP网络通讯流程

    DIOCP 运作核心探密   原文连接: http://blog.qdac.cc/?p=2362 原作者: BB 天地弦的DIOCP早已经广为人知了,有很多的同学都用上了它,甚至各种变异.修改版本也出 ...

随机推荐

  1. 延迟加载外部js文件,延迟加载图片(jquery.lazyload.js和echo,js)

    js里一说到延迟加载,大都离不开两种情形,即外部Js文件的延迟加载,以及网页图片的延迟加载: 1.首先简单说一下js文件的3种延迟加载方式: (1)<script type="text ...

  2. [HTML5] ArrayBuffer与类型化数组

    写在前面 这是关于JS二进制操作的第三篇博客,前两篇详见: [HTML5] Blob对象 [HTML5] FileReader对象 此前从宏观角度介绍了如何通过JS创建一个二进制对象,并介绍了如何将本 ...

  3. C++ 自由存储区是否等价于堆?

    "free store" VS "heap" 当我问你C++的内存布局时,你大概会回答: "在C++中,内存区分为5个区,分别是堆.栈.自由存储区.全 ...

  4. UOJ58 【WC2013】糖果公园

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  5. 【实践】Memcached实例解析

    一.关于Memcached Memcached是一个自由开源的,高性能,分布式内存对象缓存系统. Memcached是一种基于内存的Key-Value存储,用来存储小块的任意数据(字符串.对象).这些 ...

  6. JavaScript中的继承

    一.原型链(默认) function Person(){}; function Student(){}; Student.prototype = new Person(); Student.proto ...

  7. sqlserver多文件组数据库的备份和还原实战

    数据库文件过大时就要进行数据分区,就是讲数据库拆分到多个文件组中.已方便数据文件管理,提高数据库的读取效能,多文件组如何进行数据库的备份和还原呢,今天主要做多文件组数据库的备份和还原实验. 第一步 创 ...

  8. wifi万能钥pc版提示手机未连接

     关于PC版万能钥匙的用法 大部分人都是在“未连接到手机”再不知道怎么搞了 怎么连接到手机呢? 首先要把你的手机变成路由器 让电脑连上 这个都知道怎么搞吧 手机版万能钥匙有个一键让手机变成热点 再用电 ...

  9. 堆排序分析及php实现

    堆排序:是一种特殊形式的选择排序,他是简单选择排序的一种改进. 什么是堆? 具有n个元素的序列:{k1,k2,ki,…,kn} (ki <= k2i,ki <= k2i+1) 或者 (ki ...

  10. 第一章 Shiro简介——《跟我学Shiro》(转)

    目录贴:跟我学Shiro目录贴 1.1  简介 Apache Shiro是Java的一个安全框架.目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring Security,可 ...