📄 clienttool.cpp
字号:
// ClientTool.cpp: implementation of the CClientTool class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Client.h"
#include "ClientTool.h"
#include <winsock.h>
#include "ReceiveFile.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CClientTool::CClientTool()
{
}
CClientTool::~CClientTool()
{
}
bool CClientTool::Connect()
{
socket.Create();
// socket.Connect("192.168.31.121", 5000);
socket.Connect("192.168.0.66", 3490);
return true;
}
int CClientTool::sendMessage(CString message)
{
return send(socket, message, message.GetLength(),0);
}
bool CClientTool::closeConnect()
{
if(socket != INVALID_SOCKET)
{
socket.ShutDown();
socket.Close();
}
return true;
}
CString CClientTool::reveMessage()
{
int iBytesRead;
int iBufferLength;
int iEnd;
int iSpaceRemaining;
char chIncomingDataBuffer[100];
memset(chIncomingDataBuffer, 0, 100);
iBufferLength = iSpaceRemaining = sizeof(chIncomingDataBuffer);
iEnd = 0;
iSpaceRemaining -= iEnd;
iBytesRead = recv(socket, (LPSTR)(chIncomingDataBuffer+iEnd), iSpaceRemaining, 0);
iEnd+=iBytesRead;
if (iBytesRead == SOCKET_ERROR)
AfxMessageBox("OnClientRead recv reported a socket error. ");
chIncomingDataBuffer[iEnd] = '\0';
CString recv = chIncomingDataBuffer;
return recv;
}
int CClientTool::Send(CEncapsulation* message)
{
return send(socket,message->GetContent(),message->GetLength(),0);
}
void CClientTool::Recv(CMessage* &message)
{
char buf[1024];
int n=recv(socket,buf,1024,0);
CAnalyse analyse(buf, n);
message=analyse.GetObject();
}
CString CClientTool::getmyip()
{
BYTE *p;
char temp[100];
struct hostent *hp;
char ip[16];
if(gethostname(temp, sizeof(temp))== 0)
{
if((hp =gethostbyname(temp))!=0)
{
p =(BYTE *)hp->h_addr;
wsprintf(ip, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
// MessageBox( ip, "IP Address", MB_OK);
}
}
return ip;
}
bool CClientTool::ReceiveFile(CString msg)
{
CString ip,recvfilename;
ip=msg.Left(16);
ip.TrimRight();
recvfilename=msg.Mid(17);
recvfilename.TrimLeft();
TCHAR szFilters[]=_T("All files(*.*)|*.*||");
CString filename2,path,messagefile;
CFileDialog fileChoose(FALSE, NULL, recvfilename, NULL, szFilters);
if (fileChoose.DoModal() != IDOK) //如果用户选择确定
{
AfxMessageBox("你没有选择路径!");
return FALSE;
}
path=fileChoose.GetPathName();
CReceiveFile recvfile;
recvfile.Client(ip,path);
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -