【TCP/IP】IPV6 TCP的WinSocket实现。
制作文件传输器,云大可达10+MB/S速度(正在编写)服务器源码:注意://需要将结构SOCKADDR_IN6的sin6_addr.s6_addr调整为u_char;否则accept时产生10014错误
#include <stdio.h>
#include <winsock2.h>
#include <Ws2tcpip.h>
#include "tpipv6.h"
#pragma comment(lib,"ws2_32")//加载ws2_32lib库
char* IPV6AddressToString(u_char* buf)//将IPV6地址转换为标准形势,随意而作,存在内存泄露问题;
{
char* str = (char*)malloc(40);
memset(str,0,40);
for(int i=0;i<7;i++)
{
sprintf(str,"%s%x%x:",str,buf,buf);
}
sprintf(str,"%s%x%x",str,buf,buf);
return str;
}
int main()
{
WSADATA wsaData;
int reVel;
WSAStartup(MAKEWORD(1,1),&wsaData);
SOCKET s = socket(AF_INET6,SOCK_STREAM,IPPROTO_TCP);
if (s == INVALID_SOCKET)
{
printf("创建Socket失败.\n");
}else{
printf("创建Socket成功.\n");
addrinfo hints;
addrinfo* res = NULL;
memset(&hints,0,sizeof(hints));
hints.ai_family=AF_INET6;
hints.ai_socktype=SOCK_STREAM;
hints.ai_protocol=IPPROTO_TCP;
hints.ai_flags=AI_PASSIVE;
reVel = getaddrinfo("::1","3000",&hints,&res);
if(reVel !=0)
{
printf("getaddrinfo失败.\n");
}else{
printf("getaddrinfo成功.\n");
reVel = bind(s,res->ai_addr,res->ai_addrlen);
if(reVel !=0)
{
printf("bind失败.\n");
}else{
printf("bind成功.\n");
reVel = listen(s,1);
if(reVel !=0)
{
printf("listen失败.\n");
}else{
printf("listen成功.开始等待客户接入\n");
SOCKADDR_IN6 childadd;//需要将结构SOCKADDR_IN6的sin6_addr.s6_addr调整为u_char;否则accept时产生10014错误
int len = sizeof(SOCKADDR_IN6);
SOCKET childs = accept(s,(sockaddr *)&childadd,&len);
printf("用户进入成功:%s\n",IPV6AddressToString(childadd.sin6_addr.s6_addr));
char* buf = new char;
recv(childs,buf,1024,0);
printf("收到数据:%s\n",buf);
send(childs,"OK",sizeof("OK"),0);
closesocket(s);
closesocket(childs);
WSACleanup();
}
}
}
}
return 0;
} 客户端源码:#include <stdio.h>
#include <winsock2.h>
#include <Ws2tcpip.h>
#include "tpipv6.h"
#pragma comment(lib,"ws2_32")//加载ws2_32lib库
int main()
{
WSADATA wsaData;
int reVel;
WSAStartup(MAKEWORD(1,1),&wsaData);
SOCKET s = socket(AF_INET6,SOCK_STREAM,IPPROTO_TCP);
if (s == INVALID_SOCKET)
{
printf("创建Socket失败.\n");
}else{
printf("创建Socket成功.\n");
addrinfo hints;
addrinfo* res = NULL;
memset(&hints,0,sizeof(hints));
hints.ai_family=AF_INET6;
hints.ai_socktype=SOCK_STREAM;
hints.ai_protocol=IPPROTO_TCP;
hints.ai_flags=AI_PASSIVE;
reVel = getaddrinfo("::1","3000",&hints,&res);
connect(s,res->ai_addr,res->ai_addrlen);
send(s,"Hi,IPV6",sizeof("Hi,IPV6"),0);
char* buf = new char;
recv(s,buf,1024,0);
printf("收到数据:%s\n",buf);
closesocket(s);
WSACleanup();
}
}VC6.0源码下载:http://download.csdn.net/detail/u011910399/6854899 支持!顺带说一句……我可能会给你的代码进行重排版,不介意吧? 0xAA55 发表于 2014-3-19 20:41
支持!顺带说一句……我可能会给你的代码进行重排版,不介意吧?
没事,万恶的十个字
页:
[1]