- UID
- 23
- 精华
- 积分
- 60
- 威望
- 点
- 宅币
- 个
- 贡献
- 次
- 宅之契约
- 份
- 最后登录
- 1970-1-1
- 在线时间
- 小时
|
制作文件传输器,云大可达10+MB/S速度(正在编写)服务器源码:
注意://需要将结构SOCKADDR_IN6的sin6_addr.s6_addr调整为u_char[20];否则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[i*2],buf[i*2+1]);
- }
- sprintf(str,"%s%x%x",str,buf[14],buf[15]);
- 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[20];否则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[1024];
- recv(childs,buf,1024,0);
- printf("收到数据:%s\n",buf);
- send(childs,"OK",sizeof("OK"),0);
- closesocket(s);
- closesocket(childs);
- WSACleanup();
- }
- }
- }
- }
- return 0;
- }
复制代码 |
|