简体   繁体   English

C ++ Winsock将冻结接收数据

[英]C++ Winsock Will Freeze Receiving Data

I'm making a program with Code::Blocks in C++ To Send and Receive data with Winsocks but when I try to receive data my program freezes, please help. 我正在用C ++中的Code :: Blocks编写程序,以使用Winsocks发送和接收数据,但是当我尝试接收数据时,程序冻结了,请帮忙。 I don't know why? 不知道为什么 I was able to get just 1 line without using a loop so I added a loop to see if I could get more lines that come in, and now it just freezes. 我不使用循环就只能得到1行,所以我添加了一个循环以查看是否可以插入更多行,现在它冻结了。

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include "resource.h"
#include <winsock.h>

using namespace std;

SOCKET mysocket;
SOCKADDR_IN SockAddr;
char buf1[120];
char buf2[120];
char ReadIp[120];
int UsePort;
int n;

HINSTANCE hInst;

BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_INITDIALOG:
            WSADATA WsaDat;
            WSAStartup(MAKEWORD(1,1), &WsaDat);
            mysocket = socket(AF_INET, SOCK_STREAM, 0);
            return TRUE;

        case WM_CLOSE:
            EndDialog(hwndDlg, 0);
            return TRUE;

        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case IDC_BTN_QUIT:
                    EndDialog(hwndDlg, 0);
                    return TRUE;

                case BtnSend:
                    GetDlgItemText(hwndDlg, PacketText, buf2, sizeof(buf2));
                    send(mysocket, buf2, sizeof(buf2), 0);
                    return TRUE;

                case IDC_BTN_TEST:
                    UsePort = GetDlgItemInt(hwndDlg, PortText, NULL, FALSE);
                    GetDlgItemText(hwndDlg, IpText, ReadIp, sizeof(ReadIp));
                    SockAddr.sin_port = htons(UsePort);
                    SockAddr.sin_addr.s_addr=inet_addr(ReadIp);
                    SockAddr.sin_family = AF_INET;
                    connect(mysocket, (SOCKADDR *)(&SockAddr), sizeof(SockAddr));
                    do {
                        n = recv(mysocket, buf1, sizeof(buf1), 0);
                    if (n > 0)
                        SetDlgItemText(hwndDlg, List1, buf1);
                    } while (n > 0);
                    return TRUE;
                    }
    }

    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst = hInstance;

    // The user interface is a modal dialog box
    return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DialogProc);
}

It freezes because it's waiting for data. 它冻结,因为它正在等待数据。 If you don't want to wait for data, don't make a Winsock call that does that. 如果您不想等待数据,请不要进行Winsock调用。 Use one of the many non-blocking I/O methods available on Windows such as Async operations , I/O completion ports or completion events. 使用Windows上可用的许多非阻塞I / O方法之一,例如Async操作I / O完成端口或完成事件。

By the way, your code seems badly broken in many ways. 顺便说一下,您的代码在许多方面似乎都被严重破坏了。 It seems to be missing a protocol . 它似乎缺少协议 You can't use TCP without designing and implementing a protocol on top of TCP. 如果不在TCP之上设计和实现协议,就不能使用TCP。 You will get totally unreliable behavior if you don't process the data you've received through some sort of protocol engine that reconstructs the transmitted information. 如果不处理通过某种构造重新构造所传输信息的协议引擎处理的数据,则将获得完全不可靠的行为。 For example, in your case, if someone sends "foo", the final text will depend on how the data got assembled for transport. 例如,在您的情况下,如果有人发送“ foo”,则最终文本将取决于如何组装数据以进行传输。 It might be "foo", but it might be "o" since the "fo" can be overwritten. 它可能是“ foo”,但可能是“ o”,因为可以覆盖“ fo”。

If the other side is sending lines , as you claim, you need to code this end to receive lines. 如您所言,如果另一端正在发送线路 ,则需要对此端进行编码以接收线路。 That is, you need to keep receiving data until you have a line and not let later receive overwrite earlier receives so that you can re-assemble the transmitted line. 也就是说,您需要一直接收数据,直到有一行为止,并且不要让以后的接收覆盖之前的接收,以便您可以重新组装传输的线路。 The code to actually implement the receiving of lines is totally missing in the code you pasted. 您粘贴的代码中完全缺少实际实现行接收的代码。 (If, for example, a "line" is defined as a bunch of bytes terminated by a newline character, there needs to be code to search for the newline characters and assemble everything before them into a single buffer for processing.) (例如,如果“行”定义为以换行符结尾的一堆字节,则需要有代码来搜索换行符并将它们之前的所有内容组合到单个缓冲区中以进行处理。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM