他们是短连接的,服务器接受客户端之后,马上发送一个消息,发送完以后立即将客户端断开掉,然后继续等待下一个连接.
使用Winsocket2必须要引用到的头文件和需要包含到的链接库文件:
![None.gif](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
#include <WinSock2.h>
![None.gif](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
#pragma comment( lib, "ws2_32.lib" )
以下代码是Winsocket2的系统初始化和关闭的封装代码.
class WinSocketSystem
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/2f88ce130b654eb5dc6788e02dbcfc90.gif)
{
public:
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
WinSocketSystem()
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
{
int iResult = WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
if (iResult != NO_ERROR)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
{
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
exit(-1);
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
}
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
}
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
~WinSocketSystem()
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
{
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
WSACleanup();
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
}
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
protected:
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
WSADATA wsaData;
![ExpandedBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/0ac3a2d53663ec01c7f7225264eeefae.gif)
};
static WinSocketSystem g_winsocketsystem;
服务器端代码:
class TCPServer
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/2f88ce130b654eb5dc6788e02dbcfc90.gif)
{
public:
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
TCPServer()
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
: mServerSocket(INVALID_SOCKET)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
{
// 创建套接字
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
mServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if (mServerSocket == INVALID_SOCKET)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
{
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
std::cout << "创建套接字失败!" << std::endl;
return;
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
}
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
// 填充服务器的IP和端口号
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
mServerAddr.sin_family = AF_INET;
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
mServerAddr.sin_addr.s_addr = INADDR_ANY;
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
mServerAddr.sin_port = htons((u_short)SERVER_PORT);
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
// 绑定IP和端口
if ( ::bind(mServerSocket, (sockaddr*)&mServerAddr,
sizeof(mServerAddr)) == SOCKET_ERROR)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
{
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
std::cout << "绑定IP和端口失败!" << std::endl;
return;
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
}
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
// 监听客户端请求,最大同时连接数设置为10.
if ( ::listen(mServerSocket, SOMAXCONN) == SOCKET_ERROR)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
{
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
std::cout << "监听端口失败!" << std::endl;
return;
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
}
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
std::cout << "启动TCP服务器成功!" << std::endl;
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
}
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
~TCPServer()
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
{
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
::closesocket(mServerSocket);
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
std::cout << "关闭TCP服务器成功!" << std::endl;
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
}
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
void run()
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
{
int nAcceptAddrLen =
sizeof(mAcceptAddr);
for (;;)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
{
// 以阻塞方式,等待接收客户端连接
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
mAcceptSocket = ::accept(mServerSocket, (
struct sockaddr*)&mAcceptAddr, &nAcceptAddrLen);
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
std::cout << "接受客户端IP:" << inet_ntoa(mAcceptAddr.sin_addr) << std::endl;
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
// 发送消息
int ret = ::send(mAcceptSocket, SEND_STRING, (
int)strlen(SEND_STRING), 0);
if (ret != 0)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
{
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
std::cout << "发送消息成功!" << std::endl;
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
}
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
// 关闭客户端套接字
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
::closesocket(mAcceptSocket);
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
std::cout << "断开客户端端成功!" << std::endl;
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
}
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
}
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
private:
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
SOCKET mServerSocket;
///< 服务器套接字句柄
sockaddr_in mServerAddr; ///< 服务器地址
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
SOCKET mAcceptSocket;
///< 接受的客户端套接字句柄
sockaddr_in mAcceptAddr; ///< 接收的客户端地址
};![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
int _tmain(
int argc, _TCHAR* argv[])
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
{
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
TCPServer server;
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
server.run();
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
return 0;
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
}
客户端代码:
class TCPClient
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/2f88ce130b654eb5dc6788e02dbcfc90.gif)
{
public:
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
TCPClient()
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
{
// 创建套接字
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
mServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if (mServerSocket == INVALID_SOCKET)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
{
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
std::cout << "创建套接字失败!" << std::endl;
return;
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
}
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
// 填充服务器的IP和端口号
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
mServerAddr.sin_family = AF_INET;
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
mServerAddr.sin_addr.s_addr = inet_addr(SERVER_IP);
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
mServerAddr.sin_port = htons((u_short)SERVER_PORT);
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
// 连接到服务器
if ( ::connect(mServerSocket, (
struct sockaddr*)&mServerAddr,
sizeof(mServerAddr)))
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
{
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
::closesocket(mServerSocket);
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
std::cout << "连接服务器失败!" << std::endl;
return;
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
}
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
}
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
~TCPClient()
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
{
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
::closesocket(mServerSocket);
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
}
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
void run()
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
{
int nRecvSize = 0;
char buff[BUFFER_SIZE];
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
memset(buff, 0,
sizeof(buff) );
while (nRecvSize = ::recv(mServerSocket, buff, BUFFER_SIZE, 0) )
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
{
if (mServerSocket == INVALID_SOCKET)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
{
break;
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
}
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
std::cout << buff << std::endl;
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
}
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
std::cout << "已经和服务器断开连接!" << std::endl;
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
}
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
private:
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
SOCKET mServerSocket;
///< 服务器套接字句柄
sockaddr_in mServerAddr; ///< 服务器地址
};![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
int _tmain(
int argc, _TCHAR* argv[])
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
{
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
TCPClient client;
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
client.run();
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
system("pause");
return 0;
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
}