简体   繁体   中英

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) , 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM