Solved

C Programming: sockets - read()

Posted on 2013-10-27
4
385 Views
Last Modified: 2013-10-28
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
Comment
Question by:pzozulka
4 Comments
 
LVL 24

Expert Comment

by:chaau
ID: 39604855
I think you need to call accept() before calling read()
0
 
LVL 8

Author Comment

by:pzozulka
ID: 39604862
This is on the client side. I think the client only does socket() and connect().
0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 39604875
Your source does not appear to match the output in the log - is this the full source?
0
 
LVL 9

Accepted Solution

by:
masheik earned 500 total points
ID: 39605663
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now