473,386 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Socket Help

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*********
Mar 3 '06 #1
9 1812
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-lit....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

Mar 3 '06 #2
KL
on 3/3/2006 2:24 PM mlimber said the following:
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-lit....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);
}

--
KL
Mar 3 '06 #3
KL wrote:
on 3/3/2006 2:24 PM mlimber said the following:
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-lit....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
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Mar 3 '06 #4
KL
on 3/3/2006 5:47 PM Default User said the following:
KL wrote:

on 3/3/2006 2:24 PM mlimber said the following:
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-lit....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.
Mar 4 '06 #5
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

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Mar 4 '06 #6
KL
on 3/3/2006 6:30 PM Default User said the following:
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
Mar 4 '06 #7
KL wrote:
on 3/3/2006 6:30 PM Default User said the following:
Fix the issues already pointed out. Run it again. Give us your input
data, your results, and how those results differed from your
expectations.

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
Mar 4 '06 #8
KL
on 3/3/2006 7:29 PM Default User said the following:
KL wrote:

on 3/3/2006 6:30 PM Default User said the following:
Fix the issues already pointed out. Run it again. Give us your input
data, your results, and how those results differed from your
expectations.


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.


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.
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.

You were right, it wasn't the for loop per se, it was the incorrect math
correction I had made to the for loop.
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


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?

--
KL
Mar 4 '06 #9

"KL" <kl*******@aohell.com> wrote in message
news:vf******************************@telcove.net. ..
on 3/3/2006 6:30 PM Default User said the following:
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.
Mar 4 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: anuradha.k.r | last post by:
hi, i am writing a socket program in python,both client side and server side.I've written the client side which is working perfectly fine(checked it against server program written in C).but as for...
4
by: faktujaa | last post by:
Hi, I am having some problem with callback used in socket implementation. private static void Connect(string strPrtrIPAddr, int intPrtrPort, ref Socket rsocClient) { try { // Create remote end...
9
by: AA | last post by:
This is making me crazy!! Please, if some body can help me. I'm testing a ver simple socket client. In my test I just open and close a connection (in a loop) to my local IIS server (port 80)...
1
by: John Sheppard | last post by:
Thanks to everyone that responded to my previous Socket Programming question. Now I have run into some behavior that I don't quite understand. Programming environment. VS.NET 2003, C#, Windows...
2
by: Droopy | last post by:
Hi, I try to implement a reusable socket class to send and receive data. It seems to work but I have 2 problems : 1) I rely on Socket.Available to detect that the connection is closed (no...
7
by: Colin | last post by:
I'm writing a little console socket server but I'm having some difficulty. Can I ask your advice - where is the best place to get some help on that topic? It would be nice if some people who knew...
2
by: djc | last post by:
I read a network programming book (based on framework 1.1) which indicated that you should 'never' use the RecieveTimeout or the SendTimeout 'socket options' on TCP sockets or you may loose data. I...
6
Sagittarius
by: Sagittarius | last post by:
I will first try to describe my problem in words. I have a simple program, written in C++, that needs to send a single bytearray via a UDP socket to a microprocessor, which returns an answer, also...
3
by: doc | last post by:
What will a flash xml client socket connect to? I have a working php TCP/IP server socket bound to a port >1023 and the flash client will not even connect to it. I can connect to it with non-xml...
1
by: orehian | last post by:
Construct a one-time password system. · Write a server code and a client code. The server code takes as input a username and a one-time password from the client and then sends a message...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.