Windows 网络通讯开发
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
转载于:https://www.cnblogs.com/laohan1221/p/6244503.html
Windows 网络通讯开发相关推荐
- windows网络通讯端口
windows安全设置 一.windows网络通讯端口 1.端口分类 windows网络通讯端口可分为3大类: 1)公认端口(Well Known Ports):从0到1023,它们紧密绑定于一些服务 ...
- 【网络通讯开发系列】如何抓取终端设备的TLS报文(一)
文章目录 1 写在前面 2 需求分析 3 几个方法 4 原理分析 5 方法实践 5.1 工具准备 5.2 详细步骤 6 经验总结 7 参考链接 8 更多分享 作者:架构师李肯 一个专注于嵌入式IoT领 ...
- Socket网络通讯开发总结之:Java 与 C进行Socket通讯
先交待一下业务应用背景: 服务端:移动交费系统:基于C语言的Unix系统 客户端:增值服务系统:基于Java的软件系统 通迅协议:采用TCP/IP协议,使用TCP以异步方式接入 数据传输:基于Sock ...
- java c 网络_Socket网络通讯开发总结之:Java 与 C进行Socket通讯(转)
先交待一下业务应用背景: 服务端:移动交费系统:基于C语言的Unix系统 客户端:增值服务系统:基于Java的软件系统 通迅协议:采用TCP/IP协议,使用TCP以异步方式接入 数据传输:基于Sock ...
- 如何在Windows系统上用抓包软件Wireshark截获iPhone等网络通讯数据
http://www.jb51.net/os/windows/189090.html 今天给大家介绍一种如何在Windows操作系统上使用著名的抓包工具软件Wireshark来截获iPhone.iPa ...
- 用USB代替网络通讯(2)—自制开发板上实现的解决故障过程
任务动机:在自制RK3399开发板上实现基于openEuler的usbnet 任务描述:在两个RK3399板子上分别烧录openEuler系统,编译内核实现usbnet的通信.通过比较不同开发板的电路 ...
- IOCP 网络通讯模型源码解读
From: http://hi.baidu.com/tsingsing/item/1aa5062fa27791fa50fd87b7 以前写服务器的时候用的是iocp,最近偶然发现windows的 网络 ...
- 【网络编程开发系列】好端端的MQTT-broker重新部署后居然出现TLS握手失败了
摘要:本文通过一次真实的现网案例复盘,深度还原TLS握手问题的排查思路和方法,希望对广大读者有所启发和帮助. 文章目录 1 写在前面 2 问题描述 2.1 项目背景 2.2 现场问题 3 场景复现 3 ...
- Linux、Windows网络工程师面试题精选
1.请你修改一下LINUX的视频驱动和声音驱动? 答: redhatlinux中用sndconfig来设置声卡,如果没有某个模块,就需要重新编译内核(编译最新发布的linux 内核),如果还不行,只好 ...
最新文章
- android中常用的快捷键
- 平正真诚——记红帆公司2011年秋季旅游·衡山
- 浅谈 PHP 与手机 APP 开发(API 接口开发)
- ASP.NET 4学习笔记(1) SQL注入攻击及解决方案.
- IDEA中进行SpringBoot整合spring-ws开发webservice接口后期要修改xsd文件流程
- “病毒防治”页面中“社区热帖”版块不显示
- SAP Spartacus cxOutlet里的元数据存储,outlet名称和待渲染Component的映射关系
- linux 查询 lib信息,怎么查看linux是否使用 libarchive
- 过去3个多月的1200个小时里,我收获了什么?| 2021年年中总结
- tfidf处理代码_tfidf.txt
- 飞鸽传书2007 一个自己开发的软件
- 爱数助力国资委实现混合IT环境下的业务保护
- 【java学习之路】(javaWeb篇)007.正则表达式专题
- 常用User-Agent大全 -《狗嗨默示录》-
- AD库:如何从立创商城得到自己想要的库
- thrift开源项目研究
- Java项目:人才求职招聘管理系统(java+SpringBoot+FreeMarker+JPA+Mysql)
- 服务器虚拟化太金苹果专业十,合作能力
- Nginx的安装使用----反向代理服务器
- 【此间乐,不思蜀】 大一不想咸鱼的暑假7.17