Sponsored Content
Top Forums Programming socket function to read a webpage (socket.h) Post 302409342 by cyler on Wednesday 31st of March 2010 07:33:56 PM
Old 03-31-2010
socket function to read a webpage (socket.h)

Why does this socket function only read the first 1440 chars of the stream. Why not the whole stream ? I checked it with gdm and valgrind and everything seems correct...



Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/time.h>

int main(void)
{
    // set family and socket type
    struct addrinfo hints, *result;
    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;

    // resolve hostname to IP address: specify host and port number (in char not int)
    if(getaddrinfo("www.unix.com", "80", &hints, &result) != 0)
    {
        freeaddrinfo(result);
        puts("Could not resolve hostname.");
        exit(1);
    }

    // create socket and free addrinfo memory
    int newsocket = socket(result->ai_family, result->ai_socktype, 0);
    if(newsocket == -1)
    {
        puts("Could not create socket.");
        freeaddrinfo(result); // free addrinfo memory
        close(newsocket);
        exit(1);
    }

    // set socket timeouts
	struct timeval timeout;
	memset(&timeout, 0, sizeof(timeout)); // zero timeout struct before use
	timeout.tv_sec = 5;
	timeout.tv_usec = 0;
	setsockopt(newsocket, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); // send timeout
	setsockopt(newsocket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); // receive timeout

    // connect to website
    if(connect(newsocket, result->ai_addr, result->ai_addrlen) == -1)
    {
        puts("Could not connect.");
        freeaddrinfo(result); // free addrinfo memory
        close(newsocket);
        exit(1);
    }

    // send headers to connection
    char headers[256] = "GET /index HTTP/1.1\r\nHost: www.unix.com\r\nUser-Agent: Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0)\r\nReferer: \r\nConnection: Close\r\n\r\n";
    if( (send(newsocket, headers, strlen(headers), 0)) == -1 )
    {
        puts("Could not send data.");
        freeaddrinfo(result); // free addrinfo memory
        close(newsocket);
        exit(1);
    }

    // receive webpage from connection
    char response[51201] = "";
    ssize_t length = recv(newsocket, response, 51200, 0);
    response[strlen(response)] = '\0'; // terminate the string
    if(length == -1)
    {
        puts("Could not receive data.");
        freeaddrinfo(result); // free addrinfo memory
        close(newsocket);
        exit(1);
    }

    // free addrinfo memory
    freeaddrinfo(result);

    // close socket
    close(newsocket);

printf("%s\n%i\n", response, length);

    return 0;
}


Last edited by cyler; 04-03-2010 at 08:05 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

send function in socket

Hi All, I encountered a stange problem while doing a perl script to use socket. i need to transfer a file from client to sever. but error came as argument missing in send function.........Plz tell me the wt r the arguments in send and recv functions....... (0 Replies)
Discussion started by: trupti_rinku
0 Replies

2. Programming

Anyone know how to use socket select() function?

hello socket programming expert, I having difficulties in understanding how select() function in socket programming work.... I'm trying to create my own peer-to-peer chat or file transfer program by using the select() function.... Therefore does anyone had any tutorial or source code that... (4 Replies)
Discussion started by: draggy
4 Replies

3. Programming

connect() function in C++ socket programming

Hello All, I have a problem using connect(...) function in C++. I am using SSH from my windows system to connect it to linux server. The program works fine if I run it directly in Linux machine but I need it to run through windows machine. The function returns -1 and so my program terminates. ... (3 Replies)
Discussion started by: smdhd3
3 Replies

4. Programming

Please help! accept function problems in Socket programming

Hi, I have a client-server socket program. It has been working fine for over a year, but recently it started to show strange behavior.:confused: After the server program runs for a while, it will show in the top command saying it is using lots of CPU, MEM. I assume it means the server code is... (1 Reply)
Discussion started by: natxie
1 Replies

5. UNIX for Advanced & Expert Users

connect problem for sctp socket (ipv6 socket) - Runtime fail Invalid Arguments

Hi, I was porting ipv4 application to ipv6; i was done with TCP transports. Now i am facing problem with SCTp transport at runtime. To test SCTP transport I am using following server and client socket programs. Server program runs fine, but client program fails giving Invalid Arguments for... (0 Replies)
Discussion started by: chandrutiptur
0 Replies

6. Programming

which socket should socket option on be set

Hi all, On the server side, one socket is used for listening, the others are used for communicating with the client. My question is: if i want to set option for socket, which socket should be set on? If either can be set, what's the different? Again, what's the different if set option... (1 Reply)
Discussion started by: blademan100
1 Replies

7. Programming

Error with socket operation on non-socket

Dear Experts, i am compiling my code in suse 4.1 which is compiling fine, but at runtime it is showing me for socket programming error no 88 as i searched in errno.h it is telling me socket operation on non socket, what is the meaning of this , how to deal with this error , please... (1 Reply)
Discussion started by: vin_pll
1 Replies

8. IP Networking

Clarification - Setting socket options at the same time when socket is listening

