Socket 及 FTP通讯

#pragma once  // 头文件

#include "afxwin.h"
#include <afxsock.h>
#include <afxinet.h>

class CRobotControl
{
public:
	CRobotControl(void);
	~CRobotControl(void);

public:
	BOOL RobotSocketConnect(CString m_RobotIP, int strport);
	BOOL RobotFTPConnect(CString m_RobotIP, CString strusr, int strport);
	BOOL RobotSendFile(CString str, CString strname);
	void RobotSend(CString strOrdel);
	char* CStringToChar(CString strCLIPath);
	void RobotSocketClose();

public:
	CInternetSession *pRobotSession;
	CFtpConnection *pRobotConnection;

	SOCKET m_hSocket;
	struct sockaddr_in local;
};
#include "StdAfx.h"  // cpp文件
#include "RobotControl.h"

CRobotControl::CRobotControl(void)
{
	pRobotConnection = NULL;
	pRobotSession = NULL;
	m_hSocket = NULL;
}

CRobotControl::~CRobotControl(void)
{
	RobotSocketClose();
	if (pRobotSession)
	{
		pRobotSession->Close();
		delete pRobotSession;
	}
}

void CRobotControl::RobotSocketClose()
{
	closesocket(m_hSocket);
	if (m_hSocket != NULL)
	{
		m_hSocket = NULL;
		//m_hSocket = INVALID_SOCKET;
	}
	//

	pRobotConnection = NULL;
}

BOOL CRobotControl::RobotSocketConnect(CString m_RobotIP, int strport)
{
	AfxSocketInit();
	m_hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (m_hSocket == SOCKET_ERROR)
	{
		return FALSE;
	}

	unsigned long ul = 1;
	int ret = ioctlsocket(m_hSocket, FIONBIO, (unsigned long*)&ul);

	if(ret==SOCKET_ERROR)
		return FALSE;

	char cip[30]; 
	wcstombs(cip, m_RobotIP, m_RobotIP.GetLength() * 2);

	local.sin_family = AF_INET;
	local.sin_addr.S_un.S_addr = inet_addr(cip);
	local.sin_port = htons(strport);

	connect(m_hSocket, (sockaddr *)&local, sizeof(local));

	ul = 0;
	ret = ioctlsocket(m_hSocket, FIONBIO, (unsigned long*)&ul);

	if(ret==SOCKET_ERROR)
		return FALSE;

	struct timeval timeout;
	fd_set set;
	FD_ZERO(&set);
	FD_SET(m_hSocket, &set);
	timeout.tv_sec = 1;
	timeout.tv_usec = 0;
	if( select(0, NULL, &set, NULL, &timeout) <= 0) 
	{ 
		closesocket (m_hSocket);
		m_hSocket = INVALID_SOCKET;

		AfxMessageBox(_T("连接失败,请核对IP和端口!"));
		return FALSE;
	} 
	return TRUE;
}

BOOL CRobotControl::RobotFTPConnect(CString m_RobotIP, CString strusr, int strport)
{
	pRobotSession = new CInternetSession(_T(""), 1, /*INTERNET_OPEN_TYPE_PRECONFIG | INTERNET_OPEN_TYPE_DIRECT | */INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY);
	pRobotSession->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 1, 0);   // 超时设置
	try
	{
		pRobotConnection = pRobotSession->GetFtpConnection(m_RobotIP, _T("maintenance"), _T("spec_cal"), strport, FALSE);
		//pRobotConnection = pRobotSession->GetFtpConnection(_T("10.1.6.116"), _T(""), _T(""), strport, FALSE);
		if(pRobotConnection)  // OpenURL(strURL)
		{
			//AfxMessageBox(_T("机械臂FTP正常连接!"));
			return TRUE;
		}
	}
	catch (CInternetException* pEx)
	{
		TCHAR szErr[1024];
		pEx->GetErrorMessage(szErr,1024);
		//AfxMessageBox(szErr);
		pEx->Delete();
		pRobotConnection = NULL;
		return FALSE;
	}
	return FALSE;
}

