?? transbmp.cpp
字號:
// TransBmp.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "TransBmp.h"
#include "MainFrm.h"
#include "TransBmpDoc.h"
#include "TransBmpView.h"
#include "common.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define LINESIZE 1024
HANDLE g_hEvent;
/////////////////////////////////////////////////////////////////////////////
// CTransBmpApp
BEGIN_MESSAGE_MAP(CTransBmpApp, CWinApp)
//{{AFX_MSG_MAP(CTransBmpApp)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
// Standard file based document commands
ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTransBmpApp construction
CTransBmpApp::CTransBmpApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CTransBmpApp object
CTransBmpApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CTransBmpApp initialization
BOOL CTransBmpApp::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
// Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(); // Load standard INI file options (including MRU)
///////////////////////////////////////////////////////////////////////////////////////////
//等待連接,連接成功后顯示界面顯示傳來的位圖
Init();
///////////////////////////////////////////////////////////////////////////////////////////
//主線程中進行四循環監聽來自玩家的連接
SOCKET hAcceptSocket;
hAcceptSocket=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
int reuse = 1;
setsockopt(hAcceptSocket, SOL_SOCKET,SO_DONTLINGER|SO_REUSEADDR, (char FAR *)&reuse, sizeof(int));
struct sockaddr_in LocalAddr; //本端IP地址 Local Address
LocalAddr.sin_family = AF_INET;
LocalAddr.sin_addr.s_addr = htonl(INADDR_ANY);
LocalAddr.sin_port = htons((short int)(LOG_SERVER_PORT));//
bind(hAcceptSocket,(struct sockaddr*)&LocalAddr,sizeof(struct sockaddr));
TRACE("----------------------------------------------------------\n");
TRACE("Recv Server listen on PORT %d\n", LOG_SERVER_PORT);
TRACE("----------------------------------------------------------\n");
listen(hAcceptSocket,MAX_CONNECTION_NUM);
char szIP[16]; SOCKET hPlayerCommSocket;
struct sockaddr RemoteAddr;
int AddrLen = sizeof(struct sockaddr);
if ((hPlayerCommSocket = accept (hAcceptSocket, (struct sockaddr FAR *)&RemoteAddr,
(int FAR* ) &AddrLen))!=INVALID_SOCKET)
{
memset(szIP,0,16);
GetPeerIPBySocket(hPlayerCommSocket,szIP);
TRACE("Accept new connection from IP = %s on sock : %d\n",
szIP,hPlayerCommSocket);
_beginthread(PlayerCommThread,0,(LPVOID)hPlayerCommSocket);
}
///////////////////////////////////////////////////////////////////////////////////////////
// To create the main window, this code creates a new frame window
// object and then sets it as the application's main window object.
g_hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
int Value;
ResetEvent(g_hEvent);
Value=WaitForSingleObject(g_hEvent,60000000);
if (Value==WAIT_TIMEOUT)
{
AfxMessageBox("等待超時!");
return false;
}
///////////////////////////////////////////////////////////////////////////////////////////
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CTransBmpDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CTransBmpView));
AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
// No message handlers
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// App command to run the dialog
void CTransBmpApp::OnAppAbout()
{
CAboutDlg aboutDlg;
aboutDlg.DoModal();
}
/////////////////////////////////////////////////////////////////////////////
// CTransBmpApp message handlers
//通信主線程
///////////////////////////////////////////////////////////////////////////////////////////////
//一個玩家一個該線程,用于該玩家的通信收/發
//該線程中代表該玩家的唯一指針是pNewPlayer
void convert_long ( unsigned char * buffer )
{
unsigned char Temp;
Temp = *( ( unsigned char * ) buffer );
*( ( unsigned char * ) buffer ) = *( ( unsigned char * ) ( buffer + 3 ) );
*( ( unsigned char * ) ( buffer + 3 ) ) = Temp;
Temp = *( ( unsigned char * ) ( buffer + 1 ) );
*( ( unsigned char * ) ( buffer + 1 ) ) = *( ( unsigned char * ) ( buffer + 2 ) );
*( ( unsigned char * ) ( buffer + 2 ) ) = Temp;
}
DWORD g_dwPlayerCommThreadId = 0;
void PlayerCommThread(LPVOID hSocket)
{
SOCKET hPlayerCommSocket = (SOCKET) hSocket;
g_dwPlayerCommThreadId = GetCurrentThreadId();
TRACE("----------------------------------------------------------\n");
TRACE("PlayerCommThread (ID=0x%x) started on sock=%d!\n",
g_dwPlayerCommThreadId,hPlayerCommSocket);
TRACE("----------------------------------------------------------\n");
char szRecvBuf[LINESIZE];
///////////////////////////////////////////////////////////////////////////////////////////
CString strFileName(_T("test.bmp"));
CFile File(strFileName, CFile::modeReadWrite|CFile::modeCreate);
///////////////////////////////////////////////////////////////////////////////////////////
//死循環中接收
//每接收一個包就處理一個,其實這里就只處理兩種包:
//1 即登錄請求身份驗證,通過后發OK,若不通過,則發FAIL
//2 離開消息
int nBytesRecv;
char szIP[16];
memset(szIP,0,16);
GetPeerIPBySocket(hPlayerCommSocket,szIP);
///////////////////////////////////////////////////////////////////////////////////////////
struct sockaddr_in addr;
struct sockaddr addrname;
int namelen = sizeof(struct sockaddr);
getpeername(hPlayerCommSocket,&addrname,&namelen);//獲取對端IP
memcpy((char *)&addr,(char *)&addrname,sizeof(sockaddr));
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
DWORD dwHead[2] ;//= {0x6e636868,0};//hhcn
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
for(;;)
{
memset(szRecvBuf,0,LINESIZE);
nBytesRecv = RecvFixedBytes(hPlayerCommSocket,(char *)dwHead,8);
if (nBytesRecv <= 0)
{
TRACE("對方斷線\n");
break;
}
if(dwHead[0]!=0x6e636868) //"hhcn"
{
TRACE("頭不對\n");
continue;
}
dwHead[1] = ntohl(dwHead[1]);
memset(szRecvBuf,0,LINESIZE);
nBytesRecv=RecvFixedBytes(hPlayerCommSocket,szRecvBuf,dwHead[1]);
TRACE("收到%d字節,來自IP= %s,dwHead[1] = %d\n",nBytesRecv,szIP,dwHead[1]);
if(nBytesRecv <= 0)
{
TRACE("對方斷線\n");
File.Close();
SetEvent(g_hEvent);
TRACE("BMP文件傳輸完畢\n");
return;//TCP斷線
}
//TRACE("收到:%s\n",szRecvBuf);
///////////////////////////////////////////////////////////////////////////////////////
File.Write((void*)szRecvBuf,dwHead[1]);
if(dwHead[1] < LINESIZE || nBytesRecv < LINESIZE)
{
File.Close();
SetEvent(g_hEvent);
TRACE("BMP文件傳輸完畢\n");
break;
}
///////////////////////////////////////////////////////////////////////////////////////////
}
TRACE("PlayerCommThread exit!\n");
}
///////////////////////////////////////////////////////////////////////////////////////////////
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -