Home > Software engineering >  Winsock2 only create the same socket
Winsock2 only create the same socket

Time:09-20

I'm working on a server/client chat room for school.

Everything worked fine until I decided to get inputs of my Clients. Since then, I don't know why but everytime I create a new socket, it will always be the same. I've tried to create a new solution, and just create a socket and even their.. sockets are the sames.

My test code as simple as that :

#pragma comment (lib, "Ws2_32.lib")
#include <WinSock2.h>
#include <iostream>

int main()
{
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &wsaData))
    {
        return 1;
    }

    // Create Socket
    SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);

    char a;

    std::cout << "Socket : " << sock << std::endl;
    std::cin >> a;
}

I end up with 4 times the same socket is created:

image

Some time, weirdly it work completly fine, but shortly after it get back to that.

CodePudding user response:

Unlike on other platforms, where sockets are indexes into a per-process file table, sockets on Windows are kernel objects. When a process exits, any open objects are released automatically, allowing the kernel to reuse them. This is perfectly normal behavior.

CodePudding user response:

i hasd the same problem but it kinda fixed itself so i'm curious to see what kind of answer i could find on it

  • Related