简体   繁体   中英

Recv hangs even though I followed all conventions?

I am trying to create a small program that takes a http requests through stdin and sends it to a server. This is the code I am using:

int portno =        3000;
char *message = buf;
char response[4096];
int byte_count;
fsize = strlen(message);
int sockfd;
/* fill in the parameters */
printf("Request:\n%s\n",message);

/* create the socket */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) error("ERROR opening socket");
int sz = (1024 * 1024);
if (setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sz, sizeof(sz)) == -1) {
    perror("setsockopt");
    exit(1);
}
struct sockaddr_in saddr;
saddr.sin_family = AF_INET;
saddr.sin_port = htons(portno);
saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if (connect(sockfd, &saddr, sizeof(saddr)) == -1) {
    perror("connect");
}
send(sockfd, message, fsize, MSG_NOSIGNAL);
printf("written");
byte_count = recv(sockfd,response,sizeof(response)-1,0); // <-- -1 to leave room for a null terminator
response[byte_count] = 0; // <-- add the null terminator
printf("recv()'d %d bytes of data in buf\n",byte_count);
printf("%s",response);
close(sockfd);

buf is equal to this

GET /alias%2Findex.html HTTP/1.0\r\n
\r\n
\r\n
\r\n

I have done some research through other stack overflow posts and they state that recv usually hangs when the system is waiting for a response. I do not know what could be causing this.

Here is your program only slightly modified. And it works for me. Are you certain whatever server you are running on localhost port 3000 is responding properly? BTW, I had to change the port to 8080 for my system.

#include <netinet/in.h>
#include <netinet/tcp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

char buf[1 << 16] = "GET /file.txt HTTP/1.0\r\n"
                    "\r\n"
                    "\r\n";

int main() {
  int portno = 8080;
  char *message = buf;
  int byte_count;
  int fsize = strlen(message);
  int sockfd;
  /* fill in the parameters */
  printf("Request:\n%s\n", message);

  /* create the socket */
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0)
    perror("ERROR opening socket");
  int sz = (1024 * 1024);
  if (setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sz, sizeof(sz)) == -1) {
    perror("setsockopt");
    exit(1);
  }
  struct sockaddr_in saddr;
  saddr.sin_family = AF_INET;
  saddr.sin_port = htons(portno);
  saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  if (connect(sockfd, (struct sockaddr *)&saddr, sizeof(saddr)) == -1) {
    perror("connect");
  }
  send(sockfd, message, fsize, MSG_NOSIGNAL);
  printf("written");
  while ((byte_count = recv(sockfd, buf, sizeof(buf) - 1, 0)) >
         0) {            // <-- -1 to leave room for a null terminator
    buf[byte_count] = 0; // <-- add the null terminator
    printf("recv()'d %d bytes of data in buf\n", byte_count);
    printf("%s", buf);
  }
  close(sockfd);

  return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM