Hello, i've tried to use Windows sockets not in my LAN and i've noticed that every time my program starts it gets CPU overflowed weirdly

ProgramConfig myConfig = ExecuteServerConfig("config.ini");
    WSAData wData;
    if(WSAStartup(MAKEWORD(2,2),&wData) == -1)return 0;
    
    sockaddr_in serveraddr;
    int sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
   
    serveraddr.sin_port = htons(myConfig.Port);
    serveraddr.sin_addr.s_addr = inet_addr(myConfig.Server);
    serveraddr.sin_family = AF_INET;
    
    bind(sock,(sockaddr*)&serveraddr,sizeof(serveraddr));
    listen(sock,1);
    sockaddr_in clientaddr;
    int size = sizeof(clientaddr);
    while(1)
    {
        int csocket = accept(sock,(sockaddr*)&clientaddr,&size);
        int byteswritten;
        char buffer[256];
        while(recv(csocket,buffer,sizeof(buffer),0) != -1)
        {
            CommandHandler(buffer);
            free(buffer);
        }
    }

my client socket is always the same client with the same ip, that connects without stopping even when i'm changing port in my config file.
what is this weird thing?

Recommended Answers

All 2 Replies

You probably should declare clientaddr as a SOCKADDR_STORAGE rather than a sockaddr_in to ensure that clientaddr is large enough for any data that accept might write to it.

However that may not actually cure your problem.

it gets CPU overflowed weirdly

...what? You should really post an accurate error description in the future.

As to your problem (or one of them), you're trying to free buffer even though you allocated it on the stack. So that inevitably leads to stack corruption.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.