C language: “can’t bind to the port” problem (Windows)


Sometimes your program process can stuck and trigger error like this:

Couldn’t bind to the port

Ok, we have such check in out sockets C code:

if (bind (socket_desc, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0)
printf("Couldn't bind to the port\n");

And it return negative value.. How to fix it?

  1. find which process using this port via cmd:
    netstat -ano | findstr :<port number>
    For example:
    C:\Users\n>netstat -ano | findstr :2000
    You will see such message:
    TCP 127.0.0.1:2000 0.0.0.0:0 LISTENING 3560
  2. last number is ID of process. To kill process:
    taskkill /pid <process ID> /F
    Example:
    C:\Users\n>taskkill /pid 3560 /F
    SUCCESS: The process with PID 3560 has been terminated.

Enjoy!


This entry was posted in C language (en). Bookmark the permalink.

Leave a Reply

🇬🇧 Attention! Comments with URLs/email are not allowed.
🇷🇺 Комментарии со ссылками/email удаляются автоматически.