gpt4 book ai didi

c++ - TCP连接失败

转载 作者:行者123 更新时间:2023-11-28 08:26:32 27 4
gpt4 key购买 nike

我在使用带有 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

最佳答案

从套接字返回 0 个读取字节是完全合法的。这仅意味着您必须等待并重试。

例如看这个 read_data() function .它检查读取的字节数是否为 > 0 ,然后存储它们。它检查读取的字节数是否为 < 0 , 则报错。

0的案例当然算不上真正的大成功,但也算不上失败。再试一次。我没有详 segmentation 析你的代码,所以可能还有其他问题,但这是一个经常犯的重要错误。 (此外,那些未显示的类中有什么?)

您可以研究的是非阻塞套接字。另见 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 .

关于c++ - TCP连接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3907309/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com