⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socketsend.cpp

📁 一个网络监视的程序
💻 CPP
字号:
// SocketSend.cpp: implementation of the SocketSend class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "CCAMSC.h"
#include "SocketSend.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

SocketSend::SocketSend()
{
	InitializeCriticalSection(&CriticalSectionBuff);
	InitializeCriticalSection(&CriticalSectionSock);
	for(int i=0;i<=17;i++)
		storeCpuInfo[i]='\0';
}
//*******************************************************************************
//函 数 名:StartSock
//功    能:加载创建sock所需要的库,加载成功返回1,失败返回-1;
//输入参数:无
//输出参数:无
//编写日期:2004.07.16
//修改日期:
//作    者:吴海松
//其他说明:
//********************************************************************************
DWORD SocketSend::StartSock()
{
	WSADATA WSAdata;
//	AfxMessageBox("开始初始化socket......\n");
	if(WSAStartup(MAKEWORD(2,2),&WSAdata)!=0)
	{
		AfxMessageBox("socket初始化失败!\n\n");
		return(-1);
	}
	
//	AfxMessageBox("初始化socket成功.\n\n");
	return 1;
}
//*******************************************************************************
//函 数 名:CreatSocket
//功    能:创建socket。创建成功返回1,失败返回-1。
//输入参数:无
//输出参数:无
//编写日期:2004.07.16
//修改日期:
//作    者:吴海松
//其他说明:
//********************************************************************************
DWORD SocketSend::CreateSocket()	
{
//	AfxMessageBox("开始创建socket......\n");
	sock=socket(AF_INET,SOCK_STREAM,0);//创建套前字
	if(sock==SOCKET_ERROR)
	{
		AfxMessageBox("创建socket失败!\n\n");
		WSACleanup();
		return(-1);
	}
//	AfxMessageBox("创建socket成功.\n\n");
	return 1;
	
	
}
//*******************************************************************************
//函 数 名:CallServer
//功    能:和服务器连接.
//输入参数:无
//输出参数:无
//编写日期:2004.07.16
//修改日期:
//作    者:吴海松
//其他说明:
//********************************************************************************
BOOL SocketSend::CallServer( char serverIp[20],int CallPort)
{
	int ConnectTime=0;
	long ErrorCode;
	CreateSocket();
	ServerAddr.sin_family=AF_INET;
	ServerAddr.sin_addr.s_addr=inet_addr(serverIp);
	ServerAddr.sin_port=htons(CallPort);
	
//	AfxMessageBox("正在试图连接服务器......\n");
	while(connect(sock,(struct sockaddr *)&ServerAddr,sizeof(ServerAddr))==SOCKET_ERROR && ConnectTime<=MaxConnectTimes)
	{
		ErrorCode=WSAGetLastError(); //输出错误代码
		switch(ErrorCode)
		{
		case 10046:
			AfxMessageBox("10046 不支持的协议!\n");
			break;
		case 10047:
			AfxMessageBox("10047 地址家族不支持的操作!\n");
			break;
		case 10048:
			AfxMessageBox("10048 地址正在使用!\n");
			break;
		case 10049:
			AfxMessageBox("10049 不能分配请求的地址!\n");
			break;
		case 10050:
			AfxMessageBox("10050 网络断开!\n");
			break;
		case 10051:
			AfxMessageBox("10051 网络不可达!\n");
			break;
		case 10052:
			AfxMessageBox("10052 网络重设时断开了连接!\n");
			break;
		case 10053:
			AfxMessageBox("10053 软件造成连接取消!\n");
			break;
		case 10054:
			AfxMessageBox("10054 连接被对方重设!\n");
			break;
		case 10055:
			AfxMessageBox("10055 没有缓冲区空间!\n");
			break;
		case 10056:
			AfxMessageBox("10056 该套接字已经被使用!\n");
			break;
		case 10057:
			AfxMessageBox("10057 套接字尚未连接!\n");
			break;
		case 10060:
			AfxMessageBox("10060 连接超时!\n");
			ConnectTime++;
			break;
		case 10061:
			AfxMessageBox("10061 连接被拒绝!\n");
			Sleep(1000);
			break;
		case 10064:
			AfxMessageBox("10064 主机已关闭!\n");
			break;
		case 10065:
			AfxMessageBox("10065 没有到主机的路由!\n");
			Sleep(1000);
			break;
		case 10092:
			AfxMessageBox("10092 Winsock.dll版本有错误!\n");
			break;
		case 10093:
			AfxMessageBox("10093 Winsock尚未初始化!\n");
			break;
		default:
			AfxMessageBox("出现未知错误!\n");
		}
//		AfxMessageBox("\n正在试图连接服务器......\n");
		ConnectTime++;
		
	}
	if(ConnectTime<=MaxConnectTimes)
		return 1; //连接成功
	else
		return 0; //连接失败
}
//*******************************************************************************
//函 数 名:SendBuff
//功    能:向服务器发送数据.发送成功返回1,发送失败返回0.
//输入参数:要发送的数据包
//输出参数:无
//编写日期:2004.07.16
//修改日期:
//作    者:吴海松
//其他说明:
//********************************************************************************
int SocketSend::SendBuff(std::string CpuInfo)
{
	EnterCriticalSection(&CriticalSectionBuff);

	strcpy(storeCpuInfo,CpuInfo.c_str());
	CreateThread( NULL,0,SendPacket,this,0,0 );
	LeaveCriticalSection(&CriticalSectionBuff);
	
	return 1;

	
}
//********************************************************************************
SocketSend::~SocketSend()
{
	closesocket(sock);
	WSACleanup();

}




















⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -