ipv6实现tcp编程示例
开发测试环境:WIN7SP、 VS2005
TcpServer.cpp
#include <iostream>
#include <WinSock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
int main(int argc, char** argv)
{
using namespace std;
cout<<"Server Start..."<<endl;
WORD wVersion = 0;
WSADATA wsaData;
memset(&wsaData, 0, sizeof(wsaData));
wVersion = MAKEWORD(2, 2);
if (0 != WSAStartup(wVersion, &wsaData))
{
return -1;
}
if (2 != LOBYTE(wsaData.wVersion) || 2 != HIBYTE(wsaData.wVersion))
{
WSACleanup();
return -1;
}
struct addrinfo hints;
struct addrinfo* res = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
if (0 != getaddrinfo(NULL, "6677", &hints, &res))
{
WSACleanup();
return -1;
}
SOCKET sk = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
bind(sk, res->ai_addr, res->ai_addrlen);
if (0 != listen(sk, 5))
{
WSACleanup();
closesocket(sk);
return -1;
}
struct sockaddr_storage storage;
int len = sizeof(storage);
memset(&storage, 0, len);
SOCKET newSk = accept(sk, (struct sockaddr*)&storage, &len);
closesocket(sk);
char recvBuf[1024] = {'\0'};
int lenRecv = 0;
lenRecv = recv(newSk, recvBuf, 1024, 0);
cout<<"Recv From Client:"<<recvBuf<<" Len:"<<lenRecv<<endl;
send(newSk, recvBuf, lenRecv, 0);
WSACleanup();
closesocket(newSk);
return 0;
}
TcpClient.cpp
#include <iostream>
#include <WinSock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
int main(int argc, char** argv)
{
using namespace std;
cout<<"Client Start..."<<endl;
WORD wVersion = 0;
WSADATA wsaData;
memset(&wsaData, 0, sizeof(wsaData));
wVersion = MAKEWORD(2, 2);
if (0 != WSAStartup(wVersion, &wsaData))
{
return -1;
}
if (2 != LOBYTE(wsaData.wVersion) || 2 != HIBYTE(wsaData.wVersion))
{
WSACleanup();
return -1;
}
struct addrinfo hints;
struct addrinfo* res = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_CANONNAME;
if (0 != getaddrinfo("123.123.123.19", "6677", &hints, &res))
{
WSACleanup();
return -1;
}
SOCKET sk = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (0 != connect(sk, res->ai_addr, res->ai_addrlen))
{
WSACleanup();
closesocket(sk);
return -1;
}
char sendBuf[1024] = {"abcdef"};
char recvBuf[1024] = {'\0'};
int lenRecv = 0;
send(sk, sendBuf, strlen(sendBuf), 0);
lenRecv = recv(sk, recvBuf, 1024, 0);
cout<<"Recv From Server:"<<recvBuf<<" Len:"<<lenRecv<<endl;
WSACleanup();
closesocket(sk);
return 0;
}
您可能感兴趣的文章
- 01-10数据结构课程设计-用栈实现表达式求值的方法详解
- 01-10使用OpenGL实现3D立体显示的程序代码
- 01-10求斐波那契(Fibonacci)数列通项的七种实现方法
- 01-10C语言 解决不用+、-、&#215;、&#247;数字运算符做加法
- 01-10使用C++实现全排列算法的方法详解
- 01-10用C++实现DBSCAN聚类算法
- 01-10深入全排列算法及其实现方法
- 01-10全排列算法的非递归实现与递归实现的方法(C++)
- 01-10用C语言实现单链表的各种操作(一)
- 01-10用C语言实现单链表的各种操作(二)
阅读排行
本栏相关
- 04-02c语言函数调用后清空内存 c语言调用
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言的正则匹配函数 c语言正则表达
- 04-02c语言用函数写分段 用c语言表示分段
- 04-02c语言中对数函数的表达式 c语言中对
- 04-02c语言编写函数冒泡排序 c语言冒泡排
- 04-02c语言没有round函数 round c语言
- 04-02c语言分段函数怎么求 用c语言求分段
- 04-02C语言中怎么打出三角函数 c语言中怎
- 04-02c语言调用函数求fibo C语言调用函数求
随机阅读
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 01-11ajax实现页面的局部加载
- 04-02jquery与jsp,用jquery
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法实例总结
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-10SublimeText编译C开发环境设置