网络程序在Linux中运行,而不是在Cygwin下运行

时间:2011-06-09 12:42:22

标签: c networking cygwin

我编写了一个简单的程序,它从stdin读取输入,并通过TCP将其发送到侦听端口15557的服务器。

当我在Linux下编译并运行它时它工作正常。但是,当我尝试在Cygwin下编译并运行它时,它失败并显示以下错误消息:

$ ./a.out servername.net 15557 < test.dat
Unable to connect: Cannot assign requested address

我排除了任何防火墙/网络问题,因为我能够通过telnet连接到同一台服务器并发送手动输入的相同数据。

知道这里出了什么问题吗?

更新

按照@Hasturkun的线索,我已经在GDB下启动了该程序来检查gethostbyname的结果是什么。就在gethostbyname电话之后:

(gdb) print *serverent
$2 = {h_name = 0x603217 "bilbo.neurobat.net", h_aliases = 0x603030, h_addrtype = 2, h_length = 4, h_addr_list = 0x6031c0}
(gdb) print serverent->h_addr_list[0]
$3 = 0x60322c ">\002V0"
(gdb) print atoi(">\002V0")
$5 = 0

我不明白如何解释“&gt; \ 002V0”字符串。这应该是一个互联网地址吗?

/ UPDATE

FWIW,整个计划如下所示:

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>

#include "neurocli.h"

int main(int argc, char* argv[]) {
  char *line = NULL;
  char buf[40] = {0};
  ssize_t write_len = 0, read_len;
  size_t n = 0;
  int neuro_socket;

  if (argc != 3) {
    fprintf(stderr, "Usage: %s host port\n", argv[0]);
    return -1;
  }

  neuro_socket = open_tcp_socket(argv[1], atoi(argv[2]));

  /* Main loop: connect, write, read, re-connect and finally close */
  while ((write_len=getline(&line, &n, stdin)) != -1) {
    printf("# %s", line);
    if (write(neuro_socket, line, write_len) < 0) {
      perror("Unable to write to server");
      exit(EXIT_FAILURE);
    }
    if (*line=='\n') {
      read_len = read(neuro_socket, buf, 40);
      buf[read_len] = '\0';
      printf("%s", buf);
      close(neuro_socket);
      neuro_socket = open_tcp_socket(argv[1], atoi(argv[2]));
    }
    free(line);
    line = NULL; /* getline() needs this */
  }
  free(line);
  close(neuro_socket);
  return 0;
}


int open_tcp_socket(char *server, int port) {
  int result;
  static struct sockaddr_in *sockaddr;

  /* Create socket */
  if ((result = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("Unable to create socket");
    exit(EXIT_FAILURE);
  }

  if (sockaddr==NULL)
    sockaddr = make_sockaddr(server, port); /* never mind the memory leak */

  /* Connect */
  if (connect(result, (struct sockaddr*)sockaddr, sizeof(*sockaddr)) != 0) {
    perror("Unable to connect");
    exit(EXIT_FAILURE);
  }

  return result;
}

struct sockaddr_in *make_sockaddr(char *name, int port) {
  struct sockaddr_in *sockaddr = malloc(sizeof(struct sockaddr_in));
  struct hostent *serverent;

  memset(sockaddr, 0, sizeof(*sockaddr));
  sockaddr->sin_family = AF_INET;
  sockaddr->sin_port = htons(port);

  if ((serverent = gethostbyname(name)) == NULL) {
    perror("Unable to lookup server IP address");
    exit(EXIT_FAILURE);
  }

  sockaddr->sin_addr.s_addr = atoi(serverent->h_addr_list[0]);

  return sockaddr;
}

2 个答案:

答案 0 :(得分:1)

这一行

sockaddr->sin_addr.s_addr = atoi(serverent->h_addr_list[0]);

不正确。 h_addr_list包含struct in_addrstruct in6_addr的地址,长度在h_length中提供,类型可以通过h_addrtype确定。

获取地址memcpy或将其分配给相应的类型

答案 1 :(得分:0)

connect()调用失败,因为地址无效。很可能对gethostbyname()的调用返回一个没有有用地址的结构(即h_addr_list [0] == 0)。

不确定cygwin如何与DNS客户端进行交互...