telnet 服务端发送什么样的socket数据包让客户端断开连接?

如果只是close(connext_socket)客户端telnet程序并不会退出。

有的telnet服务端在登录的过程中,密码输错了会自动退出telnet显示

Connection closed by foreign host.

我稍微看了一下telnet协议,好像是返回已字节IAC(0xFF)开始的数据包

char over[10] = "";
sprintf(over, "%c%c", 255,244);// IP 244 终止进程
send(connect_d, over, strlen(over), 0);
close(connect_d);

然而客户端并没有退出。

阅读 6.5k
1 个回答

和 telnet 没什么关系。

调用 shutdown() 关闭socket连接即可。

之所以客户端不断开连接,使用我写的是多进程的TCP服务,子进程和父进程其实是共享某些资源的,在子进程中用close关闭socket只是将socket的引用计数减一,而不会真正的去关闭。要想关闭去使用shutdown().

深入分析tcp close与shutdown

TCP FIN not sent on doing 'close ()' for a multi-threaded TCP client
Call shutdown(WR) will send FIN,but close have little different:if fd reference count not equal 0 will not send FIN.

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题