【问题标题】:"Connected" UDP sockets, communication in both directions?“已连接” UDP 套接字,双向通信?
【发布时间】:2018-12-23 06:32:39
【问题描述】:

如何在连接的 UDP 套接字上实现 2 路通信?

我可以从客户端向服务器发送消息,但无法从服务器获取消息。这是我的代码。我认为这个问题一定是在服务器端,但我不知道如何解决这个问题。我故意删除了错误检查,只是为了在 SO 上发布并保持我的帖子简短。我没有收到任何错误。

我可以使用 未连接 UDP 套接字运行此程序,但不能使用 已连接 套接字。

Server.c

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <errno.h>

int main()
{
  int sockfd;
  struct sockaddr_in me;
  char buffer[1024];

  sockfd = socket(AF_INET, SOCK_DGRAM, 0);

  memset(&me, '\0', sizeof(me));
  me.sin_family = AF_INET;
  me.sin_port = htons(8080);
  me.sin_addr.s_addr = inet_addr("127.0.0.1");

  bind(sockfd, (struct sockaddr *)&me, sizeof(me));

  recv(sockfd, buffer, 1024, 0);
  printf("[+]Data Received: %s\n", buffer);

  strcpy(buffer, "Hello Client\n");
  send(sockfd, buffer, 1024, 0);
  printf("[+]Data Send: %s\n", buffer);

  return 0;
}

Client.c

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <errno.h>

int main()
{
  int sockfd;
  struct sockaddr_in other;
  char buffer[1024];

  sockfd = socket(AF_INET, SOCK_DGRAM, 0);

  memset(&other, '\0', sizeof(other));
  other.sin_family = AF_INET;
  other.sin_port = htons(8080);
  other.sin_addr.s_addr = inet_addr("127.0.0.1");

  connect(sockfd, (struct sockaddr *)&other, sizeof(other));

  strcpy(buffer, "Hello Server\n");
  send(sockfd, buffer, 1024, 0);
  printf("[+]Data Send: %s\n", buffer);

  recv(sockfd, buffer, 1024, 0);
  printf("[+]Data Received: %s\n", buffer);

  return 0;
}

服务器输出

[+]Data Received: Hello Server
[+]Data Send: Hello Client

客户端输出

[+]Data Send: Hello Server
// Here it does not receive the message sent by server.

【问题讨论】:

  • 您没有检查任何这些系统调用的返回值。当 2 个套接字绑定到同一台机器上同一地址的同一端口时,您认为会发生什么?
  • 正如我所说,我刚刚删除了对这篇文章的错误检查。我已经在检查返回值和输出是否与我发布的相同。
  • 啊,确实,我读到你有 2 个绑定。
  • @AnttiHaapala 我该如何解决这个问题。请指导我。
  • 不,问题又是你删除了错误处理代码,所以我们不知道你是否有同样的错误。

标签: c sockets networking network-programming udp


【解决方案1】:

在 linux 上,straceing 可执行文件,服务器发送确实这样说:

sendto(3, "Hello Client\n\0\0\0\310$\220\4J\177\0\0\0\0\0\0\0\0\0\0"...,
       1024, 0, NULL, 0) = -1 EDESTADDRREQ (Destination address required)

即服务器套接字确实不知道它需要发送到的地址。 任何 UDP 套接字必须通过connecting,sendto 中提供目标套接字地址,使套接字的另一端知道。 UDP 套接字上的connect 意味着只需为send 设置默认地址。


要连接“服务器”端的套接字,与未知方连接,您应该使用recvfrom 找出发送方的套接字地址 - 然后您可以使用此地址connect 或继续使用sendto .使用sendto,同一个套接字可以同时与许多不同的方进行通信。


TCP 服务器/客户端套接字是不同的情况,因为服务器端的listen/accept 返回一个与原始服务器套接字不同的 连接套接字。

【讨论】:

  • 如何在服务器端连接套接字?
  • @BhavinPanara 已更新。基本上,“连接”与您之前所说的工作机制不同。
  • 所以我必须在服务器端的recvfrom() 上接收第一条消息,然后我可以使用我刚刚收到的地址在服务器端调用connect(),然后只有我可以继续使用@987654333 @ & recv() 供双方日后交流。我说的对吗?
  • @BhavinPanara 是的
  • 好的。感谢您提供的所有信息。
最近更新 更多