-
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");
}
-
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
-
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
-
>#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)