Client connects and disconnects x2


  • Hello,

    I'm currently creating a application to work with a server that I am creating. The server is a win32 console application and the client is C++ windows store application, however I seem to have an issue with my code somewhere.

    The server outputs:

    Server: A client has connected. then Server: A client has disconnected this happens twice.

    Connection handler:

    for (;;) { while (socketConnection = accept(socketListen, (SOCKADDR*)&Address, &AddressSize)) { if (socketConnection == INVALID_SOCKET) { closesocket(socketConnection); break; } unsigned ThreadID; HANDLE clientHandle = (HANDLE)_beginthreadex(NULL, 0, &ClientSession, (void*)socketConnection, 0, &ThreadID); Success = send(socketConnection, "You have connected to the server.", 120, NULL); printf("Server: A client has connected.\n"); } }

    Disconnect handler:

    unsigned __stdcall ClientSession(void *data)
    	while (true)
    		int Result;
    		char buf[BUF_LEN];
    		Result = recv(socketConnection, buf, BUF_LEN, NULL);
    		string converter;
    		converter = buf;
    		if (Result == -1)
    			if (errno == 0)
    				printf("Server: client has disconnected.\n");
    				cout << "Server: Error whilst listening. ErrorID: " << errno << "\n";
    			cout << "Server: Received " << converter << " from client\n";
    	return 1;

    Client side:

    	auto Sock = ref new Windows::Networking::Sockets::StreamSocket();
    	auto localHostName = ref new Windows::Networking::HostName(SERVER_IP);
    	String^ localService(SERVER_PORT);
    	Sock->ConnectAsync(localHostName, localService);

    I'm using sstream for client and Winsock for server.

    I just don't understand why this happens.

    Any help is greatly appreciated.



    EDIT: I would like to add that I've tried using a console application as client and it works fine without any sort of errors

    Wednesday, October 30, 2013 7:25 PM

All replies