I need clarification on whether it is okay to set socket options on a listening socket simultaneously when it is being used in an accept() call? Following is the scenario:- -- Task 1 - is executing in a loop - polling a listen socket, lets call it 'fd', (whose file descriptor is global)... (2 Replies)
Discussion started by: jake24
2 Replies

9. Shell Programming and Scripting

Read and write to tcp socket

Hello all, I have a requirement to read and write to a tcp socket from an HP-UX shell script. I see a /dev/tcp character device on my servers: crw-rw-rw- 1 root root 72 0x00004f Mar 28 18:37 /dev/tcp So I believe this is what I should use. The problem is that all the... (2 Replies)
Discussion started by: lupin..the..3rd
2 Replies

10. Programming

read from socket

hello I have a C program and I want to read data from a socket ,and especially I want a client to send a .jpg to the server.The problem I face is that no matter what the size of the photo is,my read command seems to read an amount of data and then does nothing. while(1){ ... (4 Replies)
Discussion started by: vlm
4 Replies
GETADDRINFO(3)						   BSD Library Functions Manual 					    GETADDRINFO(3)

NAME
getaddrinfo, freeaddrinfo -- socket address structure to host and service name SYNOPSIS
#include <sys/types.h> #include <sys/socket.h> #include <netdb.h> int getaddrinfo(const char *hostname, const char *servname, const struct addrinfo *hints, struct addrinfo **res); void freeaddrinfo(struct addrinfo *ai); DESCRIPTION
The getaddrinfo() function is used to get a list of IP addresses and port numbers for host hostname and service servname. It is a replace- ment for and provides more flexibility than the gethostbyname(3) and getservbyname(3) functions. The hostname and servname arguments are either pointers to NUL-terminated strings or the null pointer. An acceptable value for hostname is either a valid host name or a numeric host address string consisting of a dotted decimal IPv4 address or an IPv6 address. The servname is either a decimal port number or a service name listed in services(5). At least one of hostname and servname must be non-null. hints is an optional pointer to a struct addrinfo, as defined by <netdb.h>: struct addrinfo { int ai_flags; /* input flags */ int ai_family; /* protocol family for socket */ int ai_socktype; /* socket type */ int ai_protocol; /* protocol for socket */ socklen_t ai_addrlen; /* length of socket-address */ struct sockaddr *ai_addr; /* socket-address for socket */ char *ai_canonname; /* canonical name for service location */ struct addrinfo *ai_next; /* pointer to next in list */ }; This structure can be used to provide hints concerning the type of socket that the caller supports or wishes to use. The caller can supply the following structure elements in hints: ai_family The protocol family that should be used. When ai_family is set to PF_UNSPEC, it means the caller will accept any protocol family supported by the operating system. ai_socktype Denotes the type of socket that is wanted: SOCK_STREAM, SOCK_DGRAM, or SOCK_RAW. When ai_socktype is zero the caller will accept any socket type. ai_protocol Indicates which transport protocol is desired, IPPROTO_UDP or IPPROTO_TCP. If ai_protocol is zero the caller will accept any protocol. ai_flags The ai_flags field to which the hints parameter points shall be set to zero or be the bitwise-inclusive OR of one or more of the values AI_ADDRCONFIG, AI_ALL, AI_CANONNAME, AI_NUMERICHOST, AI_NUMERICSERV, AI_PASSIVE, and AI_V4MAPPED. AI_ADDRCONFIG If the AI_ADDRCONFIG bit is set, IPv4 addresses shall be returned only if an IPv4 address is configured on the local system, and IPv6 addresses shall be returned only if an IPv6 address is configured on the local system. AI_ALL If the AI_ALL bit is set with the AI_V4MAPPED bit, then getaddrinfo() shall return all matching IPv6 and IPv4 addresses. The AI_ALL bit without the AI_V4MAPPED bit is ignored. AI_CANONNAME If the AI_CANONNAME bit is set, a successful call to getaddrinfo() will return a NUL-terminated string con- taining the canonical name of the specified hostname in the ai_canonname element of the first addrinfo struc- ture returned. AI_NUMERICHOST If the AI_NUMERICHOST bit is set, it indicates that hostname should be treated as a numeric string defining an IPv4 or IPv6 address and no name resolution should be attempted. AI_NUMERICSERV If the AI_NUMERICSERV bit is set, then a non-null servname string supplied shall be a numeric port string. Otherwise, an EAI_NONAME error shall be returned. This bit shall prevent any type of name resolution service (for example, NIS+) from being invoked. AI_PASSIVE If the AI_PASSIVE bit is set it indicates that the returned socket address structure is intended for use in a call to bind(2). In this case, if the hostname argument is the null pointer, then the IP address portion of the socket address structure will be set to INADDR_ANY for an IPv4 address or IN6ADDR_ANY_INIT for an IPv6 address. If the AI_PASSIVE bit is not set, the returned socket address structure will be ready for use in a call to connect(2) for a connection-oriented protocol or connect(2), sendto(2), or sendmsg(2) if a connectionless pro- tocol was chosen. The IP address portion of the socket address structure will be set to the loopback address if hostname is the null pointer and AI_PASSIVE is not set. AI_V4MAPPED If the AI_V4MAPPED flag is specified along with an ai_family of AF_INET6, then getaddrinfo() shall return IPv4-mapped IPv6 addresses on finding no matching IPv6 addresses ( ai_addrlen shall be 16). The AI_V4MAPPED flag shall be ignored unless ai_family equals AF_INET6. All other elements of the addrinfo structure passed via hints must be zero or the null pointer. If hints is the null pointer, getaddrinfo() behaves as if the caller provided a struct addrinfo with ai_family set to PF_UNSPEC and all other elements set to zero or NULL. After a successful call to getaddrinfo(), *res is a pointer to a linked list of one or more addrinfo structures. The list can be traversed by following the ai_next pointer in each addrinfo structure until a null pointer is encountered. The three members ai_family, ai_socktype, and ai_protocol in each returned addrinfo structure are suitable for a call to socket(2). For each addrinfo structure in the list, the ai_addr member points to a filled-in socket address structure of length ai_addrlen. This implementation of getaddrinfo() allows numeric IPv6 address notation with scope identifier, as documented in chapter 11 of draft-ietf- ipv6-scoping-arch-02.txt. By appending the percent character and scope identifier to addresses, one can fill the sin6_scope_id field for addresses. This would make management of scoped addresses easier and allows cut-and-paste input of scoped addresses. At this moment the code supports only link-local addresses with the format. The scope identifier is hardcoded to the name of the hardware interface associated with the link (such as ne0). An example is ``fe80::1%ne0'', which means ``fe80::1 on the link associated with the ne0 interface''. The current implementation assumes a one-to-one relationship between the interface and link, which is not necessarily true from the specifi- cation. All of the information returned by getaddrinfo() is dynamically allocated: the addrinfo structures themselves as well as the socket address structures and the canonical host name strings included in the addrinfo structures. Memory allocated for the dynamically allocated structures created by a successful call to getaddrinfo() is released by the freeaddrinfo() function. The ai pointer should be a addrinfo structure created by a call to getaddrinfo(). RETURN VALUES
getaddrinfo() returns zero on success or one of the error codes listed in gai_strerror(3) if an error occurs. EXAMPLES
The following code tries to connect to ``www.kame.net'' service ``http'' via a stream socket. It loops through all the addresses available, regardless of address family. If the destination resolves to an IPv4 address, it will use an AF_INET socket. Similarly, if it resolves to IPv6, an AF_INET6 socket is used. Observe that there is no hardcoded reference to a particular address family. The code works even if getaddrinfo() returns addresses that are not IPv4/v6. struct addrinfo hints, *res, *res0; int error; int s; const char *cause = NULL; memset(&hints, 0, sizeof(hints)); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_STREAM; error = getaddrinfo("www.kame.net", "http", &hints, &res0); if (error) { errx(1, "%s", gai_strerror(error)); /*NOTREACHED*/ } s = -1; for (res = res0; res; res = res->ai_next) { s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if (s < 0) { cause = "socket"; continue; } if (connect(s, res->ai_addr, res->ai_addrlen) < 0) { cause = "connect"; close(s); s = -1; continue; } break; /* okay we got one */ } if (s < 0) { err(1, "%s", cause); /*NOTREACHED*/ } freeaddrinfo(res0); The following example tries to open a wildcard listening socket onto service ``http'', for all the address families available. struct addrinfo hints, *res, *res0; int error; int s[MAXSOCK]; int nsock; const char *cause = NULL; memset(&hints, 0, sizeof(hints)); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; error = getaddrinfo(NULL, "http", &hints, &res0); if (error) { errx(1, "%s", gai_strerror(error)); /*NOTREACHED*/ } nsock = 0; for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) { s[nsock] = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if (s[nsock] < 0) { cause = "socket"; continue; } if (bind(s[nsock], res->ai_addr, res->ai_addrlen) < 0) { cause = "bind"; close(s[nsock]); continue; } (void) listen(s[nsock], 5); nsock++; } if (nsock == 0) { err(1, "%s", cause); /*NOTREACHED*/ } freeaddrinfo(res0); SEE ALSO
bind(2), connect(2), send(2), socket(2), gai_strerror(3), gethostbyname(3), getnameinfo(3), getservbyname(3), resolver(3), hosts(5), resolv.conf(5), services(5), hostname(7), named(8) R. Gilligan, S. Thomson, J. Bound, J. McCann, and W. Stevens, Basic Socket Interface Extensions for IPv6, RFC 3493, February 2003. S. Deering, B. Haberman, T. Jinmei, E. Nordmark, and B. Zill, IPv6 Scoped Address Architecture, internet draft, draft-ietf-ipv6-scoping- arch-02.txt, work in progress material. Craig Metz, "Protocol Independence Using the Sockets API", Proceedings of the freenix track: 2000 USENIX annual technical conference, June 2000. STANDARDS
The getaddrinfo() function is defined by the IEEE Std 1003.1-2004 (``POSIX.1'') specification and documented in RFC 3493, ``Basic Socket Interface Extensions for IPv6''. BSD
July 1, 2008 BSD
All times are GMT -4. The time now is 05:19 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy