C - UDP客户端未完成SendTo到Echo Server

时间:2018-02-18 00:18:02

标签: c server udp echo

我正在尝试完成一个简单的echo服务器。目标是将消息重复回客户端。服务器和客户端都编译。服务器运行,你只需要给它一个端口来运行。客户端具有地址,端口和消息。当客户端通过程序进入sendto部分时,它会停止并在那里等待。我的目标是将它发送到服务器,然后将服务器发送回去。

我相信服务器可以运行,或者它最少处于接收模式,因为它进入while循环来执行此操作。该部分可以发送回说明它有效。

对于客户端,我尝试直接发送参数,但也通过c-string。我已经尝试了一个硬编码,但没有一个有效。我已经在那里待了好几个小时,所以我决定寻求帮助,因为我不能把别的东西当成新手。

客户端

//argv[1] address, argv[2] port, argv[3] message
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>

int main(int argc, char *argv[]){

int BUF_LEN;

for (BUF_LEN = 0; argv[3][BUF_LEN] != '\0'; BUF_LEN++){
//  printf("BUF_LEN = %i\n", BUF_LEN);
}
    int s, n, port_no, r;
    struct sockaddr_in server_addr;
    char *haddr, *message;
    char buf[BUF_LEN+1];

printf("Variables created\n");

    s = socket(AF_INET, SOCK_DGRAM, 0); // create a socket for UDP

printf("Socket created as s: %i\n", s);

bzero((char *)&server_addr, sizeof(server_addr));   // clear

server_addr.sin_family = AF_INET;   //IPv4 Internet family
server_addr.sin_addr.s_addr = inet_addr(argv[1]);   //server address
server_addr.sin_port = htons(atoi(argv[2]));    // server port number

printf("Server addr complete\n");

//Bind() - Not necessary

printf("Bind skipped\n");

//sendto()
    r = sendto(s, argv[3], strlen(argv[3]), 0, (struct sockaddr *)&server_addr, BUF_LEN);
    printf("Message Sent");

//recvfrom()
    n = recvfrom (s, buf, BUF_LEN, 0, (struct sockaddr *)&server_addr, &BUF_LEN);
    printf("Message Received: %s\n", buf);

close(s);

}

服务器

//Argv[1] : port number
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>

int main(int argc, char *argv[]){

int n; 
int MAXLINE = 512;
int sock_server, sock_client, r, len;
char buf[MAXLINE];
struct sockaddr_in my_addr, client_addr;    

//printf("Variables created\n");

sock_server = socket (PF_INET, SOCK_DGRAM, 0); 

//printf("Socket created\n");

if (sock_server < 0){
    perror("Bind failed");
    exit(1);
}

bzero(&my_addr, sizeof(my_addr));       // clear

my_addr.sin_family = AF_INET;           //Address Family INET
my_addr.sin_port = htons(atoi(argv[1]));    //Server port number
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);    // Accept from anywhere

//printf("Addresses created\n");

r = bind(sock_server, (struct sockaddr*)(&my_addr), sizeof(my_addr));
    if (r < 0) {
        perror("Bind failed");
        exit(1);
    }

printf("Read to receive\n");

while(1) {

//printf("First while loop\n");

len = sizeof(struct sockaddr_in);

    //recvfrom()
    n = recvfrom (sock_client, buf, MAXLINE, 0, (struct sockaddr *)&client_addr, &len);

while (n > 1){
    printf("Second while loop\n");

    printf("Message Received: %s\n", buf);

    //sendto()
    sendto(sock_client, buf, n, 0, (struct sockaddr *)&client_addr, len);
    n = 0;
    }
}

    close(sock_client);     //close the client socket
//printf("Connection sock_client Closed");
}

1 个答案:

答案 0 :(得分:0)

以下是sizeof(server_addr)客户端调用中使用sendto的客户端的固定版本:

//argv[1] address, argv[2] port, argv[3] message
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>

int main(int argc, char *argv[]){

  int BUF_LEN;

  for (BUF_LEN = 0; argv[3][BUF_LEN] != '\0'; BUF_LEN++){
    //  printf("BUF_LEN = %i\n", BUF_LEN);
  }

  int s, n, port_no, r;
  struct sockaddr_in server_addr;
  char *haddr, *message;
  char buf[BUF_LEN+1];

  printf("Variables created\n");

  s = socket(AF_INET, SOCK_DGRAM, 0); // create a socket for UDP

  printf("Socket created as s: %i\n", s);

  bzero((char *)&server_addr, sizeof(server_addr));   // clear

  server_addr.sin_family = AF_INET;   //IPv4 Internet family
  server_addr.sin_addr.s_addr = inet_addr(argv[1]);   //server address
  server_addr.sin_port = htons(atoi(argv[2]));    // server port number

  printf("Server addr complete\n");

  //Bind() - Not necessary

  printf("Bind skipped\n");

  r = sendto(s, argv[3], strlen(argv[3]), 0, (struct sockaddr *)&server_addr, sizeof(server_addr));
  printf("Message Sent");

  n = recvfrom (s, buf, BUF_LEN, 0, (struct sockaddr *)&server_addr, &BUF_LEN);
  printf("Message Received: %s\n", buf);

  close(s);

}