如何将客户端重新连接到服务器? [英] How to reconnect the clients to server?

查看:253
本文介绍了如何将客户端重新连接到服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器程序(套接字流)正在运行,并且接受客户端.由于某些异常情况,服务器将被终止.对方客户端正在等待服务器回复. 如何将正在运行的客户端重新连接到新服务器?插座有什么功能吗?

My server program (socket stream) is running and it accepts the clients. Due to some abnormal condition, server is getting terminated. The other side clients are waiting for server reply. How to I reconnect the running clients to new server? any functions in sockets?

推荐答案

曾经被connect()使用过的套接字 不能不能被重用,并再次调用.

A socket that had been connect()ed once cannot be reused with another call to connect().

连接到TCP服务器并读取/写入一些数据的步骤如下(伪代码):

The steps to connect to a TCP server and read/write some data are as follows (pseudo code):

fd = socket(...) // create socket descriptor (allocate socket resource)
connect(fd, server-address, ...) // connect to server
read/write(fd, data)  // read from server 
close(fd) // close /socket descriptor (free socket resource)

如果服务器在connect之后关闭,则所有客户端可以并且应该做的是

In case the server goes down after connect all the client could and shall do is

close(fd) // close /socket descriptor (free socket resource)

,然后重新开始:

fd = socket(...) // create socket descriptor (allocate socket resource)
...

从头开始:

connect(fd, server-address, ...) // connect to server
...

可能会导致不确定的行为,但至少会导致错误.

would probably lead to undefined behaviour, but at least to an error.

这篇关于如何将客户端重新连接到服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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