server端
#pragma once
#ifndef __SOCKET_H__
#define __SOCKET_H__
#include
#include
#pragma comment(lib,"ws2_32.lib")
const int MAX_DATA = 1024;
class CServer
{
public:
CServer();
~CServer();
bool InitSocket();
bool Communication();
private:
SOCKET m_sockServer;
};
server,cpp
#include "Server.h"
CServer::CServer()
:m_sockServer(INVALID_SOCKET)
{
}
CServer::~CServer()
{
closesocket(m_sockServer);
WSACleanup();
}
bool CServer::InitSocket()
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData))
{
//初始化环境失败
std::cout << "init failed\n";
return false;
}
if (INVALID_SOCKET == (m_sockServer = socket(AF_INET, SOCK_STREAM, 0/*IPPROTO_TCP*/)))
{
//创建失败
WSACleanup();
std::cout << "create scoket failed\n";
return false;
}
sockaddr_in serverAddr;
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
serverAddr.sin_port = htons(6000);
//绑定
if (SOCKET_ERROR == bind(m_sockServer, (sockaddr*)&serverAddr, sizeof(sockaddr)))
{
//绑定失败
std::cout << "bind failed\n";
WSACleanup();
return false;
}
//监听
if (SOCKET_ERROR == listen(m_sockServer, SOMAXCONN))
{
//监听失败
WSACleanup();
std::cout << "listen failed\n";
return false;
}
return true;
}
bool CServer::Communication()
{
SOCKET sockClient = INVALID_SOCKET;
sockaddr_in clientAddr;
int nLen = sizeof(SOCKADDR);
memset(&clientAddr, 0, nLen);
long lCnt = 0;
//接收连接
while (1)
{
//阻塞
if (SOCKET_ERROR != (sockClient = accept(m_sockServer, (SOCKADDR*)&clientAddr, &nLen)))
{
break;
}
else
{
std::cout << lCnt++ << "次:accept failed\n";
}
}
//发送数据
char szSendData[] = "this wite speaking";
if (SOCKET_ERROR == send(sockClient, szSendData, sizeof(szSendData), 0))
{
//发送失败
return false;
}
//接收数据,阻塞
char szData[MAX_DATA] = { 0 };
memset(szData, 0, MAX_DATA);
if (SOCKET_ERROR == recv(sockClient, szData, MAX_DATA, 0))
{
//接收失败
return false;
}
std::cout << szData << std::endl;
return true;
}
client.h
#pragma once
#ifndef __CLIENT_H__
#define __CLIENT_H__
#include
#include
using std::cout;
#pragma comment(lib,"ws2_32.lib")
const int MAX_DATA = 1024;
class CTCPClient
{
private:
SOCKET m_sockClient;
public:
CTCPClient();
~CTCPClient();
bool InitSocket();
bool Communication();
};
#endif // !__CLIENT_H__
client.cpp
CTCPClient::CTCPClient()
:m_sockClient(INVALID_SOCKET)
{
}
CTCPClient::~CTCPClient()
{
closesocket(m_sockClient);
WSACleanup();
}
bool CTCPClient::InitSocket()
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData))
{
//初始化环境失败
std::cout << “init failed\n”;
return false;
}
if (INVALID_SOCKET == (m_sockClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)))
{
//创建套接字失败
WSACleanup();
std::cout << "create socket failed\n";
return false;
}
sockaddr_in sockSrv;
memset(&sockSrv, 0, sizeof(sockSrv));
sockSrv.sin_family = AF_INET;
sockSrv.sin_port = htons(6000);
sockSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
if (SOCKET_ERROR == connect(m_sockClient, (sockaddr*)&sockSrv, sizeof(sockaddr)))
{
//连接失败
WSACleanup();
std::cout << "connect failed\n";
return false;
}
return true;
}
bool CTCPClient::Communication()
{
char szData[MAX_DATA] = { 0 };
if (SOCKET_ERROR == recv(m_sockClient, /(LPSTR)/szData, MAX_DATA, 0))
{
std::cout << “recv failed\n”;
return false;
}
cout << szData << “\n”;
char szSendData[] = “hello,wite”;
if (SOCKET_ERROR == send(m_sockClient, szSendData, sizeof(szSendData), 0))
{
std::cout << “send failed\n”;
return false;
}
return true;
}