connect()函数耗时太长

时间:2014-09-12 19:52:46

标签: c linux unix networking tcp

当我输入终端时:

echo "GET /" | ./<executable name> www.google.com <port number, usually 80>

终端只是坐在那里等待输入或它陷入无限循环。发生的事情是,我认为联系花了太长时间。

  /*Creating socket*/
  int sock = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP);
  if (sock < 0) {
    printf("error creating socket\n");
    exit(1);
  }
  printf("1\n");
  /*Establish connection to the echo server*/
  int r = connect(sock, addrList->ai_addr, addrList->ai_addrlen);
  printf("1.5\n");
  if (r < 0) {
    perror("Connection failed\n");
    exit(1);
  }
  printf("2\n");

在这里,1打印出来,但连接后的1.5没有打印出来,终端只是坐着。

这个问题以前没有发生过,我过去常常把页面的源代码拿回来。但现在这个问题正在发生。

在我输入终端后开始发生:netstat -an -A inet | grep :2525

所以这可能会产生影响。

以下是整个代码:

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

int main(int argc, char *argv[]) {
  if (argc != 3) {
    printf("Invalid arguments\n");
    exit(1);
  }

  char *serverIP = argv[1]; /*Server hostname*/
  char *portNumber = argv[2]; /*Port Number*/
  void *numericAddress;
  char addrBuffer[INET6_ADDRSTRLEN];
  in_port_t port;
  char buffer_stdin[65535];
  char buffer_stdout[65535];
  int bytes_read = 0;
  int bytes_written = 0;

  /*getting integral number of string representation of port number*/
  in_port_t servPort = atoi(argv[2]);

  /*------------------get binary number of hostname-----------------*/
  struct addrinfo addrCriteria;
  memset(&addrCriteria, 0, sizeof(addrCriteria));
  addrCriteria.ai_family = AF_INET;
  addrCriteria.ai_socktype = SOCK_STREAM;
  addrCriteria.ai_protocol = IPPROTO_TCP;

  struct addrinfo *addrList;

  int rtnVal = getaddrinfo(serverIP, portNumber, &addrCriteria, &addrList);
  if (rtnVal != 0) {
    printf("getaddrinfo() failed\n");
    exit(1);
  }

  numericAddress = &((struct sockaddr_in *) (addrList->ai_addr))->sin_addr;
  /*Converting port to binary*/
  ((struct sockaddr_in *)(addrList->ai_addr))->sin_port = htonl(servPort);
  /*----------------------------------------------------------------*/

  inet_ntop(addrList->ai_addr->sa_family, numericAddress, addrBuffer, sizeof(addrBuffer));
  printf("IP ADDRESS: %s\n", addrBuffer);

  /*Creating socket*/
  int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (sock < 0) {
    printf("error creating socket\n");
    exit(1);
  }
  /*printf("1\n");*/
  /*Establish connection to the echo server*/
  int r = connect(sock, addrList->ai_addr, addrList->ai_addrlen);
  printf("%d\n", r);
  if (r < 0) {
    perror("Connection failed\n");
    exit(1);
  }
  printf("2\n");
  /*Reading from stdin and writing to socket until stdin ends
    bytes_read = read(0, buffer_stdin, sizeof(buffer_stdin));
    write(sock, buffer_stdin, bytes_read);*/

  while ((bytes_read = read(0, buffer_stdin, sizeof(buffer_stdin)-1)) > 0) {
    write(sock, buffer_stdin, bytes_read);
  }

  /*Shutting down write end of socket*/
  int r_shutdown = shutdown(sock, SHUT_WR);
  if (r_shutdown < 0) {
    printf("Shutting down write end of socket failed\n");
    exit(1);
  }

  /*Reading from socket and writing to stdout until socket ends*/
  while ((bytes_read = read(sock, buffer_stdout, sizeof(buffer_stdout)-1)) > 0) {
    write(1, buffer_stdout, bytes_read);
  }

  close(sock);
  exit(0);
}

2 个答案:

答案 0 :(得分:0)

正确的做法是:

struct sockaddr_in address;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if(sockfd!=-1)
  {
          perror("socket :");
          printf("sockfd = %d\n", sockfd);
  }
  else
  {
          perror("socket");
          exit(EXIT_FAILURE);
  }

  address.sin_family = AF_INET;
  address.sin_addr.s_addr = inet_addr("127.0.0.1");
  address.sin_port = htons(9734);
  len = sizeof(struct sockaddr_in);

  result = connect(sockfd, (struct sockaddr *)&address, len);

答案 1 :(得分:0)

Nvm我明白了。

显然我不得不考虑big endian vs little endian,所以在这一行:

((struct sockaddr_in *)(addrList->ai_addr))->sin_port = htonl(servPort);

htonl应该是htons,所以:

((struct sockaddr_in *)(addrList->ai_addr))->sin_port = htons(servPort);