📄 socket lib.cpp
字号:
//
// winSocket testbed application
// Ryan Lederman - ryan@winprog.org / http://ryan.ript.net/
// 01/21/2002 - 12:15 AM
// Notes: Make sure to link with wsock32.lib and libcmt.lib (for multithreading)
//
//#import "RSADLL.dll"
#include "stdafx.h"
#include "resource.h"
#include "winSocket.h"
#include "stdlib.h"
#include "process.h"
#include <string.h>
#include "RSADLL.h"
// Defines
#define MSGBOX_ERROR(x) MessageBox( GetForegroundWindow(), TEXT(x), "winSocket - ERROR", MB_OK | MB_ICONSTOP )
#define WM_WINSOCK (WM_USER+1)
#define WINTHREAD unsigned __stdcall
const unsigned int port=3333;
//#define port 3333
// Function declarations
BOOL CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
//LRESULT ( HWND hWnd );
LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam );
LRESULT OnWinsockEvent( HWND hWnd, WPARAM wParam, LPARAM lParam );
//LRESULT OnSend( HWND hWnd );
//LRESULT OnResetField( HWND hWnd, int ID );
void EnableEdits( HWND hWnd, BOOL bEnable );
void EditPrint( HWND hWnd, char *format, ... );
void EditAppendPrint( HWND hWnd, char *szOutput, ... );
void ToggleConnectedState( HWND hWnd, BOOL bConnected );
//void DisableForConnect( HWND hWnd, BOOL bEnable );
void DoConnect( HWND hWnd );
WINTHREAD ConnectThread( void *pVoid );
// Global variables
winSocket m_Socket; // Our winSocket wrapper class
BOOL m_bConnected; // Boolean representing state of application
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
//
// Entry point for the application.
// Initializes the main window (dialog) and waits to exit
// When this function returns, the process is exited and
// control is returned to Windows
//
if( DialogBox( hInstance, (LPCSTR)IDD_MAIN, NULL, WndProc ) == -1 )
{
MSGBOX_ERROR( "Failed to initialize application!" );
return 1;
}
return 0;
}
BOOL CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
//
// Message procedure for our user interface
// All messages will pass through this function
//
switch( msg )
{
case WM_INITDIALOG:
if( m_Socket.Create() != ERR_SUCCESS ) // Create the socket
MSGBOX_ERROR( "Failed to create socket!" );
break;
case WM_CLOSE:
m_Socket.Close(); // Destroy the socket
EndDialog( hWnd, 0 );
PostQuitMessage( 0 );
break;
case WM_COMMAND: return OnCommand( hWnd, wParam, lParam );
case WM_WINSOCK: return OnWinsockEvent( hWnd, wParam, lParam );
}
return 0;
}
LRESULT OnWinsockEvent( HWND hWnd, WPARAM wParam, LPARAM lParam )
{
//
// WM_WINSOCK message handler. This function is executed when
// an event or error occurs on our non-blocking socket (m_Socket)
//
WORD wEvent = 0;
WORD wError = 0;
char szIncomingData[1024] = {0};
char szErrDisplay[1024] = {0};
char szLastError[ERR_MAXLENGTH+1] = {0};
int iLastError = 0;
wError = WSAGETSELECTERROR( lParam ); // Find out if any errors occurred.
if( wError )
{ // Some error DID occur, so find out what it was and display it
m_Socket.get_LastError( szLastError, &iLastError );
wsprintf( szErrDisplay, "A Winsock error occurred.\nDescription: %s\nError Number: %d", szLastError, iLastError );
MSGBOX_ERROR( szErrDisplay );
return 0;
}
wEvent = WSAGETSELECTEVENT( lParam ); // Get the event that occurred
switch( wEvent )
{
case FD_READ: // Data has arrived on the socket, append to received field
m_Socket.Receive( (SOCKET)wParam, szIncomingData, 1024 ); // Read data from socket
EditAppendPrint( hWnd, "%s\r\n", szIncomingData );
break;
case FD_CLOSE: // Connection was closed by remote side
ToggleConnectedState( hWnd, FALSE );
EnableEdits( hWnd, TRUE );
m_Socket.Close(); // Reset our socket object
break;
}
return 0;
}
//LRESULT OnConnect( HWND hWnd )
//{
//
// This function is executed when the connect/disconnect button
// is pressed, and performs the associated operation
//
/* UINT iThreadID = 0;
if( m_bConnected ) // We're already connected, do disconnect
{
m_Socket.Close(); // Close socket and reset object
ToggleConnectedState( hWnd, FALSE );
EnableEdits( hWnd, TRUE );
SetFocus( GetDlgItem( hWnd, IDC_HOST ) );
return 0;
}
// Otherwise, let's connect. Spawn the connection thread which handles remote connection
_beginthreadex( NULL, 0, ConnectThread, (void*)hWnd, 0, &iThreadID );
*/
// return 0;
//}
WINTHREAD ConnectThread( void *pVoid )
{
//
// This thread is spawned when a connection needs to be made.
// if the Connect() call is not in a seperate thread from the message loop,
// the user interface will lock up while Connect() executes.
//
DoConnect( (HWND)pVoid );
_endthreadex( 0 );
return 0;
}
void DoConnect( HWND hWnd )
{
//
// This function is executed by the ConnectThread.
// it disables parts of the user interface while the connection is processed
//
char szHost[512] = {0};
char szPort[512] = {0};
char szLastError[ERR_MAXLENGTH+1] = {0};
int iLastError = 0;
// EnableEdits( hWnd, FALSE ); // Disable host and port fields
SetWindowText( hWnd, "winSocket - Connecting..." ); // Let user know the app is working
// DisableForConnect( hWnd, FALSE );
GetDlgItemText( hWnd, IDC_HOST, szHost, 512 );
// GetDlgItemText( hWnd, IDC_PORT, szPort, 512 ); // Yeah, I know you don't need 512 :P
if( strlen( szHost ) == 0 )
{
// Bad param(s)!
//EnableEdits( hWnd, TRUE );
MSGBOX_ERROR( "Please fill in the host fields before connecting." );
return;
}
if( m_Socket.Create() != ERR_SUCCESS )
{
MSGBOX_ERROR( "Failed to create socket!" );
return;
}
if( m_Socket.Connect( szHost, port ) != ERR_SUCCESS ) // Connect to remote host
{
EnableEdits( hWnd, TRUE ); // It failed, re-enable edit boxes
ToggleConnectedState( hWnd, FALSE ); // Reset application to disconnected state
//DisableForConnect( hWnd, TRUE );
m_Socket.get_LastError( szLastError, &iLastError ); // Get error information from socket
// EditPrint( hWnd, "Failed to connect to [%s : %d]!\rt, atoi( szPort ), s\nError: %s - %d", szHoszLastError, iLastError );
MSGBOX_ERROR( "connect failed" );
return;
}
SetWindowText( hWnd,"connect success");
// Connected!
//DisableForConnect( hWnd, TRUE );
ToggleConnectedState( hWnd, TRUE );
if( m_Socket.asyncSelect( hWnd, WM_WINSOCK, FD_READ | FD_CLOSE ) != ERR_SUCCESS ) // Async0rize our socket
MSGBOX_ERROR( "Failed to set socket to non-blocking mode!" );
}
LRESULT OnButton1( HWND hWnd )
{
InitialRSA();
char strdata[16]="";
DoConnect(hWnd);
m_Socket.Receive(strdata,sizeof(strdata));
SetDlgItemText(hWnd,IDC_PORT,strdata);
return 0;
}
LRESULT OnButton2( HWND hWnd )
{
char strdata[16];//="123456789012345";//{'s','d','s','a','d','d','d','d','d','d','d','d','a','f','d','c'};
GetDlgItemText( hWnd, IDC_PORT, strdata, 16 );
char temp[16]="";
char temp1[16]="";
char temp2[36]="";
long temp3[16];
char send_to[32]="";
unsigned char numch;
unsigned char numch1;
BYTEARRAY pk,sk,n;
BYTEARRAY indata[16];
BYTEARRAY outdata[16];
memset(pk,0,sizeof(BYTEARRAY));
memset(sk,0,sizeof(BYTEARRAY));
memset(n,0,sizeof(BYTEARRAY));
InitialRSA();
if (!AutoGenKeys(4))
{
MSGBOX_ERROR("自动生成密钥失败");
return 0;
}
BYTEARRAY p,q,z;
GetPQZ(p,q,z);
GetPK_SK_N(pk,sk,n);
SetPK_SK_N(pk,sk,n);
for(int tt=0;tt<16;tt++)
{
temp1[16-tt]=pk[DATALENGTH-tt]+'0';
}//temp1中存放的是客户端公共密钥
//交换密钥
BYTEARRAY tem;
for(int ww=0;ww<DATALENGTH;ww++)
{
tem[ww]=pk[ww];
pk[ww]=sk[ww];
sk[ww]=tem[ww];
}
for(int m=0;m<16;m++)
{
for(int n=0;n<DATALENGTH;n++)
{
indata[m][n]=0;
outdata[m][n]=0;
}
}
for(int k=0;k<16;k++)
{
memset(indata[k],0,sizeof(BYTEARRAY));
memset(outdata[k],0,sizeof(BYTEARRAY));
numch=strdata[k];
int ll=int(numch);
int a=ll/100;
int b=(ll-a*100)/10;
int c=ll-a*100-b*10;
indata[k][DATALENGTH-1]=c;
indata[k][DATALENGTH-2]=b;
indata[k][DATALENGTH-3]=a;
}
if (RSACrypt(indata,outdata,16,0))//加密
{
MSGBOX_ERROR("由于密钥的问题,加密失败");
return 0;
}
//显示密文:
for(int gj=0;gj<16;gj++)
{
temp[gj]='0';
}
for (int ff=0;ff<16;ff++)
{
for(int pp=0;pp<36;pp++)
{
temp2[pp]=outdata[ff][pp]+'0';
}
temp[ff]=char(atol(temp2));
}
SetDlgItemText(hWnd,IDC_EDIT2,temp);
//encrypt again
//temp存放的是密文
/*for(int mm=0;mm<16;mm++)
{
pk[DATALENGTH-1-mm]=strdata[15-mm]-'0';//server public_key
}
for(int nn=0;nn<16;nn++)//server n
{
n[DATALENGTH-1-nn]=strdata[31-nn]-'0';
}
memset(indata[k],0,sizeof(BYTEARRAY));
memset(outdata[k],0,sizeof(BYTEARRAY));
for(int vv=0;vv<16;vv++)//设置明文
{
long r=long(temp[vv]);
ltoa(r,temp2,36);
for(int yy=0;yy<DATALENGTH;yy++)
{
indata[vv][DATALENGTH-yy-1]=temp2[DATALENGTH-yy-1];
}
}
if (RSACrypt(indata,outdata,16,0))//加密
{
MSGBOX_ERROR("由于密钥的问题,加密失败");
return 0;
}
for (int ff=0;ff<16;ff++)//显示密文
{
for(int pp=0;pp<36;pp++)
{
temp2[pp]=outdata[ff][pp]+'0';
}
temp[ff]=char(atol(temp2));
}
SetDlgItemText(hWnd,IDC_EDIT1,temp);//temp存放密文
//for(int yy=0;yy<16;yy++)
// {
// send_to[yy]=temp1[yy];
//}
//for(int xx=16;xx<32;xx++)
//{
// send_to[xx]=temp[xx-DATALENGTH];
//}
//m_Socket.Send(send_to,sizeof(send_to));//send public key and encode to server
// sleep(10);
// m_Socket.Receive(strdata,sizeof(strdata));
//SetDlgItemText(hWnd,IDC_EDIT2,strdata);*/
// 再解密
if (RSACrypt(outdata,indata,16,1))
{
MSGBOX_ERROR("由于密钥的问题,解密失败!");
return 0;
}
for(int hj=0;hj<16;hj++)
{
temp[hj]='0';
}
for (int gg=0;gg<16;gg++)
{
int q=10;
int y=0;
y=indata[gg][33]*100+indata[gg][34]*10+indata[gg][35];
temp[gg]=char(y);
}
SetDlgItemText(hWnd,IDC_EDIT3,temp);
/* if(m_Socket.Send(strdata,sizeof(strdata)))
{
MSGBOX_ERROR("send failed!");
return 0;
}
if(m_Socket.Receive(strdata,sizeof(strdata)))
{
MSGBOX_ERROR("recieve failed!");
return 0;
}
SetDlgItemText(hWnd,IDC_EDIT2,strdata);
*/
// MSGBOX_ERROR( "connect failed!" );
return 0;
}
LRESULT OnButton3( HWND hWnd )
{
char string[16];
char string1[16];
GetDlgItemText( hWnd, IDC_EDIT3,string, 16 );
if( strlen( string ) == 0 )
{
MSGBOX_ERROR( "Please fill in the name fields." );
return 0;
}
//encrypt(string);
m_Socket.Send(string,sizeof(string));
if(m_Socket.Receive(string1,sizeof(string1)))
{
MSGBOX_ERROR("recieve failed!");
return 0;
}
//dencrypt();
SetDlgItemText(hWnd,IDC_EDIT4,string1);
return 0;
}
LRESULT OnButton4( HWND hWnd )
{
m_Socket.Close();
MSGBOX_ERROR("connect has been closed");
exit(1);
return 0;
}
//LRESULT OnSend( HWND hWnd )
//{
//
// This function is executed when the user presses the Send button
// while connected to a remote host.
// Sends whatever data is in the send field to the remote site
//
// char szOutgoingData[2048] = {0};
//
// GetDlgItemText( hWnd, IDC_TOSEND, szOutgoingData, 2048 );
//
// if( strlen( szOutgoingData ) <= 0 )
// {
// MSGBOX_ERROR( "No data to send!" );
// return 0;
// }
//
// m_Socket.Send( szOutgoingData, strlen( szOutgoingData ) ); // Send the data to the remote host
//
//return 0;
//}
LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam )
{
//
// This is the WM_COMMAND message handler.
// it is executed when the user presses one of the buttons
// on the user interface
//
switch( LOWORD( wParam ) )
{
// case IDC_CONNECT: return OnConnect( hWnd );
// case IDC_SEND: return OnSend( hWnd );
// case IDC_RESETRECEIVE: return OnResetField( hWnd, IDC_RECEIVED );
// case IDC_RESETSEND: return OnResetField( hWnd, IDC_TOSEND );
case IDC_BUTTON1:return OnButton1( hWnd );
case IDC_BUTTON2:return OnButton2( hWnd );
case IDC_BUTTON3:return OnButton3( hWnd );
case IDC_BUTTON4:return OnButton4( hWnd );
}
return 0;
}
//LRESULT OnResetField( HWND hWnd, int ID )
//{
// SetDlgItemText( hWnd, ID, "" );
// return 0;
//}
void ToggleConnectedState( HWND hWnd, BOOL bConnected )
{
//
// This function toggles the user interface between
// connected and disconnected states
//
char szCaption[1024] = {0};
char szHost[512] = {0};
int iRemotePort = 0;
switch( bConnected )
{
case TRUE:
SetDlgItemText( hWnd, IDC_CONNECT, "&Disconnect" );
//EnableWindow( GetDlgItem( hWnd, IDC_SEND ), TRUE );
m_Socket.get_RemoteHost( szHost, 512 ); // Get remote host and IP address from socket
m_Socket.get_RemotePort( &iRemotePort );
wsprintf( szCaption, "winSocket - %s : %d", szHost, iRemotePort );
SetWindowText( hWnd, szCaption );
break;
case FALSE:
SetDlgItemText( hWnd, IDC_CONNECT, "&Connect" );
//EnableWindow( GetDlgItem( hWnd, IDC_SEND ), FALSE );
SetWindowText( hWnd, "winSocket test application" );
break;
}
m_bConnected = bConnected;
}
//void DisableForConnect( HWND hWnd, BOOL bEnable )
//{
// EnableWindow( GetDlgItem( hWnd, IDC_CONNECT ), bEnable );
// EnableWindow( GetDlgItem( hWnd, IDC_SEND ), bEnable );
// EnableWindow( GetDlgItem( hWnd, IDC_RESETRECEIVE ), bEnable );
// EnableWindow( GetDlgItem( hWnd, IDC_RESETSEND ), bEnable );
//}
void EnableEdits( HWND hWnd, BOOL bEnable )
{
EnableWindow( GetDlgItem( hWnd, IDC_HOST ), bEnable );
EnableWindow( GetDlgItem( hWnd, IDC_PORT ), bEnable );
}
void EditPrint( HWND hWnd, char *format, ... )
{
char szData[1024] = {0};
va_list pArgList;
va_start( pArgList, format );
wvsprintf( szData, format, pArgList );
va_end( pArgList );
SetDlgItemText( hWnd, IDC_RECEIVED, szData );
}
void EditAppendPrint( HWND hWnd, char *szOutput, ... )
{
HWND hWndEdit;
int length;
char szBuffer[2048] = {0};
va_list pArgList ;
va_start ( pArgList, szOutput );
wvsprintf ( szBuffer, szOutput, pArgList );
va_end ( pArgList );
hWndEdit = GetDlgItem( hWnd, IDC_RECEIVED );
length = SendMessage( hWndEdit, WM_GETTEXTLENGTH , 0, 0 );
SendMessage( hWndEdit, EM_SETSEL, (WPARAM)length, (LPARAM)length );
SendMessage( hWndEdit, EM_REPLACESEL, (WPARAM)FALSE, (LPARAM)szBuffer );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -