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

📄 server2dlg.cpp

📁 网络传输方面的范例程序,其中有MD5加密和解密的子程序,可以参考
💻 CPP
字号:
// server2Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "server2.h"
#include "server2Dlg.h"
#include "MD5.h"


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

/////////////////////////////////////////////////////////////////////////////
// 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)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CServer2Dlg dialog

CServer2Dlg::CServer2Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CServer2Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CServer2Dlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CServer2Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CServer2Dlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CServer2Dlg, CDialog)
	//{{AFX_MSG_MAP(CServer2Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CServer2Dlg message handlers

BOOL CServer2Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CServer2Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CServer2Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CServer2Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CServer2Dlg::OnOK() 
{
const SOCKET_BUFFER_MAX_LENGTH = 8000;
      //int SOCKET_BUFFER_MAX_LENGTH;
	  //SOCKET_BUFFER_MAX_LENGTH=8000;
	WSADATA wsaData;
	WSAStartup( MAKEWORD(2,2) , &wsaData );
	//输入服务器地址
	char strclientIP[32];
	strcpy((char*)strclientIP,"25.20.183.175");

	char strserIP[32];
	strcpy((char*)strserIP,"25.20.183.176");



	char strSendMsg[SOCKET_BUFFER_MAX_LENGTH+1] = { 0 };
	unsigned short SERVER_PORT;
	SERVER_PORT=188;
	//将服务器网络地址转换成sockaddr_in格式
	sockaddr_in clientAddr,seraddr;
  seraddr.sin_addr.S_un.S_addr=inet_addr( (char*)strserIP );
	clientAddr.sin_addr.S_un.S_addr = inet_addr( (char*)strclientIP );
    clientAddr.sin_family = AF_INET;
	
	clientAddr.sin_port = ntohs( SERVER_PORT );
	
	SOCKET Sockserver = socket( AF_INET , SOCK_STREAM , IPPROTO_TCP );
		int lent=sizeof(clientAddr);
	bind(Sockserver,(SOCKADDR*)&clientAddr,lent);
      listen(Sockserver,10);
	//if(listen(Sockserver,10)!=0)
	///	MessageBox("over time!");

	SOCKET sockConn=accept(Sockserver,(SOCKADDR*)&seraddr,&lent);
      // CString name;	
	int mu=0;	CString s;	int suser=9;
	for(int u=0;u<3;u++)
	{
		mu=mu+1;
	char name[100];

    recv(sockConn,(char*)&name,100,0);
	
	s.Format("%s",name);
	MessageBox(s);


	  if(strcmp(s,"xiaowenhai")== 0) 
	  {
	 	        suser=0;
				send(sockConn,(char*)&suser,4,0);  
				break;
	  }
	  else
	  {
		  suser=1;
	      send(sockConn,(char*)&suser,4,0);  
	  }
   	}
	if(mu!=3||mu==3&&suser==0)
	{

   srand((unsigned)time(NULL));
	 int x= rand(); 
 x=x/100;
 CString s6;
 s6.Format("%d\n",x);

	MessageBox(s6);
	send(sockConn,(char*)&x,4,0);

    char input1[100];
/////	memset(input1,'0',100);
	   recv(sockConn,input1,100,0);
	   s6.Format("%s",input1);
	   /// MessageBox(s6);
		/// Sleep(5000);
	   CString snut;
	   snut.Format("%d",x);
	   s6=s6+snut;
	  /// MessageBox(s6);*/


	     strncpy(input1,s6,strlen(s6));
		   input1[strlen(s6)]='\0';
		   CString u;
		   u.Format("u is %s,chuangdu%d",input1,strlen(input1)+1);
		   MessageBox(u);

unsigned char digest2[16];
  CMD5 yangMD5;
yangMD5.Digest_data((unsigned char*)input1,16,digest2);
////////////digest2[16]='\0';
CString syt;
syt.Format("%s",digest2);
//MessageBox(syt);
	 unsigned char digest[16];
int jieguo=9;
    int chang=recv(sockConn,(char*)digest,16,0);
	syt.Format("%s,%d",digest,chang);
//MessageBox(syt);
	for(int i=0;i<16;i++)
	{
		if(digest[i]!=digest2[i])
		{
			s.Format("error,i=%d",i);
		//	MessageBox(s);
			jieguo=1;
			goto L;
		}
		jieguo=0;
	}

//////	MessageBox("right");
	send(sockConn,(char*)&jieguo,4,0);  
				
  

///	digest[16]='\0';

//////////////////Digest_data(unsigned char *input,int inputlen,unsigned char digest[16])
/*
  	 char Buffer[] = "This is a test of the memset function";

  
	MessageBox(s);
int iSendCount=0;
iSendCount= send(sockConn,Buffer,strlen(Buffer)+1,0);
s.Format("已将数据发送到服务器,共 %d 字节.\n" , iSendCount );*/
	}
	closesocket(sockConn);
//	send(
	
/*



	//connect( SockClient , (SOCKADDR*)&ServerAddr , sizeof(ServerAddr) );
	printf( "连接到服务器 %s : %d......连接成功\n" , inet_ntoa(ServerAddr.sin_addr) , SERVER_PORT );
    
	//发送数据
	strSendMsg[SOCKET_BUFFER_MAX_LENGTH]=0;
	int iSendCount = send( SockClient , strSendMsg , strlen(strSendMsg) , 0 );
	printf( "\t已将数据发送到服务器,共 %d 字节.\n" , iSendCount );
	//关闭套接字
	closesocket( SockClient );
	//清除网络库连接
	WSACleanup();
//	return 0;    
        CString s;
		s.Format("已将数据发送到服务器,共%d字节.", iSendCount); 
		MessageBox(s);*/	
L:	CDialog::OnOK();
	
}

⌨️ 快捷键说明

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