Solved

C Programming: sockets - read()

Posted on 2013-10-27
4
387 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 25

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 56

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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

749 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