原文转自 http://blog.csdn.net/supersonico/article/details/18900319
一.用VC++来编写TCP 服务器/客户端的简单程序。
注意事项:
1.要加载ws2_32.lib链接库。
项目->属性->弹出属性页->连接器->输入->附加依赖项
2.要使用#include <WinSock2.h>头文件。
实现步骤:
1.服务器端:
①加载套接字库 (可以直接从MSDN上复制 WSAStartup)
②创建socket
③绑定socket(bind)
④将socket设置成监听状态(listen)
⑤等待客户端的访问(accept)
⑥接收客户端信息(recv)
⑦往客户端发送信息(send)
⑧关闭套接字(closesocket)
⑨清除套接字库(WSACleanup)
#include <iostream>
#include <WinSock2.h>
#include <stdlib.h>
using namespace std; void main()
{
//加载套接字
WORD wVersionRequested;
WSADATA wsaData;
int err; wVersionRequested = MAKEWORD(, ); err = WSAStartup(wVersionRequested, &wsaData);
if (err != )
{
return;
}
if (LOBYTE(wsaData.wVersion) != || HIBYTE(wsaData.wVersion) != )
{
WSACleanup();
return;
} //创建用于监听的套接字
SOCKET sockSrv = socket(AF_INET, SOCK_STREAM, ); SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(); //绑定套接字
bind(sockSrv, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
//将套接字设置为监听模式
listen(sockSrv, ); SOCKADDR_IN addrClient;
int len = sizeof(SOCKADDR); while ()
{
//等待客户的请求到来
SOCKET sockConn = accept(sockSrv, (SOCKADDR*)&addrClient, &len);
char sendBuf[];
sprintf_s(sendBuf, , "Welcome %s", inet_ntoa(addrClient.sin_addr)); //发送数据
send(sockConn, sendBuf, strlen(sendBuf) + , );
char recvBuf[];
//接受数据
recv(sockConn, recvBuf, , );
//打印接受的数据
printf("%s\n", recvBuf);
//关闭套接字
closesocket(sockConn);
}
}
2.客户端:
①加载套接字库
②向服务器发送请求 connect
③接受服务器的信息 recv
④向服务器发送信息 send
⑤关闭套接字 closesocket
⑥清除套接字库 WSACleanup
#include <iostream>
#include <WinSock2.h>
#include <stdlib.h>
using namespace std; void main()
{
//加载套接字
WORD wVersionRequested;
WSADATA wsaData;
int err; wVersionRequested = MAKEWORD(, ); err = WSAStartup(wVersionRequested, &wsaData);
if (err != )
{
return;
}
if (LOBYTE(wsaData.wVersion) != || HIBYTE(wsaData.wVersion) != )
{
WSACleanup();
return;
} //创建套接字
SOCKET socketClient = socket(AF_INET, SOCK_STREAM, );
//向服务器发送请求
SOCKADDR_IN sockAddrClient;
sockAddrClient.sin_addr.S_un.S_addr = inet_addr("120.77.57.44");
sockAddrClient.sin_family = AF_INET;
sockAddrClient.sin_port = htons(); if (SOCKET_ERROR == connect(socketClient, (SOCKADDR*)&sockAddrClient, sizeof(SOCKADDR)))
{
closesocket(socketClient);
WSACleanup();
return;
}
//接受信息
char recvBuf[];
recv(socketClient, recvBuf, , );
printf("%s\n", recvBuf);
//发送数据
send(socketClient, "This is lisi", strlen("This is lisi") + , );
//关闭套接字
closesocket(socketClient);
WSACleanup();
cin.get();
cin.get();
}
二.用VC++来编写UDP 服务器/客户端的简单程序。
1.服务器端:
①加载套接字库(WSAstartup)
②创建socket
③绑定socket(bind)
④接受从客户端发来的信息(recvfrom)
⑤关闭套接字(closesocket)
⑥清空套接字库(WSAClearup)
#include <iostream>
#include <stdlib.h>
#include <WinSock2.h> using namespace std; void main()
{
//加载套接字
WORD wVersionRequested;
WSADATA wsaData;
int err; wVersionRequested = MAKEWORD(,); err = WSAStartup(wVersionRequested,&wsaData);
if (err != )
{
return ;
}
if (LOBYTE(wsaData.wVersion)!= || HIBYTE(wsaData.wVersion)!=)
{
WSACleanup();
return ;
} //创建套接字
SOCKET sockSrv = socket(AF_INET,SOCK_DGRAM,); SOCKADDR_IN sockAddrSrv;
sockAddrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
sockAddrSrv.sin_family = AF_INET;
sockAddrSrv.sin_port = htons();
//绑定套接字
bind(sockSrv,(SOCKADDR*)&sockAddrSrv,sizeof(SOCKADDR)); SOCKADDR_IN sockAddrClient;
int len = sizeof(SOCKADDR);
char recvBuf[];
//接受客户端信息
recvfrom(sockSrv,recvBuf,,,(SOCKADDR*)&sockAddrClient,&len);
printf("%s\n",recvBuf); //关闭套接字
closesocket(sockSrv);
//清除套接字库
WSACleanup();
cin.get();
cin.get();
}
2.客户端:
①加载套接字库 (WSAstartup)
②创建socket
③发送数据(sendto)
④关闭套接字(closesocket)
⑤清空套接字库(WSAClearup)
#include <iostream>
#include <stdlib.h>
#include <WinSock2.h> using namespace std; void main()
{
//加载套接字
WORD wVersionRequested;
WSADATA wsaData;
int err; wVersionRequested = MAKEWORD(,); err = WSAStartup(wVersionRequested,&wsaData);
if (err != )
{
return ;
}
if (LOBYTE(wsaData.wVersion)!= || HIBYTE(wsaData.wVersion)!=)
{
WSACleanup();
return ;
}
//创建套接字
SOCKET sockClient = socket(AF_INET,SOCK_DGRAM,); SOCKADDR_IN sockAddrSrv;
sockAddrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
sockAddrSrv.sin_family = AF_INET;
sockAddrSrv.sin_port = htons(); //char sendBuf[100];
sendto(sockClient,"Hello",strlen("Hello")+,,(SOCKADDR*)&sockAddrSrv,sizeof(SOCKADDR));
closesocket(sockClient);
WSACleanup();
cin.get();
cin.get();
}