sendto():尝试使用 UDP 从服务器发送响应时出现传输端点未连接错误

sendto(): Transport endpoint is not connected error while trying to send a response from server using UDP

我正在尝试使用 UDP 执行简单的客户端-服务器程序,其中我将一个字符串形式的客户端发送到服务器并且服务器确认 "got it"。在我尝试发送 ack 之前一切正常。发送到生成 "Transport endpoint is not connected" 错误。 我正在使用 sentto() 以下列格式发送到服务器。

sendto(sid, message, strlen(message), 0, (struct sockaddr *) &saddr, len);

并从喜欢的人那里收到

recvfrom(sid, message, 50, 0, (struct sockaddr*) &caddr, &len);

我正在使用 AF_UNIX 插座。当我将它移植到 AF_INET.

时,该程序运行良好

您应该使用 recvfrom() 从套接字读取并将接收到的套接字信息传递回 sendto(),如下所示:

struct sockaddr_in client;
socklen_t slen = sizeof(client);

int l  = recvfrom(fd, buf,      sizeof(buf), 0, &client, &slen);
int st = sendto  (fd, "got it", 6,           0, &client,  slen);

这应该会如您所愿