Socket Help

K

KL

Hey...I am working on a project for school. I preface this so everyone
understands that I don't want the full answer, rather a nudge in the
correct direction.

The following code section is supposed parse in a url from the command
line. For some reason, I am not parsing the command line properly. Can
someone help nudge me towards the correct way to do this?

int main(int argc, char *argv[]) {

int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
cout << "cannot create socket" << endl;
exit(1);
}

for (int a=0; a<argc; ++a){
string url, hostname, filename;
// get rid of the initial "http://" characters
url=argv[a];
url.erase(0,7);
hostname = url;
filename = url;
int loc = hostname.find("/",0);
if (loc != string::npos) {
hostname.erase(loc);
filename.erase(0,loc);
}
else {
filename = "/";
}
--

KL

______________________________00
_______________________0000000000
______________________00___00___00
__________________________00____00
_________________________00_____00
________________________00______00
________________000____00_______00
___________________00000000000000000
_____________________00_________00_000
____________________00__________00
____________________00__________00
___________________00___________00
_______00________00_____________00
_________00______00______________00___
___________000000_________________00000

********ROLL TIDE*********
 
M

mlimber

KL said:
Hey...I am working on a project for school. I preface this so everyone
understands that I don't want the full answer, rather a nudge in the
correct direction.

The following code section is supposed parse in a url from the command
line. For some reason, I am not parsing the command line properly. Can
someone help nudge me towards the correct way to do this?

int main(int argc, char *argv[]) {

int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
cout << "cannot create socket" << endl;
exit(1);
}

for (int a=0; a<argc; ++a){
string url, hostname, filename;
// get rid of the initial "http://" characters
url=argv[a];
url.erase(0,7);
[snip]

Your code is incomplete (see
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8). What's
the for-loop for? Note that argv[0] is the first argument, aka the name
of the program, not the first parameter to it.

Please shorten your signature.

Cheers! --M
 
K

KL

KL said:
Hey...I am working on a project for school. I preface this so everyone
understands that I don't want the full answer, rather a nudge in the
correct direction.

The following code section is supposed parse in a url from the command
line. For some reason, I am not parsing the command line properly. Can
someone help nudge me towards the correct way to do this?

int main(int argc, char *argv[]) {

int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
cout << "cannot create socket" << endl;
exit(1);
}

for (int a=0; a<argc; ++a){
string url, hostname, filename;
// get rid of the initial "http://" characters
url=argv[a];
url.erase(0,7);

[snip]

Your code is incomplete (see
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8). What's
the for-loop for? Note that argv[0] is the first argument, aka the name
of the program, not the first parameter to it.

Please shorten your signature.

Cheers! --M
Sorry, wasn't sure how much of the code to include. Here is the whole
thing, and a shorter signature.

#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string>

using namespace std;

int main(int argc, char *argv[]) {

int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
cout << "cannot create socket" << endl;
exit(1);
}

for (int a=0; a<argc; ++a){
string url, hostname, filename;
// get rid of the initial "http://" characters
url=argv[a];
url.erase(0,7);
hostname = url;
filename = url;
int loc = hostname.find("/",0);
if (loc != string::npos) {
hostname.erase(loc);
filename.erase(0,loc);
}
else {
filename = "/";
}
cout << "\tparsed input into hostname '" << hostname
<< "' and filename '" << filename << "'" << endl;

struct sockaddr_in server;
struct hostent *host = gethostbyname(hostname.c_str());
if (host == NULL) {
cout << "gethostbyname failed" << endl;
exit(1);
}
server.sin_family = host->h_addrtype;
server.sin_addr.s_addr = ( (struct in_addr *) (host->h_addr) )->s_addr;
// we know the web is at port 80
server.sin_port = htons(80);

if (connect(sock, (struct sockaddr *) &server, sizeof(server)) < 0) {
cout << "connect failed" << endl;
exit(1);
}

// Carry out the actual conversation with the web server

char response[256];
string request;

// First - send a GET request that requests the specific file from the
server
request = "GET " + filename + " HTTP/1.0\015\012\015\012";
send(sock, request.c_str(), request.length() , 0);

// Second - print out HTML code transmitted from the server (a real
browser formats it)
while (recv(sock,response,255,0) > 0) {
cout << response;
for (int a=0; a<256; ++a) response[a] = '\0';
}
}
// Protocol finished, close the socket and exit
close(sock);
}
 
D

Default User

KL said:
KL said:
Hey...I am working on a project for school. I preface this so
everyone understands that I don't want the full answer, rather a
nudge in the correct direction.

The following code section is supposed parse in a url from the
command line. For some reason, I am not parsing the command line
properly. Can someone help nudge me towards the correct way to
do this?

int main(int argc, char *argv[]) {

int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
cout << "cannot create socket" << endl;
exit(1);
}

for (int a=0; a<argc; ++a){
string url, hostname, filename;
// get rid of the initial "http://" characters
url=argv[a];
url.erase(0,7);

[snip]

Your code is incomplete (see
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8).
What's the for-loop for? Note that argv[0] is the first argument,
aka the name of the program, not the first parameter to it.

Please shorten your signature.

Cheers! --M
Sorry, wasn't sure how much of the code to include. Here is the
whole thing, and a shorter signature.

#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string>

using namespace std;

int main(int argc, char *argv[]) {

int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
cout << "cannot create socket" << endl;
exit(1);
}

for (int a=0; a<argc; ++a){

As mentioned elsewhere, it's likely incorrect to start with argv[0].
string url, hostname, filename;
// get rid of the initial "http://" characters
url=argv[a];
url.erase(0,7);
hostname = url;
filename = url;

This has two problems. One is that you don't verify that the argument
indeed starts with that string. Secondly, it's somewhat inefficient.
While efficiency is always implementation-specific, and shouldn't drive
the program, in this case it's algorithmically inefficient as well.

I'd do something like this:

size_t offset = 0;

if (strncmp(argv[a], "http://", 7) == 0)
{
offset = 7;
}

url = argv[a]+offset;


That saves allocating space for those characters that you just erase a
moment later.



Brian
 
K

KL

KL wrote:

KL wrote:


Hey...I am working on a project for school. I preface this so
everyone understands that I don't want the full answer, rather a
nudge in the correct direction.

The following code section is supposed parse in a url from the
command line. For some reason, I am not parsing the command line
properly. Can someone help nudge me towards the correct way to
do this?

int main(int argc, char *argv[]) {

int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
cout << "cannot create socket" << endl;
exit(1);
}

for (int a=0; a<argc; ++a){
string url, hostname, filename;
// get rid of the initial "http://" characters
url=argv[a];
url.erase(0,7);

[snip]

Your code is incomplete (see
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8).
What's the for-loop for? Note that argv[0] is the first argument,
aka the name of the program, not the first parameter to it.

Please shorten your signature.

Cheers! --M

Sorry, wasn't sure how much of the code to include. Here is the
whole thing, and a shorter signature.

#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string>

using namespace std;

int main(int argc, char *argv[]) {

int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
cout << "cannot create socket" << endl;
exit(1);
}

for (int a=0; a<argc; ++a){


As mentioned elsewhere, it's likely incorrect to start with argv[0].

string url, hostname, filename;
// get rid of the initial "http://" characters
url=argv[a];
url.erase(0,7);
hostname = url;
filename = url;


This has two problems. One is that you don't verify that the argument
indeed starts with that string. Secondly, it's somewhat inefficient.
While efficiency is always implementation-specific, and shouldn't drive
the program, in this case it's algorithmically inefficient as well.

I'd do something like this:

size_t offset = 0;

if (strncmp(argv[a], "http://", 7) == 0)
{
offset = 7;
}

url = argv[a]+offset;


That saves allocating space for those characters that you just erase a
moment later.



Brian


In this assignment, we know that the user will include the http:// so
that isn't an issue. Apparently, I am not parsing the command-line
properly. That is where I am a bit lost.
 
D

Default User

KL said:
on 3/3/2006 5:47 PM Default User said the following:
This has two problems. One is that you don't verify that the
argument indeed starts with that string. Secondly, it's somewhat
inefficient. While efficiency is always implementation-specific,
and shouldn't drive the program, in this case it's algorithmically
inefficient as well.

I'd do something like this:

size_t offset = 0;

if (strncmp(argv[a], "http://", 7) == 0)
{
offset = 7;
}

url = argv[a]+offset;


That saves allocating space for those characters that you just
erase a moment later.
In this assignment, we know that the user will include the http:// so
that isn't an issue.

How do you know that?
Apparently, I am not parsing the command-line
properly. That is where I am a bit lost.

Fix the issues already pointed out. Run it again. Give us your input
data, your results, and how those results differed from your
expectations.


Brian
 
K

KL

KL wrote:

on 3/3/2006 5:47 PM Default User said the following:
This has two problems. One is that you don't verify that the
argument indeed starts with that string. Secondly, it's somewhat
inefficient. While efficiency is always implementation-specific,
and shouldn't drive the program, in this case it's algorithmically
inefficient as well.

I'd do something like this:

size_t offset = 0;

if (strncmp(argv[a], "http://", 7) == 0)
{
offset = 7;
}

url = argv[a]+offset;


That saves allocating space for those characters that you just
erase a moment later.

In this assignment, we know that the user will include the http:// so
that isn't an issue.


How do you know that?

Apparently, I am not parsing the command-line
properly. That is where I am a bit lost.


Fix the issues already pointed out. Run it again. Give us your input
data, your results, and how those results differed from your
expectations.


Brian
Thanks for the replies Brian.

I do know that the professor already told us he would be using the
standard http:// in the command-line. So that is a non issue, THANK
GOD! I was able to fix the earlier problem, math helps sometimes LOL

Now it will compile and runs, but it runs into a segmentation fault
(core dump) issue. But I am just now thinking that it might have to do
with haveing a for loop. I mean, why have it if there will only be one
site on the command-line? I better double check if we need to contend
with more than one input on the command-line.

Let me check/try these two things and get back to you all.

Thanks again,

KL
 
D

Default User

KL said:
on 3/3/2006 6:30 PM Default User said the following:
Thanks for the replies Brian.
Sure.

I do know that the professor already told us he would be using the
standard http:// in the command-line. So that is a non issue, THANK
GOD! I was able to fix the earlier problem, math helps sometimes LOL

Ok. I might suggest putting the test in anyway, just to show how cool
you are, but probably not necessary for a school problem.
Now it will compile and runs, but it runs into a segmentation fault
(core dump) issue. But I am just now thinking that it might have to
do with haveing a for loop. I mean, why have it if there will only
be one site on the command-line? I better double check if we need to
contend with more than one input on the command-line.

With what you gave so far, that shouldn't be a concern. Your loop was
controlled by argc.
Let me check/try these two things and get back to you all.

That's a good idea. If you continue to have trouble, post the new code.



Brian
 
K

KL

KL wrote:




Ok. I might suggest putting the test in anyway, just to show how cool
you are, but probably not necessary for a school problem.

If it weren't for the crunch of having this thing due within 2 days, I
might do that. But right now, I will be happy to just complete it as
assigned.
With what you gave so far, that shouldn't be a concern. Your loop was
controlled by argc.
You were right, it wasn't the for loop per se, it was the incorrect math
correction I had made to the for loop.
That's a good idea. If you continue to have trouble, post the new code.



Brian

I will sure post new code when I hit the next bump....knowing me that
will be shortly. I have found that sometimes just posting a question
leads you to the answer, you know?
 
J

Jim Langston

KL said:
KL wrote:

on 3/3/2006 5:47 PM Default User said the following:

This has two problems. One is that you don't verify that the
argument indeed starts with that string. Secondly, it's somewhat
inefficient. While efficiency is always implementation-specific,
and shouldn't drive the program, in this case it's algorithmically
inefficient as well.

I'd do something like this:

size_t offset = 0;

if (strncmp(argv[a], "http://", 7) == 0)
{
offset = 7;
}

url = argv[a]+offset;


That saves allocating space for those characters that you just
erase a moment later.

In this assignment, we know that the user will include the http:// so
that isn't an issue.


How do you know that?

Apparently, I am not parsing the command-line
properly. That is where I am a bit lost.


Fix the issues already pointed out. Run it again. Give us your input
data, your results, and how those results differed from your
expectations.


Brian
Thanks for the replies Brian.

I do know that the professor already told us he would be using the
standard http:// in the command-line. So that is a non issue, THANK GOD!
I was able to fix the earlier problem, math helps sometimes LOL

Now it will compile and runs, but it runs into a segmentation fault (core
dump) issue. But I am just now thinking that it might have to do with
haveing a for loop. I mean, why have it if there will only be one site on
the command-line? I better double check if we need to contend with more
than one input on the command-line.

Let me check/try these two things and get back to you all.

Thanks again,

KL

If there is only one parameter passed, such as
http://www.mysite.com/index.html then it will most likely be found at the
pointer argv[1]

I know that argv[0] is (usually) the executable name itself. Such as, if
your program is myprogram.exe then argv[0] will point to the c-string
"myprogram.exe" although some may include the path.

Get rid of the for loop. std::cout argv[1] and make sure it is what you
expect it to be then do the parsing and have it display what it prints out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top