views:

104

answers:

2

Hello, 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. 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
}
+1  A: 

I don't see any cin calls here. As for the calls to cout, you may have to flush the output, as it is being called in a separate thread. You can do this by simply calling std::endl:

cout << "New connection." << std::endl;
Vijay Mathew
+1 This has been my problem a few times. Also instead of "endl" you can use "flush" if you don't want the actual endline character.
kigurai
Thank you for both your answers. I've been trying the cin, but removed it. Activation the console worked, thanks. I am still having problems with connection, but I think I can handle it. Thanks again! :)
Neo_b
It might be an idea to accept the answer which helped you then...
Len Holgate
+1  A: 

The reason that your cout calls aren't showing up is possibly because you're supplying the wrong parameters to the linker. Are you specifying /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...

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.

By the way, 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...

Len Holgate