【发布时间】: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