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

📄 client.cpp

📁 柴油机的控制和状态检测使用的是电子设备ECU,ECU内运行的程序(生产文件)要在柴油机下线前装入。装入程序使用的是罐装设备
💻 CPP
字号:

#include "stdafx.h"
#include "SocketFile.h"
#include "Client.h"
#include "SocketFileDlg.h"

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


CClient::CClient()
{
 m_hSocket=NULL;
}

CClient::~CClient()
{

}

void CClient::ClientInit()
{
 if(WSAAsyncSelect(m_hSocket,m_hWnd,CLI_MESSAGE,FD_ACCEPT|FD_CONNECT|FD_READ|FD_WRITE|FD_CLOSE)>0)
 {
  AfxMessageBox("AsyncSelect  Error!");
 }

}

BOOL CClient::InitAndConnect(HWND hwnd, UINT port, CString strServer)
{
  m_uPort=port;
  m_hWnd=hwnd;
  m_strServer=strServer;
  //如果Socket已经创建则关闭 
  if(m_hSocket!=NULL)
   {
     closesocket(m_hSocket);
	 m_hSocket=NULL;
   }
  //
   if(m_hSocket==NULL)
   {
      m_hSocket=socket(AF_INET,SOCK_STREAM,0);
	  ASSERT(m_hSocket!=NULL);
	  ClientInit();
   }

   m_addr.sin_family=AF_INET;
   m_addr.sin_addr.S_un.S_addr = inet_addr(m_strServer.GetBuffer(0));
   m_addr.sin_port=htons(m_uPort);
   int ret=0;
   int error=0;
   //绑定端口
   ret=connect(m_hSocket,(LPSOCKADDR)&m_addr,sizeof(m_addr));

   if(ret==SOCKET_ERROR)
   {
	   if(GetLastError()!=WSAEWOULDBLOCK)
	   {
	   AfxMessageBox(_T("请确认服务器是否打开同样的端口"));
	   return FALSE;
	   }
   }
  
   	
	return true;
}
//接收数据
void CClient::GetString(CString &str)
{
recv(m_hSocket,str.GetBuffer(0),1024,MSG_DONTROUTE);
}
//发送数据
void CClient::SendString(CString a)
{
 if(send(m_hSocket,a.GetBuffer(0),a.GetLength(),0)==SOCKET_ERROR)
 {
  AfxMessageBox("Client Send Data Error! ");
 }

}

void CClient::DisConnect_Server()
{
  closesocket(m_hSocket);
  m_hSocket=NULL;
}
//按字节发送数据
BOOL CClient::SendData(char *buf, UINT len)
{
  BOOL b=true;
 if(send(m_hSocket,buf,len,0)==SOCKET_ERROR)
 {
   b=false;
 }
	return b;
}

⌨️ 快捷键说明

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