开发者

C Socket, verifying server

How does one check if no connection is possible (that is, server is down...) with c sockets? In other words; client tries to establish contact with a

connect(sock, (struct sockaddr *)&serveraddr, sizeof serveraddr)开发者_如何学Go, but server isn't responding. Client should then get hold of some variable that could verify server status, without using read / write?


connect(3) will return -1 on error, and set errno to an appropriate error.

One case that you may have to handle manually is a timeout. For that, you can either use multiple threads (a second thread kills the socket if it's not connected after the timeout expires), or use non-blocking sockets + poll(2). But that should be rare.


1Please verify your socket mode, if you did not change socket mode. Its mode is blocking socket as default.

2.If using the blocking socket mode, and if there is a client had connect the server, because the server is blocking with the current client.

3.If you want your program to support two or more clients to connect with messages, you can Google socket non-blocking, select, poll, epoll or fork new process.

I don’t know if this clears things up or makes it more confusing.Hopes to help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