地址已在使用中...套接字在C [英] Address already in use... sockets in C

查看:145
本文介绍了地址已在使用中...套接字在C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题以前被问过,但我只是不能看到它作为小细节的所有问题,我曾经见过把我关闭...

This question has been asked before, yet I just can't seem to come to grips with it as small details of all the questions ive ever seen throw me off...

我仍然有问题,当我做一个TCP套接字服务器,端口变得不可用的X ammount的时间如果我建立一个连接(如果连接不是与客户端建立,我可以重新运行程序后终止无问题)。这是明显的一些超时TCP要求之前,它将删除连接。

I keep having issues where when I make a TCP socket server, the port becomes unusable for X ammount of time IF I establish a connection (if the connection is not established with the client, I can re-run the program after termination without issue). This is aparently somthing to do with a timeout that TCP requires before it will drop the connection.

我在服务器和客户端的套接字使用close(socketfd)没有prevail ...有没有办法使套接字成功终止后使用(如,在fd上调用close),而不必等待什么似乎永远或重新启动机器?

I am using close(socketfd) on both the server and the client's socket with no prevail... Is there a way to make the socket IMMEDIATLY avaliable for use after successful termination (as in, close called on the fd) without having to wait what seems forever or restart the machine?

推荐答案

是的,相关的套接字选项称为 SO_REUSEADDR 。您可以使用 setsockopt() 设置套接字选项。

Yes, the relevant socket option is called SO_REUSEADDR. You use setsockopt() to set socket options.

你通常不应该关闭服务器的主socket,而应该使用 accept() 创建每个客户端套接字,当客户端断开连接时可以关闭。

You should normally not close the server's main socket though, instead you should use accept() to create per-client sockets, which can be closed when the client disconnects.

这篇关于地址已在使用中...套接字在C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