void CRobotControl::RobotSend(CString strOrdel)
{
	char *nSocketOrdel = CStringToChar(strOrdel);

	if (m_hSocket)
	{
		sendto(m_hSocket, nSocketOrdel, (strlen(nSocketOrdel)), 0, (LPSOCKADDR)&local, sizeof(local));
		//ShowInfo((_T("发送命令: ") + strOrdel));
	}
	else
	{
		AfxMessageBox(_T("请连接网络!"));
	}
}

BOOL CRobotControl::RobotSendFile(CString str, CString strname)
{
	if (pRobotConnection == NULL)
	{
		AfxMessageBox(_T("FTP未连接成功!"));
		return FALSE;
	}	

	CString strdir = _T("/usr/usrapp/saveCAM/");  
	//CString strdir = _T("/test/data/");
	//CString strdir;
	//pRobotConnection->GetCurrentDirectory(strdir);			
	pRobotConnection->SetCurrentDirectory(strdir);
	
	BOOL bput=pRobotConnection->PutFile((LPCTSTR)str,(LPCTSTR)strname);
	if (bput)
	{
		pRobotConnection->SetCurrentDirectory(strdir);
		//ShowInfo(_T("上传成功!"));
		return TRUE;
	}
	else
	{
		//ShowError(_T("上传失败!"));
		return FALSE;
	}
}

char* CRobotControl::CStringToChar(CString strCLIPath)
{
	DWORD dCliPath = WideCharToMultiByte(CP_OEMCP, NULL, strCLIPath, -1, NULL, NULL, 0, NULL);
	char *nCliPaths = new char[dCliPath];
	WideCharToMultiByte(CP_OEMCP, NULL, strCLIPath, -1, nCliPaths, dCliPath, 0, NULL);

	return nCliPaths;
}

void CPrinterControl::PrinterOrder(CString strOrder)  // 打印命令  PRIT START PRINT
{
	if (pFtpConnection)
	{
		try
		{
			pFtpConnection->Command(strOrder, CFtpConnection::CmdRespNone, FTP_TRANSFER_TYPE_BINARY, 1);
		}
		catch (CInternetException* pEx)
		{
			TCHAR szErr[1024];
			pEx->GetErrorMessage(szErr,1024);
			pEx->Delete();
		}
	}
	//PrinterBackInfo();
}

int CPrinterControl::PrinterBackInfo()  // 察看返回的信息
{
	DWORD dwError, dwBufferLength;
	LPTSTR strError;  // 将储存返回信息
	int ntime = -1;
	if( !InternetGetLastResponseInfo(&dwError, NULL, &dwBufferLength) )
	{
		strError = new TCHAR[dwBufferLength + 1];
		InternetGetLastResponseInfo(&dwError, strError, &dwBufferLength);
			
		CString strRemain(_T("NULL"));
		CString strE(_T(""));
		strE.Format( _T("%s"), strError );
		strRemain.Format(_T("%s"), (CStringW)(strstr(CStringToChar(strE),  "Remain")));

		if (strRemain.GetLength() > 5)
		{
			ntime = _ttoi(strRemain.Mid(8, 4));
			//strRemain.Format(_T("%d"), ntime+1);
		}
		else
		{
			strE.Format(_T("%s"), (CStringW)(strstr(CStringToChar(strE),  "Finish")));
			if (strE.GetLength() > 5)
			{
				ntime = -1;
			}
		}

		delete[] strError;
	}

	return ntime;

	/*CString strt(_T("what can i do for you?"));

	DWORD dwNum2 = WideCharToMultiByte(CP_OEMCP, NULL, strt, -1, NULL, NULL, 0, NULL);
	char *nStr = new char[dwNum2];
	WideCharToMultiByte(CP_OEMCP, NULL, strt, -1, nStr, dwNum2, 0, NULL);

	CString str1(_T(""));
	str1.Format(_T("%s"), (CStringW)(strstr(nStr,  "can")));
	ShowListInfo(str1.Mid(4, 10));*/
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值