TCP连接失败

时间:2010-10-11 14:50:12

标签: c++ sockets tcp

我在使用connect()和read()函数的简单TCP套接字时遇到问题。如果我先启动服务器,然后再启动客户端,则此程序有效。但是,当我首先启动客户端时,它继续尝试连接到服务器,然后当我启动服务器时,它连接但后续读取返回0字节。这表明服务器关闭了套接字,但调试语句不支持此功能。

我正在尝试让客户端不断尝试连接到端口6380上的服务器,然后读取数据。如果连接丢失,客户端将尝试再次重新连接。我为套接字设置了SO_REUSEADDR。还有其他我缺少的东西吗?

服务器的源代码

//***** SERVER *****
#include <iostream>
#include "SocketAdapter.h"
#include "SocketServer.h"
#include <errno.h>
#include <string.h>

unsigned char buffer[] = "123123123";

int main()
{
   SocketAdapter  socket;
   SocketServer   server(6380);

   server.Start();
   std::cout << "Wait for Connection" << std::endl;
   server.WaitForConnection(socket);
   std::cout << "Start to Send Data" << std::endl;

   while(1)
   {
      std::cout << "Sending Data   ";
      int bytesSent = socket.Send(buffer, 10);
      std::cout << "byteSent = " << bytesSent << std::endl;
      if (bytesSent == -1)
      {
         std::cout << "Errno (" << errno << ") - " << strerror(errno) << std::endl;
      }

      sleep(1);
   }//end while

   std::cout << "Ending Server" << std::endl;
   socket.Close();

   return 0;
}

客户端源代码

// ***** SIMPLE CLIENT ******
#include <iostream>
#include "SocketAdapter.h"
#include "SocketClient.h"
#include <string.h>
#include <errno.h>

int main()
{
   SocketAdapter  socket;
   SocketClient   client(6380, "127.0.0.1");

   unsigned char buffer[1024];


   while(1)
   {
      client.Start();

      //Loop till we connect to Server
      while( !client.Connect(socket) )
      {
         std::cout << "Trying to connect to server." << std::endl;
         sleep(1);
      }

      std::cout << "Connected to Server" << std::endl;

      //Read till we get a timeout
      while(1)
      {
         int bytesRead = socket.Recv(buffer, 10,2000);
         if (bytesRead != 10)
         {
            std::cout << "   Failed to Read" << std::endl;
            std::cout << "   bytesRead = " << bytesRead << std::endl;
            std::cout << "   Errno (" << errno << ") - "
                      << strerror(errno) << std::endl;
            break;
         }

         std::cout << "buffer = " << buffer << std::endl;
         usleep(250000);
      }

      std::cout << "Something went wrong, restart" << std::endl;
   }

   return 0;
}

服务器输出

./Server 
Wait for Connection
Start to Send Data
Sending Data   byteSent = 10

客户输出

./Client 
Trying to connect to server.
Trying to connect to server.
Connected to Server
   Failed to Read
   bytesRead = 0
   Errno (107) - Transport endpoint is not connected
Something went wrong, restart
Close Socket
Close Socket File Descriptor
Connected to Server
Trying to Recv on a closed socket
Return Value = -1
Errno        = Connection reset by peer

   Failed to Read
   bytesRead = -3
   Errno (104) - Connection reset by peer
Something went wrong, restart
Close Socket
Close Socket File Descriptor
Socket cannot shutdown!
Return Value = -1
Errno        = Transport endpoint is not connected

Trying to connect to server.
Close Socket
Close Socket File Descriptor
Socket cannot shutdown!
Return Value = -1
Errno        = Transport endpoint is not connected

1 个答案:

答案 0 :(得分:1)

从套接字返回0个读取字节是完全合法的。这只意味着你必须等待再试一次。

例如,请查看此read_data() function。它检查读取的字节数是否为> 0,然后存储它们。它检查读取的字节数是否为< 0,然后是错误。

当然,0案并不是真正的成功,但并非失败。再试一次。我没有详细分析您的代码,因此可能还有其他内容,但这是一个经常发生的重要错误。 (另外,那些没有显示的课程是什么?)

你可以看到的是非阻塞套接字。另请参阅FAQ for how to tell if a socket is closed on the other end.并考虑detecting transfer completion explicitly by sending and end-of-file or marker