473,749 Members | 2,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
_______________ ________0000000 000
_______________ _______00___00_ __00
_______________ ___________00__ __00
_______________ __________00___ __00
_______________ _________00____ __00
_______________ _000____00_____ __00
_______________ ____00000000000 000000
_______________ ______00_______ __00_000
_______________ _____00________ __00
_______________ _____00________ __00
_______________ ____00_________ __00
_______00______ __00___________ __00
_________00____ __00___________ ___00___
___________0000 00_____________ ____00000

********ROLL TIDE*********
Mar 3 '06 #1
9 1823
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(h ostname.c_str() );
if (host == NULL) {
cout << "gethostbyn ame failed" << endl;
exit(1);
}
server.sin_fami ly = 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,resp onse,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
inefficien t. While efficiency is always implementation-specific,
and shouldn't drive the program, in this case it's algorithmically
inefficien t 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*******@aohe ll.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
inefficie nt 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
8525
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 my server program written in python it simply hangs.it does not show any error also.I've tried sample programs available .I don understand what the reason is as i am quite new to it. here is teh server side program: ///////////////////////
4
2271
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 point. System.Net.IPAddress IPAddress = System.Net.IPAddress.Parse(strPrtrIPAddr); System.Net.IPEndPoint IPEndPoint = new System.Net.IPEndPoint(IPAddress,
9
11347
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) using System.Net.Sockets;
1
3396
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 XP. About the architecture: I have a socket server dll that contains a class that handles connections for a given local ipaddress and port. This class(server) can be started or stopped by calls to the appropriate functions. The server class has...
2
702
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 more data to expect). Sometimes, Socket.Available returns 0 but the other end of the connection did not close it ! 2) This class will be used by many other classes so I have to use the
7
2385
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 what they were doing could take a look at my code and tell me where and why I'm going wrong. Any suggestions of groups or forums?
2
15332
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 now see the socket.RecieveTimeout 'property' in the visual studio 2005 help documentation (framework 2.0) and it has example of it being used with TCP socket. This propery is also listed as 'new in .net 2.0'. 1) is the socket.RecieveTimeout...
6
15786
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 via UDP. Everything works just fine - exept when packet loss occurs (or nothing is recieved at the recieving socket). If packet loss occurs, the program locks up, and it needs to be restarted! Here comes the question:
3
3419
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 client. Can anyone explain this to me - it has been driving me mad for weeks! Please help! Thanks
1
2551
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 back to the client if they are correct. Specifications add the function of password-based user authentication to the client/server. The goal of the program is very simple: the server prints out a message only when the user has entered his/her...
0
8996
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8256
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.