C net programming

This is a discussion on C net programming within the C Programming forums, part of the General Programming Boards category; Please, help me. I' ve a problem with istream... If I try to connect to a ftp or smtp server, ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    1

    C net programming

    Please, help me. I' ve a problem with istream... If I try to connect to a ftp or smtp server, It's ok...

    When I connect to http server I get:

    the connection freeze or the web server send "Errror 400, bad request" ... Can help me?


    the code::


    /* cc -o client client.c -lsocket -lnsl on solaris
    * or gcc -o client client.c on linux
    * usage: client <server> <port>
    */

    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <arpa/inet.h>

    #define MSGLEN 100000000
    #define MSGLENOUT 100000000

    void
    E (char *msg){perror (msg);exit (-1);}


    void main (int narg, char **args)
    {

    struct hostent *_hostent;
    int _socket;
    int _domain = PF_INET;
    int _type = SOCK_STREAM;
    int _protocol = 0;
    int _flags = 0;
    int _n;

    char _msg[MSGLEN] =
    "GET /default.htm HTTP/1.1 \n Referer: http://www.pippo.com \n Connection: Keep-Alive \n User-Agent: Mozilla/4.61 (Machintosh; I; PPC) \n";

    char _buf[MSGLEN];
    struct sockaddr_in _addr;
    int _port = atoi (args[2]);
    _hostent = gethostbyname (args[1]);
    if (_hostent == NULL) E ("ERROR, no host");
    printf ("connectiong...\n");
    bcopy (_hostent->h_addr_list[0], &_addr.sin_addr,_hostent->h_length);

    _addr.sin_family = AF_INET;
    _addr.sin_port = htons (_port);
    _socket = socket (_domain, _type, _protocol);
    printf ("socket created\n");
    if (connect(_socket, (struct sockaddr *) &_addr, sizeof (_addr)) < 0) E ("ERRORE nella connessione");
    printf (" connected!\n");
    _n = send (_socket, _msg, MSGLEN, _flags);
    if (_n < 0) E ("ERROR, send message \n");
    printf ("send: %s\n", _msg);
    _n = recv (_socket, _buf, MSGLEN, _flags);
    if (_n < 0) E ("ERROR, receive message \n");
    printf ("recv: %s\n", _buf);
    close (_socket);

    printf ("close\n");

    }

  2. #2
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    Code:
    /* cc -o client client.c -lsocket -lnsl on solaris
    * or gcc -o client client.c on linux
    * usage: client <server> <port>
    */ 
    
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <arpa/inet.h>
    
    #define MSGLEN 100000000
    #define MSGLENOUT 100000000
    
    void
    E (char *msg){perror (msg);exit (-1);}
    
    
    void main (int narg, char **args) 
    {
    
    struct hostent *_hostent; 
    int _socket; 
    int _domain = PF_INET;
    int _type = SOCK_STREAM;
    int _protocol = 0;
    int _flags = 0;
    int _n;
    
    char _msg[MSGLEN] =
    "GET /default.htm HTTP/1.1 \n Referer: http://www.pippo.com \n Connection: Keep-Alive \n User-Agent: Mozilla/4.61 (Machintosh; I; PPC) \n";
    
    char _buf[MSGLEN];
    struct sockaddr_in _addr; 
    int _port = atoi (args[2]);
    _hostent = gethostbyname (args[1]);
    if (_hostent == NULL) E ("ERROR, no host");
    printf ("connectiong...\n");
    bcopy (_hostent->h_addr_list[0], &_addr.sin_addr,_hostent->h_length);
    
    _addr.sin_family = AF_INET;
    _addr.sin_port = htons (_port);
    _socket = socket (_domain, _type, _protocol);
    printf ("socket created\n");
    if (connect(_socket, (struct sockaddr *) &_addr, sizeof (_addr)) < 0) E ("ERRORE nella connessione");
    printf (" connected!\n");
    _n = send (_socket, _msg, MSGLEN, _flags);
    if (_n < 0) E ("ERROR, send message \n");
    printf ("send: %s\n", _msg);
    _n = recv (_socket, _buf, MSGLEN, _flags);
    if (_n < 0) E ("ERROR, receive message \n");
    printf ("recv: %s\n", _buf);
    close (_socket);
    
    printf ("close\n");
    
    }
    Now that I can look at it

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Well, from a quick glance I notice that you are invoking undefined behavior and trampling in the implementation's namespaces and not checking your code for icky problems like what if the user didn't enter any command line arguments. This probably doesn't have anything to do with your problem, but you should be aware of it anyway.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    >#define MSGLEN 100000000
    >#define MSGLENOUT 100000000
    Why do you need that much of memory?

    >_n = send (_socket, _msg, MSGLEN, _flags);
    Try changing MSGLEN with strlen(_msg)
    Last edited by shaik786; 09-04-2002 at 12:26 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. time measure (net and gross) with pthreads under linux
    By mynickmynick in forum Linux Programming
    Replies: 12
    Last Post: 12-01-2008, 06:39 AM
  2. Net cpu usage of pthreads?!
    By mynickmynick in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2008, 07:59 AM
  3. sending info through ports over the net for games
    By coconutkid in forum Networking/Device Communication
    Replies: 20
    Last Post: 11-21-2004, 08:07 AM
  4. Net Send
    By RoD in forum Tech Board
    Replies: 45
    Last Post: 06-08-2003, 09:49 PM
  5. Net Framework Installation
    By Sorensen in forum C# Programming
    Replies: 2
    Last Post: 01-15-2002, 10:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21