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)						     Linux Programmer's Manual						    getaddrinfo(3)

NAME
getaddrinfo - network address and service translation SYNOPSIS
#include <sys/types.h> #include <sys/socket.h> #include <netdb.h> int getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res); void freeaddrinfo(struct addrinfo *res); const char *gai_strerror(int errcode); DESCRIPTION
The getaddrinfo(3) function combines the functionality provided by the getipnodebyname(3), getipnodebyaddr(3), getservbyname(3), and get- servbyport(3) functions into a single interface. The thread-safe getaddrinfo(3) function creates one or more socket address structures that can be used by the bind(2) and connect(2) system calls to create a client or a server socket. The getaddrinfo(3) function is not limited to creating IPv4 socket address structures; IPv6 socket address structures can be created if IPv6 support is available. These socket address structures can be used directly by bind(2) or connect(2), to prepare a client or a server socket. The addrinfo structure used by this function contains the following members: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; size_t ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }; getaddrinfo(3) sets res to point to a dynamically-allocated link list of addrinfo structures, linked by the ai_next member. There are sev- eral reasons why the link list may have more than one addrinfo structure, including: if the network host is multi-homed; or if the same service is available from multiple socket protocols (one SOCK_STREAM address and another SOCK_DGRAM address, for example). The members ai_family, ai_socktype, and ai_protocol have the same meaning as the corresponding parameters in the socket(2) system call. The getaddrinfo(3) function returns socket addresses in either IPv4 or IPv6 address family, (ai_family will be set to either PF_INET or PF_INET6). The hints parameter specifies the preferred socket type, or protocol. A NULL hints specifies that any network address or protocol is acceptable. If this parameter is not NULL it points to an addrinfo structure whose ai_family, ai_socktype, and ai_protocol members specify the preferred socket type. PF_UNSPEC in ai_family specifies any protocol family (either IPv4 or IPv6, for example). 0 in ai_socktype or ai_protocol specifies that any socket type or protocol is acceptable as well. The ai_flags member specifies additional options, defined below. Multiple flags are specified by logically OR-ing them together. All the other members in the hints parameter must contain either 0, or a null pointer. The node or service parameter, but not both, may be NULL. node specifies either a numerical network address (dotted-decimal format for IPv4, hexadecimal format for IPv6) or a network hostname, whose network addresses are looked up and resolved. If the ai_flags member in the hints parameter contains the AI_NUMERICHOST flag then the node parameter must be a numerical network address. The AI_NUMERICHOST flag suppresses any potentially lengthy network host address lookups. The getaddrinfo(3) function creates a link list of addrinfo structures, one for each network address subject to any restrictions imposed by the hints parameter. ai_canonname is set to point to the official name of the host, if ai_flags in hints includes the AI_CANONNAME flag. ai_family, ai_socktype, and ai_protocol specify the socket creation parameters. A pointer to the socket address is placed in the ai_addr member, and the length of the socket address, in bytes, is placed in the ai_addrlen member. If node is NULL, the network address in each socket structure is initialized according to the AI_PASSIVE flag, which is set in the ai_flags member of the hints parameter. The network address in each socket structure will be left unspecified if AI_PASSIVE flag is set. This is used by server applications, which intend to accept client connections on any network address. The network address will be set to the loopback interface address if the AI_PASSIVE flag is not set. This is used by client applications, which intend to connect to a server running on the same network host. service sets the port number in the network address of each socket structure. If service is NULL the port number will be left uninitial- ized. The freeaddrinfo(3) function frees the memory that was allocated for the dynamically allocated link list res. RETURN VALUE
getaddrinfo(3) returns 0 if it succeeds, or one of the following non-zero error codes: EAI_FAMILY The requested address family is not supported at all. EAI_SOCKTYPE The requested socket type is not supported at all. EAI_BADFLAGS ai_flags contains invalid flags. EAI_NONAME The node or service is not known. This error is also returned if both node and service are NULL. EAI_SERVICE The requested service is not available for the requested socket type. It may be available through another socket type. EAI_ADDRFAMILY The specified network host does not have any network addresses in the requested address family. EAI_NODATA The specified network host exists, but does not have any network addresses defined. EAI_MEMORY Out of memory. EAI_FAIL The name server returned a permanent failure indication. EAI_AGAIN The name server returned a temporary failure indication. Try again later. EAI_SYSTEM Other system error, check errno for details. The gai_strerror(3) function translates these error codes to a human readable string, suitable for error reporting. SEE ALSO
getipnodebyname(3), getipnodebyaddr(3) Linux Man Page 2000-12-18 getaddrinfo(3)
All times are GMT -4. The time now is 08:28 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy