Getting “Address already in use” error using Unix socket

[亡魂溺海] 提交于 2019-12-04 03:25:10

You should unlink() the path file before bind call. You will get this error when file exists during the bind. Either you should ensure to unlink/remove the file before exiting the application or you could always unlink it before bind.

Check man page of bind. Also, note the example given in the man page at the end.

You can try to use the SO_REUSEADDR flag like so:

int yes = 1;
if (setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
    // error handling
    exit(1);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!