Ferrous Moon
http://www.ferrousmoon.com:80/forums/

Another question about CrissCross
http://www.ferrousmoon.com:80/forums/viewtopic.php?f=45&t=1497
Page 1 of 2

Author:  eddieringle [Sun Oct 12, 2008 8:43 am ]
Post subject:  Another question about CrissCross

Is there anyway to keep the CrissCross console attached to my prompt in Windows?
It always opens a separate console.

Also, I've rewritten my server code:
main.cpp:
Code:
/* * CrissCross * A multi-purpose cross-platform library. * * A product of IO.IN Research. * * (c) 2006-2008 Steven Noonan. * Licensed under the New BSD License. * */ #include "header.h" #include "webserver.h" Console *console = NULL; Server *inst = NULL; int init(string arg) { if(arg == "start") { console->WriteLine("Start web server"); inst = new Server(80); } else if(arg == "stop") { console->WriteLine("Stop web server"); } else if(arg == "help") { console->WriteLine("Usage: WebServer start | stop | help"); } return 0; } int RunApplication ( int argc, char **argv ) { console = new Console (); // Start your application here if (argc <= 1) { string usage = "Usage: WebServer start | stop | help"; console->WriteLine(usage); inst = new Server(80); } else{ init(argv[1]); } // End your application here. #ifdef TARGET_OS_WINDOWS system ( "pause" ); #endif delete console; return 0; }
webserver.cpp
Code:
#include "header.h" #include "webserver.h" Server::Server(int port) { TCPSocket * s = new TCPSocket(); s->Listen((unsigned short) port); Accept(s); } int Server::Accept(TCPSocket * a) { TCPSocket * s = a; TCPSocket * nulls = NULL; while(1) { string in; s->Accept(&nulls); &nulls->Read(in); if(in == "\0") { Respond(&nulls); } else { cout << in << endl; } } return 0; } int Server::Respond(TCPSocket * a) { #ifdef TARGET_OS_WINDOWS system ( "pause" ); #endif return 0; }
webserver.h:
Code:
#include "header.h" #ifndef WEBSERVER_H #define WEBSERVER_H enum HTTPMethod { OPTIONS = 0, //0 GET, //1 HEAD, //2 POST, //3 PUT, //4 TRACE, //5 CONNECT //6 }; class Server { public: Server(int port); // The constructor ~Server(); // The deconstructor int Accept(TCPSocket *a); int Respond(TCPSocket *a); int Parse(char* str[]); }; #endif
I'm getting the following errors:
Code:
webserver.cpp(17): error C2102: '&' requires l-value webserver.cpp(18): error C2664: 'Server::Respond' : cannot convert parameter 1 from 'CrissCross::Network::TCPSocket **' to 'CrissCross::Network::TCPSocket *'
Any ideas?

Author:  Tycho [Sun Oct 12, 2008 2:08 pm ]
Post subject:  Re: Another question about CrissCross

Quote:
Is there anyway to keep the CrissCross console attached to my prompt in Windows?
It always opens a separate console.
0.7.2 is coming out in less than a week. Let me know if the same thing happens with it.
Quote:
I'm getting the following errors:
Code:
webserver.cpp(17): error C2102: '&' requires l-value webserver.cpp(18): error C2664: 'Server::Respond' : cannot convert parameter 1 from 'CrissCross::Network::TCPSocket **' to 'CrissCross::Network::TCPSocket *'
Any ideas?
Code:
if(in == "\0") { Respond(&nulls); }
should be
Code:
if(in == "\0") { Respond(nulls); }

Author:  eddieringle [Sun Oct 12, 2008 3:54 pm ]
Post subject:  Re: Another question about CrissCross

I've changed it but I'm still getting the C2102 error for line 17:
Code:
&nulls->Read(in);
EDIT:

I've looked over tcpsocket.cpp and for the life of me cannot figure out why I'm getting this error. Maybe the doxygen documentation should be a bit more clear?

New webserver.cpp:
Code:
#include "header.h" #include "webserver.h" Server::Server(int port) { TCPSocket * s = new TCPSocket(); s->Listen((unsigned short) port); Accept(s); } int Server::Accept(TCPSocket * a) { TCPSocket * s = a; TCPSocket * nulls = NULL; while(1) { string in; s->Accept(&nulls); if(&nulls != NULL) { nulls->Read(in); if(in == "\0") { Respond(nulls); } } } return 0; } int Server::Respond(TCPSocket * a) { #ifdef TARGET_OS_WINDOWS system ( "pause" ); #endif return 0; }

Author:  Tycho [Sun Oct 12, 2008 11:09 pm ]
Post subject:  Re: Another question about CrissCross

Quote:
I've changed it but I'm still getting the C2102 error for line 17:
Code:
&nulls->Read(in);
The change is simple. Drop the '&'. I am guessing you aren't clear on how pointers work, or this error would be as plain as day.
Quote:
I've looked over tcpsocket.cpp and for the life of me cannot figure out why I'm getting this error. Maybe the doxygen documentation should be a bit more clear?
I agree there should be some examples for TCPSocket and UDPSocket usage, but again: you are missing some pretty basic knowledge here. I highly recommend looking at some C or C++ tutorial site and read up on what the '*' (indirection) and '&' (address of) operators actually do, and in what cases you use them. Before going much further in C/C++, this knowledge is absolutely essential.

