[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 392
  • Last Modified:

C Programming: sockets - read()

In my code below, once it reaches the read() statement it just hangs. According to my log file "fp" (see below), socket(), connect(), and other methods are successful. See log at the bottom.

What can be causing this?

// ----------------GET HTML --------------------

char *strRequest_html;
int myB;

struct hostent *h;
struct in_addr **addr_list;

if((h = gethostbyname(hostName)) == NULL) {
	fprintf(fp, "Couldn't get hostName by name.\n");
	fflush(fp);
}
else {
	fprintf(fp, "gethostbyname() success.\n");
}
addr_list = (struct in_addr **)h->h_addr_list;

const char *ipAddrHTML = inet_ntoa(*addr_list[0]); //gethostbyname
fprintf(fp, "IP addr: %s\n", ipAddrHTML);

socklen_t peer_addr_size_html;

int conStatusHTML;
const char *protocolNameHTML = "tcp";

int sockfd_html;
struct protoent *myProtocolHTML = getprotobyname(protocolNameHTML);
struct sockaddr_in sockIP4addrHTML;
struct sockaddr *peerSockAddrHTML;


size_t count_html = 4000;
ssize_t bytes_read_html;
size_t requestLen_html;

sockIP4addrHTML.sin_family = AF_INET;
sockIP4addrHTML.sin_port = htons(80);
// store IP addr in sockIP4addrHTML structure
inet_pton(AF_INET, ipAddrHTML, &sockIP4addrHTML.sin_addr);

sockfd_html = socket(AF_INET,SOCK_STREAM,myProtocolHTML->p_proto);

if(sockfd_html == -1)
	fprintf(fp, "socket() error: %s\n", strerror(errno));
else {
	fprintf(fp, "socket() success.\n");
}

peer_addr_size_html = sizeof(struct sockaddr_in);
peerSockAddrHTML = (struct sockaddr*)&sockIP4addr;

// Connect
conStatusHTML = connect(sockfd_html, peerSockAddrHTML, peer_addr_size_html);

if(conStatusHTML == -1)
	fprintf(fp, "connect() error: %s\n", strerror(errno));
else {
	fprintf(fp, "connect() success.\n");
}

// create buffer array of size 4000			
char *buff_html = malloc( sizeof(char) * count_html);
fprintf(fp, "malloc() passed.\n");
fflush(fp);

char buf[4192];

char *send="GET %s HTTP/1.1\r\nHOST: %s\r\n\r\n";
sprintf(buf, send, request, hostName);
fprintf(fp, "%s\n", buf);
fprintf(fp, "%d\n", strlen(buf));

	
if((write(sockfd_html, buf, strlen(buf))) < 0)
	fprintf(fp, "write() error: %s\n", strerror(errno));
else {
	fprintf(fp, "write() success.\n");
}
int receiv = 0;
if(( receiv = read(sockfd_html, buf, 4192)) < 0)
	fprintf(fp, "read() error: %s\n", strerror(errno));
else
	buf[receiv]='\0';

fprintf(fp, "%s\n", buf);

Open in new window


LOG FILE
$ cat log.txt
Logging info...
socket() success
bind() success
listen() success
Accepted()
Peer port: 32769
My port: 9000
Peer IP: 127.0.0.1
Logging info while...
Host:
www.csun.edu
Domain:
www.csun.edu
Request:
/
gethostbyname() success.
IP addr: 130.166.238.195
socket() success.
connect() success.
malloc() passed.
GET / HTTP/1.1
HOST: www.csun.edu


38
write() success.
host redirected

write() success: host redirected

Open in new window

0
pzozulka
Asked:
pzozulka
1 Solution
 
chaauCommented:
I think you need to call accept() before calling read()
0
 
pzozulkaAuthor Commented:
This is on the client side. I think the client only does socket() and connect().
0
 
Julian HansenCommented:
Your source does not appear to match the output in the log - is this the full source?
0
 
masheikCommented:
rite() success.
host redirected

write() success: host redirected

Open in new window


try it with,
if(( receiv = read(sockfd_html, buf, 4000)) < 0)
	fprintf(fp, "read() error: %s\n", strerror(errno));
else
	buf[receiv]='\0';

fprintf(fp, "%s\n", buf);

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now