结构中的ai_addr 和ar_addrlen 字段调用绑定函数bind()。
while(ptr)
{
socklisten[i]=socket(ptr->ai_family,ptr->ai_socktype,ptr->ai_protocol );
if(socklisten[i]==INVALID_SOCKET){//创建失败处理;}
rc=bind(socklisten[i],ptr->ai_addr,ptr->ai_addrlen);
if(rc==SOCKET_ERROR){//绑定失败处理}
rc=listen(slisten[i],7)//开始监听
if(rc==SOCKET_ERROR){//监听失败处理}
i++;
ptr=ptr->ai_next;
}
。。。
//完成其他编程
???¢3ìDòêμày
?ú?aà?,??3?ò????ùóúIPV6μ??òμ¥??ó|(ECHO)·t???÷3ìDò.
1.?¨á¢CIPv6àà
// IPv6.h: í·???t£??aà?ê1ó?μ?á?ì×?ó×??Dμ?“select I/O?£Dí”
#define WIN32_LEAN_AND_MEAN????????
#include <winsock2.h>
#include <ws2tcpip.h>
#include <tpipv6.h>?????? //IPv6 í·???t
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#pragma comment(lib, "ws2_32.lib")//ì×?ó×??a???t
#define DEFAULT_PORT "7274" // ??è????ú
#define BUFFER_SIZE???? 64???? // êy?Y?o3???
class CIPv6
{
public:
??// ′′?¨TCP ·t???÷
??int CreateServer(char *Port = DEFAULT_PORT,char *Address = NULl??);
??void Usage(char *ProgName);//ó??§D??¢ìáê?
??LPSTR DecodeError(int ErrorCode);//??è?′í?óD??¢
??CIPv6();
??virtual??~CIPv6();
};
// IPv61.cpp: CIPv6ààμ?êμ?? .
// IPv61.cpp: implementation of the CIPv6 class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "IPv61.h"
int CIPv6::CreateServer(char *Port, char *Address)
{
??char Buffer[BUFFER_SIZE], Hostname[NI_MAXHOST];
??int RetVal??, FromLen, AmountRead;
??SOCKADDR_STORAGE From;
??WSADATA wsaData;
??ADDRINFO Hints, *AddrInfo;
??SOCKET ServSock;
??fd_set SockSet;
??// ???ˉWinsock
??if ((RetVal??= WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0)
??{
????fprintf(stderr, "WSAStartup failed with error %d: %s\n",
??????RetVal??, DecodeError(RetVal??));
????WSACleanup();
????return -1;
??}
??if (Port == NULl??)
??{
????Usage("Port Error");
??}
??memset(&Hints, 0, sizeof(Hints));
??Hints.ai_family =AF_INET6;// Family;
??Hints.ai_socktype =SOCK_STREAM;
??Hints.ai_flags = AI_NUMERICHOST | AI_PASSIVE;
??RetVal??= getaddrinfo(Address, Port, &Hints, &AddrInfo);
??if (RetVal??!= 0)
??{
????fprintf(stderr, "getaddrinfo failed with error %d: %s\n", RetVal??, gai_strerror(RetVal??));
????WSACleanup();
????return -1;
??}
??// ′′?¨ì×?ó×?
??ServSock = socket(AddrInfo->ai_family,AddrInfo->ai_socktype, AddrInfo->ai_protocol??);
??if (ServSock == INVALID_SOCKET)
??{
????fprintf(stderr, "socket() failed with error %d: %s\n",
??????WSAGetLastError(), DecodeError(WSAGetLastError()));
??????WSACleanup();
??????return -1;
??}
??// °ó?¨ì×?ó×?
??if (bind(ServSock, AddrInfo->ai_addr, AddrInfo->ai_addrlen) == SOCKET_ERROR)
??{
????fprintf(stderr,"bind() failed with error %d: %s\n",
??????WSAGetLastError(), DecodeError(WSAGetLastError()));
????WSACleanu
|