本文将简略描述我写的基于IPv6的语音通信程序。
一.背景
我们学校的校园网是按流量计费的,然而它只记IPv4流量,对于IPv6的流量是免费的,貌似国内高校教育网都是这样。于是我便萌生了将所有的耗费流量的网络操作都改写成IPv6的念头。第一个便是文件传输,做了三次改进,效果还是不理想,主要是我多线程方面的知识还比较欠缺,不过基本功能还是实现了的。留着将来有时间再改。第二个便是语音聊天。
二.程序界面
1. 服务器端
2. 客户端
三.准备知识
1. Winsock、WSAAsyncSelect模型及IPv6有关函数
winsock是Windows网络通信API库,我们使用winsock函数进行通信。WSAAsyncSelect是异步(非阻塞)通信模式中的一种,也是最简单的一种。我用这个就够了。
MSDN中对此函数描述如下:
WSAAsyncSelect Function
The WSAAsyncSelect function requestsWindows message-based notification of network events for a socket.
int WSAAsyncSelect(
__in SOCKET s,
__in HWND hWnd,
__in unsigned intwMsg,
__in long lEvent
);
具体描述可参看MSDN,重点了解最后一个参数。当然你也可以谷歌,还可以百度。对于以上三者,我的建议程度有强到弱依次排列。
其它所涉及的网络操作函数有bind(用于绑定地址),listen(用于监听套接字),accept(用于接受套接字连接),recv(用于接收数据),send(用于发送数据),socket(用于创建套接字),closesocket(用于关闭套接字)。还有一个比较重要的函数——getaddrinfo,它的使用关系到我所编写的程序是否有存在的意义。
依然先上MSDN描述:
getaddrinfo Function
The getaddrinfo function providesprotocol-independent translation from an ANSI host name to an address.
int WSAAPI getaddrinfo(
__in const char* nodename,
__in const char* servname,
__in const struct addrinfo* hints,
__out structaddrinfo** res
);
Parameters
nodename
A pointer to a NULL-terminated ANSIstring that contains a host (node) name or a numeric host address string. Forthe Internet protocol, the numeric host address string is a dotted-decimal IPv4address or an IPv6 hex address.
servname
A pointer to a NULL-terminated ANSIstring that contains either a service name or port number represented as astring.
hints
A pointer to an addrinfo structure thatprovides hints about the type of socket the caller supports. See Remarks.
res
A pointer to a linked list of on