Solved

C Programming: sockets - read()

Posted on 2013-10-27
4
383 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
Comment Utility
I think you need to call accept() before calling read()
0
 
LVL 8

Author Comment

by:pzozulka
Comment Utility
This is on the client side. I think the client only does socket() and connect().
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

771 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

11 Experts available now in Live!

Get 1:1 Help Now