UDP服务器客户端C ++:sendto,recvfrom

时间:2018-10-29 18:21:01

标签: c++ server udp client echo

我正在尝试完成一个简单的回显服务器。目的是将消息重复发送给客户端。服务器和客户端都可以编译。服务器绑定到本地主机和端口8080。客户端具有地址,端口和消息。当客户端将程序转到sendto部分时,它将停止并在那里等待。我的目标是将其发送到服务器,然后由服务器将其发送回去。

问题:客户端正在发送消息,而服务器正在正确接收消息,但是服务器无法返回该消息。请帮忙!

服务器端代码:

#include <iostream>
#include <string.h>
#include <fstream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT 8080

using namespace std;

int main() {
  int serSockDes, len, readStatus;
  struct sockaddr_in serAddr, cliAddr;
  char buff[1024] = {0};
  char msg[] = "Hello to you too!!!\n";

  //creating a new server socket
  if((serSockDes = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("socket creation error...\n");
    exit(-1);
  }

  //binding the port to ip and port
  serAddr.sin_family = AF_INET;
  serAddr.sin_port = htons(PORT);
  serAddr.sin_addr.s_addr = INADDR_ANY;

  if((bind(serSockDes, (struct sockaddr*)&serAddr, sizeof(serAddr))) < 0) {
    perror("binding error...\n");
    exit(-1);
  }

  readStatus = recvfrom(serSockDes, buff, 1024, 0, (struct sockaddr*)&cliAddr, (socklen_t*)&len);
  buff[readStatus] = '\0';
  cout<<buff;

  cout<<len;

  sendto(serSockDes, msg, strlen(msg), 0, (struct sockaddr*)&cliAddr, len);

  return 0;
}

客户端代码:

#include <iostream>
#include <fstream>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT 8080

using namespace std;

int main(int argc, char** argv) {
  int cliSockDes, readStatus, len;
  struct sockaddr_in serAddr;
  char msg[] = "Hello!!!\n";
  char buff[1024] = {0};

  //create a socket
  if((cliSockDes = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("socket creation error...\n");
    exit(-1);
  }

  //server socket address
  serAddr.sin_family = AF_INET;
  serAddr.sin_port = htons(PORT);
  serAddr.sin_addr.s_addr = INADDR_ANY;

  sendto(cliSockDes, msg, strlen(msg), 0, (struct sockaddr*)&serAddr, sizeof(serAddr));

  readStatus = recvfrom(cliSockDes, buff, 1024, 0, (struct sockaddr*)&serAddr, (socklen_t*)&len);
  buff[readStatus] = '\0';
  cout<<buff;

  return 0;
}

1 个答案:

答案 0 :(得分:1)

客户端正在尝试将其消息发送到INADDR_ANY,这是错误的。它需要发送到特定的 IP地址。服务器可以使用INADDR_ANY监听其所有本地IP地址,这很好,但是客户端发送到的IP地址必须是服务器监听的IP地址(或者,如果客户端和服务器位于不同的地址)网络段中,客户端必须发送到到达服务器路由器的IP,然后该IP必须将消息转发到服务器正在侦听的IP。

此外,您在两端对recvfrom()sendto()的调用都缺乏适当的错误处理。具体来说,addrlen的{​​{1}}参数在输入时指定recvfrom()缓冲区的最大大小,在输出时返回存储在sockaddr中的对等地址的实际大小。 。但是您没有初始化作为sockaddr传入的len变量,因此addrlen可能会因未处理的错误而失败。

请尝试以下类似操作:

服务器:

recvfrom()

客户:

#include <iostream>
#include <string.h>
#include <fstream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
using namespace std;

#define PORT 8080

int main() {
  int serSockDes, readStatus;
  struct sockaddr_in serAddr, cliAddr;
  socklen_t cliAddrLen;
  char buff[1024] = {0};
  char msg[] = "Hello to you too!!!\n";

  //creating a new server socket
  if ((serSockDes = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("socket creation error...\n");
    exit(-1);
  }

  //binding the port to ip and port
  serAddr.sin_family = AF_INET;
  serAddr.sin_port = htons(PORT);
  serAddr.sin_addr.s_addr = INADDR_ANY;

  if ((bind(serSockDes, (struct sockaddr*)&serAddr, sizeof(serAddr))) < 0) {
    perror("binding error...\n");
    close(serSockDes);
    exit(-1);
  }

  cliAddrLen = sizeof(cliAddr);
  readStatus = recvfrom(serSockDes, buff, 1024, 0, (struct sockaddr*)&cliAddr, &cliAddrLen);
  if (readStatus < 0) { 
    perror("reading error...\n");
    close(serSockDes);
    exit(-1);
  }

  cout.write(buff, readStatus);
  cout << endl;

  if (sendto(serSockDes, msg, strlen(msg), 0, (struct sockaddr*)&cliAddr, cliAddrLen)) < 0) { 
    perror("sending error...\n");
    close(serSockDes);
    exit(-1);
  }

  close(serSockDes);
  return 0;
}