Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8867743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:06:52+00:00 2026-06-14T17:06:52+00:00

Possible Duplicate: Detect When Network Cable Unplugged I would need some help to check

  • 0

Possible Duplicate:
Detect When Network Cable Unplugged

I would need some help to check if somebody disconnected. At the moment my code says that somebody disconnected every time I connect to it. I tried to use send() to check if they are still “alive” but I’m not sure if I did it right.

#pragma comment (lib,"Ws2_32.lib")

#include <iostream>
#include <WinSock2.h>

using namespace std;

#pragma region shittyvars

int countcons = 0, connections[100];
char *dead = new char;
int error;
WSAData netinfo;
int mySocket;
HANDLE acceptth, discth;

#pragma endregion

void AcceptThread( int* a)
{
int clientSocket;
while ( (clientSocket = accept(mySocket,0,0))!=SOCKET_ERROR) 
   connections[countcons++]=clientSocket;
}
void DissconectThread(int*b)
{
for(;;)
  for(int i=0;i<countcons;i++)
   {
       error = send(connections[i],dead,4,0);
 error = WSAGetLastError();
       if(error != 0 )
       {
           closesocket(connections[i]);
           for(int j=i+1 ; j<countcons ;j++)
               connections[j-1]=connections[j];
           countcons--;
           cout<<"\nSomebody disconnected\n";
       }
   }
}

int main() {
#pragma region InitConnection
    error = WSAStartup(MAKEWORD(2,2), &netinfo);

    if (error == SOCKET_ERROR) {
        cout << "Error:Starting socket failed";
        return 1;
    }

    mySocket = socket(AF_INET, SOCK_STREAM, 0);

    if (mySocket == SOCKET_ERROR) {
        cout << "Error:Socket failed to initialize!";
        return 1;
    }
    cout << "Works!";

    struct sockaddr_in server; // network socket struct

    server.sin_family = AF_INET;
    server.sin_port = htons(7654);
    server.sin_addr.s_addr = INADDR_ANY; // Let's us accept any connection

    error = bind(mySocket, (sockaddr*) &server, sizeof(server));

    if (error == SOCKET_ERROR) {
        cout << "Error:Failed to bind socket";
        return 1;
    }

    error = listen(mySocket, 100);

    if (error == SOCKET_ERROR) {
        cout << "Error:The socket is deaf";
        return 1;
    }

#pragma endregion

    acceptth = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) AcceptThread,
            &server, 0, (LPDWORD) &acceptth);
    discth = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) DissconectThread,
            &server, 0, (LPDWORD) &discth);
    getchar();
    closesocket(mySocket);
    WSACleanup();
    getchar();
    return 0;
}

How do I solve this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:06:54+00:00Added an answer on June 14, 2026 at 5:06 pm

    Check the error you get when the send fails… it might point you in the right direction (use WSAGetLastError() on Windows). Incidentally, your DissconectThread will just spin and chew CPU until there’s a connection, then it will jam out data until it blocks on one connection with a full send buffer… probably not what you intended.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Best way to detect integer overflow in C/C++ how do we check
Possible Duplicate: Best way to detect when user leaves a web page Check if
Possible Duplicate: How can I detect the browser with PHP or JavaScript? I need
Possible Duplicate: Detect Antivirus on Windows using C# In .net I would like to
Possible Duplicate: empty field validation in jquery I need to detect if a form
Possible Duplicate: How to detect if a variable is an array When I need
Possible Duplicate: How to detect if a function is called as constructor? I would
Possible Duplicate: Detect internet Connection using Java How to check if internet connection is
Possible Duplicate: How to detect iPhone 5 (widescreen devices)? Do any adjustments need to
Possible Duplicate: Detect multitouch with Ontouch listener Android I have a question about detecting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.