Y
yadanwutuobang
Unregistered / Unconfirmed
GUEST, unregistred user!
下面是网上的源码,执行有错,改了多边,还是不行,把ip换成了127.0.0.1,可以获得本机的聊天记录吗?如果不行,怎样修改?改成命令行程序即可,谢谢啦
#include <stdlib.h>
#include <stdio.h>
#include <winsock2.h>
#include <winldap.h>
//lib
#pragma comment (lib,"ws2_32.lib")
#pragma comment (lib,"Wldap32.lib")
//struct
typedef struct IPV4_HDR{
BYTE byVerLen;
BYTE byTOS;
WORD wTotalLength;
WORD wID;
WORD wOffset;
BYTE byTTL;
BYTE byProtocol;
WORD wCheckSum;
DWORD dwSrcAddr;
DWORD dwDestAddr;
}* LPIPV4_HDR;
typedef struct TCP_HDR{
WORD wSrcPort;
WORD wDestPort;
DWORD dwSEQ;
DWORD dwACK;
WORD wLenFlag;
WORD wWinSize;
WORD wCheckSum;
WORD wURG;
}* LPTCP_HDR;
//define
#define WM_SOCKET WM_USER + 1
#define SIO_RCVALL IOC_IN | IOC_VENDOR | 1
char szWindowClass[] = "MyWindow";
// Global Variables:
HWND hBox;
//functions
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
static SOCKET mySock = INVALID_SOCKET;
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASS wc;
wc.cbClsExtra = sizeof(WNDCLASS);
memset(&wc, 0, sizeof(WNDCLASS));
wc.style = CS_CLASSDC;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = szWindowClass;
return RegisterClass(&wc);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hWnd = CreateWindow(szWindowClass, NULL, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
WSADATA WSAData;
sockaddr_in svrAddr;
int nOptVal = 1;
DWORD cbBytesReturned;
char szBuffer[0xFFFF];
WCHAR szData[0xFFFF];
char* pBuffer;
LPIPV4_HDR LPIPHEADER;
LPTCP_HDR TCPHEADER;
LPCSTR lpData;
int nHeaderLen;
int nDataLen;
RECT rc;
int nWLen;
HFILE hFile;
OFSTRUCT ReOpen;
DWORD dwWriten;
switch (message)
{
case WM_CREATE:
//Memo
hBox = CreateWindow("Edit",
"MyListBox",
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_MULTILINE,
0,
0,
0,
0,
hWnd,
NULL,
0,
NULL);
//Socket
WSAStartup(0x0202, &WSAData);
mySock = WSASocket(AF_INET,
SOCK_RAW,
IPPROTO_IP,
NULL,
NULL,
WSA_FLAG_OVERLAPPED );
if (mySock != INVALID_SOCKET)
{
svrAddr.sin_family = AF_INET;
svrAddr.sin_port = htons(0);
svrAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
if (bind(mySock,
(const struct sockaddr*)&svrAddr,
sizeof(svrAddr))!=SOCKET_ERROR)
{
if (WSAIoctl(mySock,
SIO_RCVALL,
&nOptVal,
sizeof(int),
NULL,
0,
&cbBytesReturned,
NULL,
NULL) != SOCKET_ERROR)
{
WSAAsyncSelect(mySock,
hWnd,
WM_SOCKET,
FD_READ);
}
}
}
return DefWindowProc(hWnd, message, wParam, lParam);
break;
case WM_SIZE:
GetClientRect(hWnd, &rc);
SetWindowPos(hBox,HWND_BOTTOM, 0, 0, rc.right, rc.bottom, SWP_SHOWWINDOW);
break;
case WM_DESTROY:
hFile = OpenFile("c://log.txt", &ReOpen, OF_CREATE);
if (hFile !=HFILE_ERROR)
{
for(int i = 0;
i <= SendMessage(hBox, LB_GETCOUNT, 0, 0) -1;
i++)
{
SendMessage(hBox, LB_GETTEXT, i, (LPARAM)szBuffer);
WriteFile((HANDLE)hFile,
szBuffer,
SendMessage(hBox, LB_GETTEXTLEN, i, 0),
&dwWriten,
NULL);
WriteFile((HANDLE)hFile, "/n", 1, &dwWriten, NULL);
}
CloseHandle((HANDLE)hFile);
}
closesocket(mySock);
WSACleanup();
PostQuitMessage(0);
break;
case WM_SOCKET:
nDataLen = recv(mySock, szBuffer, sizeof(szBuffer), 0);
LPIPHEADER = (LPIPV4_HDR)szBuffer;
if (LPIPHEADER->byProtocol == IPPROTO_TCP)
{
TCPHEADER = (LPTCP_HDR)&szBuffer[(LPIPHEADER->byVerLen &
0x0F)*4];
if (ntohs(TCPHEADER->wSrcPort) == 1863 ||
ntohs(TCPHEADER->wDestPort) == 1863)
{
nHeaderLen = (LPIPHEADER->byVerLen &
0x0F)*4 +
(ntohs(TCPHEADER->wLenFlag) >> 12) * 4;
lpData = (LPCSTR)&szBuffer[nHeaderLen] ;
if (strstr(lpData, "MSG") &&
strstr(lpData, "text/plain"))
{
nWLen = LdapUTF8ToUnicode(lpData,
nDataLen - nHeaderLen,
(LPWSTR)szData,
sizeof(szData));
WideCharToMultiByte(CP_ACP,
WC_COMPOSITECHECK,
szData,
nWLen,
szBuffer,
sizeof(szBuffer),
NULL,
NULL);
int nSize = GetWindowTextLength(hBox) + strlen(szBuffer) + 1;
pBuffer = (char*)malloc(nSize);
memset(pBuffer, 0, nSize);
MessageBox(0, (char*)((long)pBuffer + GetWindowText(hBox, pBuffer, GetWindowTextLength(hBox))), NULL, 0);
memcpy((void*)((long)pBuffer + GetWindowText(hBox, pBuffer, GetWindowTextLength(hBox))), szBuffer, strlen(szBuffer));
SendMessage(hBox,
WM_SETTEXT,
0,
(LPARAM)pBuffer);
free(pBuffer);
}
}
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <winsock2.h>
#include <winldap.h>
//lib
#pragma comment (lib,"ws2_32.lib")
#pragma comment (lib,"Wldap32.lib")
//struct
typedef struct IPV4_HDR{
BYTE byVerLen;
BYTE byTOS;
WORD wTotalLength;
WORD wID;
WORD wOffset;
BYTE byTTL;
BYTE byProtocol;
WORD wCheckSum;
DWORD dwSrcAddr;
DWORD dwDestAddr;
}* LPIPV4_HDR;
typedef struct TCP_HDR{
WORD wSrcPort;
WORD wDestPort;
DWORD dwSEQ;
DWORD dwACK;
WORD wLenFlag;
WORD wWinSize;
WORD wCheckSum;
WORD wURG;
}* LPTCP_HDR;
//define
#define WM_SOCKET WM_USER + 1
#define SIO_RCVALL IOC_IN | IOC_VENDOR | 1
char szWindowClass[] = "MyWindow";
// Global Variables:
HWND hBox;
//functions
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
static SOCKET mySock = INVALID_SOCKET;
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASS wc;
wc.cbClsExtra = sizeof(WNDCLASS);
memset(&wc, 0, sizeof(WNDCLASS));
wc.style = CS_CLASSDC;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = szWindowClass;
return RegisterClass(&wc);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hWnd = CreateWindow(szWindowClass, NULL, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
WSADATA WSAData;
sockaddr_in svrAddr;
int nOptVal = 1;
DWORD cbBytesReturned;
char szBuffer[0xFFFF];
WCHAR szData[0xFFFF];
char* pBuffer;
LPIPV4_HDR LPIPHEADER;
LPTCP_HDR TCPHEADER;
LPCSTR lpData;
int nHeaderLen;
int nDataLen;
RECT rc;
int nWLen;
HFILE hFile;
OFSTRUCT ReOpen;
DWORD dwWriten;
switch (message)
{
case WM_CREATE:
//Memo
hBox = CreateWindow("Edit",
"MyListBox",
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_MULTILINE,
0,
0,
0,
0,
hWnd,
NULL,
0,
NULL);
//Socket
WSAStartup(0x0202, &WSAData);
mySock = WSASocket(AF_INET,
SOCK_RAW,
IPPROTO_IP,
NULL,
NULL,
WSA_FLAG_OVERLAPPED );
if (mySock != INVALID_SOCKET)
{
svrAddr.sin_family = AF_INET;
svrAddr.sin_port = htons(0);
svrAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
if (bind(mySock,
(const struct sockaddr*)&svrAddr,
sizeof(svrAddr))!=SOCKET_ERROR)
{
if (WSAIoctl(mySock,
SIO_RCVALL,
&nOptVal,
sizeof(int),
NULL,
0,
&cbBytesReturned,
NULL,
NULL) != SOCKET_ERROR)
{
WSAAsyncSelect(mySock,
hWnd,
WM_SOCKET,
FD_READ);
}
}
}
return DefWindowProc(hWnd, message, wParam, lParam);
break;
case WM_SIZE:
GetClientRect(hWnd, &rc);
SetWindowPos(hBox,HWND_BOTTOM, 0, 0, rc.right, rc.bottom, SWP_SHOWWINDOW);
break;
case WM_DESTROY:
hFile = OpenFile("c://log.txt", &ReOpen, OF_CREATE);
if (hFile !=HFILE_ERROR)
{
for(int i = 0;
i <= SendMessage(hBox, LB_GETCOUNT, 0, 0) -1;
i++)
{
SendMessage(hBox, LB_GETTEXT, i, (LPARAM)szBuffer);
WriteFile((HANDLE)hFile,
szBuffer,
SendMessage(hBox, LB_GETTEXTLEN, i, 0),
&dwWriten,
NULL);
WriteFile((HANDLE)hFile, "/n", 1, &dwWriten, NULL);
}
CloseHandle((HANDLE)hFile);
}
closesocket(mySock);
WSACleanup();
PostQuitMessage(0);
break;
case WM_SOCKET:
nDataLen = recv(mySock, szBuffer, sizeof(szBuffer), 0);
LPIPHEADER = (LPIPV4_HDR)szBuffer;
if (LPIPHEADER->byProtocol == IPPROTO_TCP)
{
TCPHEADER = (LPTCP_HDR)&szBuffer[(LPIPHEADER->byVerLen &
0x0F)*4];
if (ntohs(TCPHEADER->wSrcPort) == 1863 ||
ntohs(TCPHEADER->wDestPort) == 1863)
{
nHeaderLen = (LPIPHEADER->byVerLen &
0x0F)*4 +
(ntohs(TCPHEADER->wLenFlag) >> 12) * 4;
lpData = (LPCSTR)&szBuffer[nHeaderLen] ;
if (strstr(lpData, "MSG") &&
strstr(lpData, "text/plain"))
{
nWLen = LdapUTF8ToUnicode(lpData,
nDataLen - nHeaderLen,
(LPWSTR)szData,
sizeof(szData));
WideCharToMultiByte(CP_ACP,
WC_COMPOSITECHECK,
szData,
nWLen,
szBuffer,
sizeof(szBuffer),
NULL,
NULL);
int nSize = GetWindowTextLength(hBox) + strlen(szBuffer) + 1;
pBuffer = (char*)malloc(nSize);
memset(pBuffer, 0, nSize);
MessageBox(0, (char*)((long)pBuffer + GetWindowText(hBox, pBuffer, GetWindowTextLength(hBox))), NULL, 0);
memcpy((void*)((long)pBuffer + GetWindowText(hBox, pBuffer, GetWindowTextLength(hBox))), szBuffer, strlen(szBuffer));
SendMessage(hBox,
WM_SETTEXT,
0,
(LPARAM)pBuffer);
free(pBuffer);
}
}
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}