简体   繁体   English

多线程无法按预期工作

[英]Multithreading won't work as expected

I have a problem with my program. 我的程序有问题。 I wanted it to have two threads, one of them listening for connections, and the other one receiving data from them... Unfortunately, it acts strangely. 我希望它有两个线程,一个线程监听连接,另一个线程从中接收数据……不幸的是,它的行为很奇怪。 It will ignore my cout and cin usage everywhere in the code, so I can't even debug it. 在代码的任何地方,它将忽略我的cout和cin用法,因此我什至无法调试它。 May I ask that someone sheds some light on it? 我可以要求有人对此有所说明吗? Thank you in advance. 先感谢您。

#include <windows.h>
#include <iostream.h>
#include <string.h>
#include <cstdlib>

int ConnectionNum, Port=4673;
WSADATA wsaData;
SOCKET Connections[256];

DWORD WINAPI ReceiveThread(LPVOID iValue)
{
    //this is going to be receiving TCP/IP packets, as soon as the connection works
}

DWORD WINAPI ListenThread(LPVOID iValue) //this thread is supposed to listen for new connections and store them in an array
{
    SOCKET ListeningSocket;
    SOCKET NewConnection;
    SOCKADDR_IN ServerAddr;
    SOCKADDR_IN ClientAddr;
    int ClientAddrLen;

    WSAStartup(MAKEWORD(2,2), &wsaData);
    ListeningSocket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    ServerAddr.sin_family=AF_INET;
    ServerAddr.sin_port=htons(Port);
    ServerAddr.sin_addr.s_addr=htonl(INADDR_ANY);

    bind(ListeningSocket, (SOCKADDR*)&ServerAddr, sizeof(ServerAddr));

    if(listen(ListeningSocket, 5)!=0)
    {
        cout << "Could not begin listening for connections.";
        return 0;
    }

    ConnectionNum=0;

    while(ConnectionNum<256)
    {
        Connections[ConnectionNum]=accept(ListeningSocket, (SOCKADDR*)&ClientAddr, &ClientAddrLen);
        ConnectionNum++;

        cout << "New connection.";
    }
}

int main()
{
    HANDLE hThread1,hThread2;
    DWORD dwGenericThread;
    char lszThreadParam[3];

    hThread1=CreateThread(NULL, 0, ListenThread, &lszThreadParam, 0, &dwGenericThread);

    if(hThread1==NULL)
    {
        DWORD dwError=GetLastError();
        cout<<"SCM:Error in Creating thread"<<dwError<<endl ;
        return 0;
    }

    hThread2=CreateThread(NULL, 0, ReceiveThread, &lszThreadParam, 0, &dwGenericThread);

    if(hThread2==NULL)
    {
        DWORD dwError=GetLastError();
        cout<<"SCM:Error in Creating thread"<<dwError<<endl ;
        return 0;
    }

    system("pause"); //to keep the entire program from ending
}

I don't see any cin calls here. 我没有看到任何CIN调用这里。 As for the calls to cout , you may have to flush the output, as it is being called in a separate thread. 至于对cout的调用,您可能必须刷新输出,因为它是在单独的线程中调用的。 You can do this by simply calling std::endl : 您可以通过简单地调用std :: endl来实现

cout << "New connection." << std::endl;

The reason that your cout calls aren't showing up is possibly because you're supplying the wrong parameters to the linker. 您的cout调用未显示的原因可能是因为您向链接器提供了错误的参数。 Are you specifying /SUBSYSTEM:CONSOLE ? 您要指定/SUBSYSTEM:CONSOLE吗? (System tab of the Linker properties). (链接器属性的“系统”选项卡)。 If not then you're not telling the operating system to create a console for the program, you may be telling it it's a windows program, and if you program doesn't have a console then you wont see your programs cout output... 如果不是,那么您不是在告诉操作系统为该程序创建控制台,您可能是在告诉它它是Windows程序,如果您的程序没有控制台,那么您将看不到程序cout输出...

Once you can see your debug... 一旦您可以看到调试...

I assume you are connecting to your test program from a client of some sort? 我假设您正在从某种客户端连接到测试程序? Nothing will happen until you connect to your program which will cause the call to Accept() to return. 在连接到程序之前,什么都不会发生,这将导致对Accept()的调用返回。

By the way, system("pause"); 顺便说一下, system("pause"); is probably the worst way to achieve what you want but I assume you're only doing that because you can't get cin to work... 这可能是实现所需目标的最糟糕的方法,但我想您只是这样做,因为您无法让cin工作。

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

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