因工作需要在IPV6环境下测试收发TCP,UDP数据包,写了个小工具,在此记录一下。
* TCP - Server
WORD wVersionRequested;
WSADATA wsaData;
int nError;
wVersionRequested = MAKEWORD(1, 1);
nError = WSAStartup(wVersionRequested, &wsaData); //Socket 版本
if (nError != 0)
{
return -1;
}
int sockfd_V6, new_fd_V6, sockfd_V4, new_fd_V4;
socklen_t len;
struct sockaddr_in6 my_addr_V6, their_addr_V6;
struct sockaddr_in my_addr_V4, their_addr_V4;
char szBuf[MAX_BUF_10] = {'\0'};
if ((sockfd_V6 = socket(PF_INET6, SOCK_STREAM, 0)) == -1)//创建socket
{
AfxMessageBox(_T("Craete socket Error!"));
return -1;
}
memset(&my_addr_V6, 0 ,sizeof(sockaddr_in6));
memset(&their_addr_V6, 0 ,sizeof(sockaddr_in6));
my_addr_V6.sin6_family = PF_INET6;
my_addr_V6.sin6_port = htons(pDlg->m_nLocalPort); //端口号
my_addr_V6.sin6_addr = in6addr_any;
if (bind(sockfd_V6, (struct sockaddr *) &my_addr_V6, sizeof(struct sockaddr_in6)) == -1)
{
AfxMessageBox(_T("bind socket Error!"));
return -1;
}
if (listen(sockfd_V6, SOCKET_MAX_NUM) == -1)
{
AfxMessageBox(_T("listen socket Error!"));
return -1;
}
while(TRUE)
{
len = sizeof(sockaddr_in6);
memset(szBuf, 0, MAX_BUF_10);
if ((new_fd_V6 = accept(sockfd_V6, (SOCKADDR*) &their_addr_V6, &len)) == -1)
{
AfxMessageBox(_T("accept socket Error!"));
return -1;
}
len = recv(new_fd_V6, szBuf, MAX_BUF_10, 0);
if (pDlg->m_bShowMsgBox && len > 0)
{
CString str(szBuf);
AfxMessageBox(str);
}
}
* TCP - Client
WORD wVersionRequested;
WSADATA wsaData;
int nError;
wVersionRequested = MAKEWORD(1, 1);
nError = WSAStartup(wVersionRequested, &wsaData);
if (nError != 0)
{
return -1;
}
if (pDlg->m_strTCP_UDP == _T("TCP"))
{
int sockfd, len;
struct sockaddr_in dest_V4;
struct sockaddr_in6 dest_V6;
char szBuff[MAX_BUF_10] = {'\0'};
memset(&dest_V4, 0, sizeof(sockaddr_in));
memset(&dest_V6, 0, sizeof(sockaddr_in6));
if (pDlg->m_bIsIPV6)
{
if ((sockfd = socket(AF_INET6, SOCK_STREAM, 0)) < 0)
{
AfxMessageBox(_T("socket Error!"));
return -1;
}
dest_V6.sin6_family = AF_INET6;
dest_V6.sin6_port = htons(pDlg->m_nDestlPort); //server端口号
USES_CONVERSION;
inet_pton(AF_INET6, T2A(pDlg->m_strServerIPAddr), &dest_V6.sin6_addr); //转换server ip的CString到sin6_addr
if (connect(sockfd, (SOCKADDR*) &dest_V6, sizeof(sockaddr_in6)) != 0) {
AfxMessageBox(_T("connect Error!"));
return -1;
}
}
CString strData = pDlg->InitDataContent(); //发送数据内容
USES_CONVERSION;
len = send(sockfd, T2A(strData), strData.GetLength(), 0);
closesocket(sockfd);
}
* UDP - Server
WORD wVersionRequested;
WSADATA wsaData;
int nError;
wVersionRequested = MAKEWORD(1, 1);
nError = WSAStartup(wVersionRequested, &wsaData);
if (nError != 0)
{
return -1;
}
if (pDlg->m_bIsIPV6)
{
int socket_V6, len, addr_len_V6;
struct sockaddr_in6 addr_V6;
char szMsg[MAX_BUF_10];
if((socket_V6 = socket(AF_INET6,SOCK_DGRAM,0))<0)
{
AfxMessageBox(_T("socket Error!"));
return(1);
}
addr_len_V6=sizeof(struct sockaddr_in6);
memset(&addr_V6, 0, sizeof(sockaddr_in6));
addr_V6.sin6_family=AF_INET6;
addr_V6.sin6_port=htons(pDlg->m_nLocalPort); //端口号
addr_V6.sin6_addr=in6addr_any;
if(bind(socket_V6,(struct sockaddr *)&addr_V6,sizeof(addr_V6))<0)
{
AfxMessageBox(_T("bind Error!"));
return(1);
}
while(1)
{
memset(szMsg, 0, MAX_BUF_10);
len = recvfrom(socket_V6, szMsg, MAX_BUF_10,0,(struct sockaddr *)&addr_V6,(socklen_t*)&addr_len_V6);
if (pDlg->m_bShowMsgBox && len > 0)
{
CString str(szMsg);
AfxMessageBox(str);
}
}
}
* UDP - Client
WORD wVersionRequested;
WSADATA wsaData;
int nError;
wVersionRequested = MAKEWORD(1, 1);
nError = WSAStartup(wVersionRequested, &wsaData);
if (nError != 0)
{
return -1;
}
if (pDlg->m_bIsIPV6)
{
int mysocket,len;
int i=0;
struct sockaddr_in6 addr;
int addr_len;
char szMsg[MAX_BUF_10];
if((mysocket=socket(AF_INET6, SOCK_DGRAM, 0))<0)
{
AfxMessageBox(_T("socket Error!"));
return -1;
}
addr_len=sizeof(struct sockaddr_in6);
memset(&addr, 0, sizeof(addr));
addr.sin6_family=AF_INET6;
addr.sin6_port=htons(pDlg->m_nDestlPort); //server端口号
USES_CONVERSION;
inet_pton(AF_INET6, T2A(pDlg->m_strServerIPAddr), &addr.sin6_addr); //转换server的IP地址
CString strData = pDlg->InitDataContent(); //发送的数据内容
sendto(mysocket,T2A(strData),strData.GetLength(),0,(struct sockaddr *)&addr,addr_len);
closesocket(mysocket);
}