I've added some comments to your code:
Code:
#include "header.h" #include "webserver.h" /* * Why not just make it Server::Server(unsigned short port)? You won't use * any other data type for a TCP or UDP port number. */ Server::Server(int port) { TCPSocket * s = new TCPSocket(); s->Listen((unsigned short) port); Accept(s); } int Server::Accept(TCPSocket * a) { TCPSocket * s = a; TCPSocket * nulls = NULL; while(1) { string in; s->Accept(&nulls); /* * This is a useless comparison. Let me phrase this comparison in plain English: * "Is the address of the variable 'nulls' a non-NULL pointer?" * The way to fix the comparison to do what you need is to again drop the '&', * and get this: 'if(nulls != NULL)'. In English: * "Does the variable 'nulls' contain a non-NULL pointer?" * These two questions are -very- different and will greatly affect the result. */ if(&nulls != NULL) { nulls->Read(in); if(in == "\0") { Respond(nulls); } } } return 0; } int Server::Respond(TCPSocket * a) { #ifdef TARGET_OS_WINDOWS system ( "pause" ); #endif return 0; }

Author:  eddieringle [Mon Oct 13, 2008 5:57 am ]
Post subject:  Re: Another question about CrissCross

Whew, it lives! :D

Thanks, finally got a working socket program... now I just have to get it to send/receive data between the server and browser.
(And possibly take a few more trips to learncpp.com ;))
Code:
if(in == "") { cout << "Gotta respond now..." << endl; Respond(nulls); }
The problem is in that block somewhere, I don't have time to look for it right now, so if anyone sees it, I would be grateful.

I have school now, so I won't be able to do anything else until I get back.

Author:  Tycho [Mon Oct 13, 2008 11:14 am ]
Post subject:  Re: Another question about CrissCross

Hint: The return value of 'Read' is not worthless.

Author:  eddieringle [Mon Oct 13, 2008 5:19 pm ]
Post subject:  Re: Another question about CrissCross

Success! I have got it to talk!
Image

and...

Image

This is the first HTTP server ever that uses CrissCross. :)

Author:  Miah [Mon Oct 13, 2008 7:38 pm ]
Post subject:  Re: Another question about CrissCross

Good for you. Do you have any plans on what to do next, or is this more of an experience collection?

Author:  eddieringle [Mon Oct 13, 2008 7:40 pm ]
Post subject:  Re: Another question about CrissCross

I want to at least get as far as a config file or parsing GET commands to serve files instead of hard coding the html.
But this is definitely a great experience for me. :)

EDIT: I've gone as far as a Google Code project to help plan out what I'm going to do, so this is beginning to be more of a job for me.

Author:  eddieringle [Tue Nov 04, 2008 5:47 pm ]
Post subject:  Re: Another question about CrissCross

Okay, everything builds fine. I can get the server to server the page. However, when the program runs and I do not immediately connect via my browser, the program crashes with a segfault.

Didn't have this issue before, I believe it happens at "ccerr = s->Accept(&nulls)", but I'm not complelely sure.

Help would be appreciated. :)

EDIT: Oops, nevermind, I found it, I was telling the server to close the connection when there was no connection to close. :oops:
Code:
int Server::Accept(TCPSocket * a) { TCPSocket * s = a; TCPSocket * nulls = NULL; while(1) { string in; int ccerr; ccerr = s->Accept(&nulls); if (ccerr == CC_ERR_WOULD_BLOCK) cout << "Non-blocking Sockets are enabled..."; if(ccerr == 0) { // If Accept(&nulls) responded with CC_ERR_NONE cout << "Reading from socket..." << endl; if(nulls->Read(in) == 0) { cout << in << endl; cout << "Gotta respond now..." << endl; Respond(nulls); } else { cout << "Error with Reading?" << endl; } } nulls->Close(); ThreadSleep(50); } return 0; }

Author:  Tycho [Wed Nov 05, 2008 2:44 am ]
Post subject:  Re: Another question about CrissCross

When creating the console do this:
Code:
console = new Console(stdout, stdin);
That will prevent it from allocating a console window.

Author:  eddieringle [Sat Nov 08, 2008 3:44 pm ]
Post subject:  Re: Another question about CrissCross

Yes, I'm using that now.

I've switched to c-style strings for my Config reader, but when I try to read from the file I get a segfault.

Anyone know what's wrong?
Code:
int Server::ReadConfig() { char *l; FileReader *f = new FileReader(); f->Open("inceku.conf"); while (!f->EndOfFile()) { f->ReadLine(l,50); } return 0; }

Author:  Tycho [Sat Nov 08, 2008 5:32 pm ]
Post subject:  Re: Another question about CrissCross

Did you even read the documentation for ReadLine()?

First parameter is your preallocated buffer. Second parameter is the size of that buffer.
Quote:
Reads a line of data.

Parameters:
_buffer A character buffer for the data to be stored in.
_bufferLength The length of the buffer specified in _buffer.

Returns:
The number of bytes read.

Author:  eddieringle [Sat Nov 08, 2008 5:34 pm ]
Post subject:  Re: Another question about CrissCross

Yeah, I did read it, but I'm not sure I understand it.

Either way, I found a nice little StringTokenizer for C++ strings, and I got that to work, but if we can solve this problem, then that would be great too.

Author:  Tycho [Sat Nov 08, 2008 5:37 pm ]
Post subject:  Re: Another question about CrissCross

I personally see no reason to use C++ STL strings. C-style strings are as simple as can be and if you use them right, performance is generally better than what you'd get with C++ STL strings.

Page 1 of 2 All times are UTC-05:00
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/