gsGomer

Members
  • Content count

    19
  • Joined

  • Last visited

Community Reputation

122 Neutral

About gsGomer

  • Rank
    Member
  1. Starting with SDL_net[solved]

    So that makes sense and indeed works. However I'm confused by your semantics, mostly because I'm very new to network programming....like this is my first network program. You say this simulates a "blocking call", and the accept function is for a "non-blocking call." I imagine that this means running accept through a loop only allows the connection of one client at a time. So if this method is limited, how would I go about using the accept method for "non-blocking calls?" I guess what I mean is how would you use the method in a useful network server that will communicate with more then one client? Thanks again for the help! [Edit] Ok, thanks for the "nudge" in the right direction. I managed to pull up some examples of SDL_net application and found that my answer is in fact context sensitive. I suppose that if I was writing this code in a game I would attempt an accept call perhaps 1-3 times a frame (I guess that really depends on the game I'm writing) and implement the rest of the code if I get a connection. I also found socket sets to be very useful for this type of situation. [/Edit] [Edited by - gsGomer on March 22, 2008 12:27:16 AM]
  2. Having two classes both access each other

    Not sure how you have your application set up but perhaps in your header file you could pre-declare your enemy/character class, then in your cpp file you could include the proper header file Example: Character.h class Enemy; //pre-declare class Character { public: ... void TakeDamage(Enemy* &theEnemy); ... }; Character.cpp #include "Enemy.h" ... void Character::TakeDamage(Enemy* &theEnemy) ... Same for the enemy class Enemy.h class Character; //pre declare class Enemy { public: ... void TakeDamage(Character* &theCharacter); ... }; Enemy.cpp #include "Character.h" ... void Enemy::TakeDamage(Character* &theCharacter); ... I'm sure there are better ways to go about this, such as using some type of event system or object messaging system. However this method should work for an easy quick fix. Hope that helps! [edit] Sorry for the "double advice" Seems Tristan beat me to the punch![/edit]
  3. So I am reading through the Beej's guide to Network programming, and its very informative. I am simultaneously trying to create a network application using the SDL_net library. So far everything is working great, but I have run into one stumbling block. I'm sure this is an easy fix, but I wanted to get some experienced advice on the matter. So far I have an application that can be a server or a client, just reads a command line argument "server" to differentiate. Then I call the resolve host function which sets the ipaddress and port to listen (or connect) on. Then I use this address information to create a tcp socket. Now for a server I know I have to call the TCP accept function to accept an incoming client. My goal however is for the server to sit and listen on a port until a client connects and then do something. Here is my code so far: #include "SDL.h" #include "SDL_net.h" bool isServer = false; IPaddress ipaddress; unsigned int port = 19800; char* host = NULL; TCPsocket tcpsock, insock; bool Init() { if(SDL_Init(SDL_INIT_EVERYTHING) == -1) return(false); if(SDLNet_Init() == -1) return(false); if(isServer) { if(SDLNet_ResolveHost(&ipaddress, NULL, port) == -1) return false; if(!(host = const_cast<char*>(SDLNet_ResolveIP(&ipaddress)))) return(false); tcpsock = SDLNet_TCP_Open(&ipaddress); if(!tcpsock) return(false); } else { if(SDLNet_ResolveHost(&ipaddress, "neprog", port) == -1) return false; tcpsock = SDLNet_TCP_Open(&ipaddress); if(!tcpsock) return(false); } return(true); } void cleanup() { if(tcpsock) SDLNet_TCP_Close(tcpsock); SDLNet_Quit(); SDL_Quit(); } int main(int argc, char** argv) { if(argc > 1) { if(strcmp("server", argv[1]) == 0) isServer = true; } if(!Init()) return 1; if(isServer) { insock = SDLNet_TCP_Accept(tcpsock); if(!insock) return 1; else { } } cleanup(); return 0; } Problem is insock always comes back NULL, which means failure to accept. How do I make my server sit and listen (rather then execute the accept function at the same time a client attempts to connect? Should I place the accept function in some sort of loop? Would appreciate any kind of help on this matter! :) [Edited by - gsGomer on March 22, 2008 12:44:33 AM]
  4. strtok unsafe?

    Excellent clarification! I swear for every thread I start on here I walk away learning at least five new things :-) Thanks for the help!
  5. strtok unsafe?

    Zahlman, Lots of good info here, I appreciate you sharing this knowledge with me. It does spawn a couple of questions. First, my code doesn't use strings because...well because its a duplicate of a program in a text book. Its actually the first example in the chapter for strings. Its good to know that the strtok is a bad function, I will remember this for future programs. You say that the STL is dead, this really confuses me. I've been told by a lot of people, be it in books, articles, forums or otherwise that I should learn the STL...I even bought a book called the STL pocket reference. If this is really an outdated topic then perhaps I'm waisting my time? The only other confusion I have is when you say that everything ending with _s is evil because its fundamentally C. Are you saying that I should never mix c and c++? If so what are the downsides to this, I know very little C coding so it wouldn't be a bad thing to not mix these together...would just be good to know if it's good practice. I'm not writing a large project of any kind with any of this code, I'm simply trying to learn the functionality (good and bad) of the string class. I do understand the dangers of using system pause at the end of a function and if I were writing anything practical it would certainly be left out. [edit]However your advice about batch files is not something I've ever tried so I will certainly give that a go![/edit] Again I appreciate your answers and advice on these topics! :)
  6. strtok unsafe?

    Yes I do like your approach to this method quite a bit more, I'm assuming the remove_if function is from the algorithm header you've included? Also, if MS says that strtok could be "unsafe" is it something I should attempt to avoid on all systems? What I mean is, if I was writing this same program on my Ubuntu machine, would I still be "unsafe" to use the strtok method? Thanks for the info!
  7. strtok unsafe?

    Lots of stuff about the CRT in there... did follow a link that states the following about the STL "Beginning with Visual C++ 2005, several enhancements have been made to the libraries that ship with Visual C++, including the Standard C++ Library, to make them more secure. Several methods in the Standard C++ Library have been identified as potentially unsafe because they could lead to a buffer overrun or other code defect. The use of these methods is discouraged, and new, safer methods have been created to replace them. These new methods all end in _s." Is this a MS only issue, or are these methods found in the actual STL??
  8. strtok unsafe?

    Taking the advice I received on using strings instead of char arrays, I decided to get a nice STL book. I'm going through a chapter regarding strings, and must say the methods I'm learning are easy to use and powerful. I wrote a simple program using the strtok method that "strips" punctuation from a string. It looks like the following: #include <iostream> #include <string> using namespace std; int main() { char x[] = "this, (is a) string!"; char *symbol; symbol = strtok(x, ",;.!?"); cout << symbol << endl; while ((symbol = strtok(NULL, " ,;.()!?")) != NULL) cout << symbol << endl; cout << "x at the end is :" << x << ": with length " << strlen(x) << "\n"; system("Pause"); return 0; } I know that system("Pause") is not portable, but I'm writing these on windows so it works for my situation At any rate the program compiles and runs fine, but I get the following warning from visual c++ 2005 express edition: Warning 1 warning C4996: 'strtok': This function or variable may be unsafe. Consider using strtok_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. h:\dev\learning\applieddatastructures\2-8strtok\main.cpp 10 So I looked up strtok_s and it seems easy, but I had some questions on its usage. 1.) Is this part of the STL, if so is it some kind of new addition to the STL or has it been around for a while? 2.) I had thought that it was safer to use strings instead of char arrays to avoid buffer overflow, is this not really the case or is this security warning coming from something else?
  9. Easy pointer question

    I understand now! It also makes sense if I go back to my old c++ book that has a lesson on pointers and references. The lessons say that these items allow access to the data without having to make a local copy. What is left out is what is going on with the pointer variable itself, which you explained very well. Printed, highlighted and thrown in my notes! Appreciate the help very much! :-)
  10. Easy pointer question

    Ashkan: Not only does this answer my question clearly, but you also helped me to solve a problem in a separate program I had written a few weeks ago. I had a situation much like the first memory allocation function you posted. From this information I gather that using a reference to a pointer is a "preferred" or "cleaner" way of using a pointer to a pointer method...which makes sense. It does however raise some confusion about pointers being passed to functions. From what I have learned thus far, pointers are variables that hold a memory address. So if I pass a pointer to a function, I'm just passing a memory address...right? If so then using the new construct would return the memory location of the new data and store it in the pointer. However as you noted when the function returns, the pointer that I passed to the function is still in its original state. So I'm obviously getting something wrong in my thinking. Do I have the definition of a pointer confused, or is there something "different" happening when the pointer is passed to a function as a parameter? I do appreciate all the answers I am getting, really am learning quite a bit here! :-)
  11. Easy pointer question

    Zao: I think I understand what you are saying. If I understand this correctly I can set this variable to NULL, and this would effect the "original" variable I passed to the function as well?
  12. Easy pointer question

    Unfortunately not, the listnode is a very simple class [sourcelang="cpp"] template <class Entry> class listnode { friend class linkedlist<Entry>; private: Entry *data; listnode *next; };
  13. Easy pointer question

    Sorry if this has been answered elsewhere but I couldn't seem to find anything and I'm a bit confused. Going through linked lists in my book and it has the following method: template <class Entry> void linkedlist<Entry>::copylist(listnode<Entry> *&newlist, listnode<Entry> *oldlist); I'm a bit confused about the first parameter as I've never seen this before (the pointer to a reference). While I'm on the subject I've seen pointers that look like "double pointers" (e.g. **dblPntr) that I'm confused about as well. If someone could at least point me in the right direction on this I would appreciate it.
  14. Length of input array

    Sounds great, got my program up and running like a charm. The book I'm going through right now covers portions of STL, but I'll be sure to pick up a book dedicated to the matter. Thanks very much for your help! :)
  15. Length of input array

    Yes I am using c++, so strings do seem to be an easy solution. Based on your advice, should I conclude that using strings is superior to character arrays when using c++ (It seems that way just from their "built in" functions). Also you say I can index a string just as a character array, what does the operator return when I do this. myString[i]; //<--does this return a string or a char?