是否应该关闭损坏的插座?

时间:2018-08-26 17:12:04

标签: c sockets tcp

我正在读取TCP套接字:

int read_result = recv(socket_fd, &some_struct, some_size, 0);

如果read_result等于-1,我还是应该在该文件描述符上调用close还是保留它?

1 个答案:

答案 0 :(得分:3)

根据public Transform WorldCenterPosition; ... void Start () { _cameraOffset = transform.position - PlayerTransform.position; } // Update is called once per frame void Update () { // Direction from World center to Player position Vector3 direction = PlayerTransform.position - WorldCenterPosition.position; // Distance for camera to be away (distance from player to world center // plus distance from player to camera) float distance = direction.magnitude + _cameraOffset.magnitude; // Direction should be unit vector direction = direction.normalized; // Multiply our unit direction by calculated distance Vector3 newPos = WorldCenterPosition.position + (direction*distance); transform.position = Vector3.Slerp(transform.position, newPos, SmoothFactor); // I suppose camera should look at player transform.LookAt (PlayerTransform); } 手册页,recv可能返回recv的原因有很多(EAGAIN,EBADF,EINVAL,ENOMEM等)。我建议对照这些预期的返回值检查-1并修改您的代码以采取相应的措施。如果要编写库,则可能要返回库特定的错误代码。如果您是应用程序,则可能要死亡或将错误代码返回给调用方。这取决于上下文。

但是,解决此问题的第一步是了解可以返回的各种错误代码,并相应地进行处理。