Here's my code
its complaining about "std::string Command;" as "string not a member of std" Any help?Code:Look at new post
Oh yeah, I am only using this for myself so i can get access to my comp from school
Here's my code
its complaining about "std::string Command;" as "string not a member of std" Any help?Code:Look at new post
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.
#include <string>
CProgramming FAQ
Caution: this person may be a carrier of the misinformation virus.
aDUH!
Stupid me.
Thanks.
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.
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:
did you mean:Code:if(Command=="Exit"||"Quit")
and your WinMain function never returns a value... It should return 0 upon successful exit.Code:if(Command == "Exit" || Command == "Quit")
Last edited by mrafcho001; 05-27-2006 at 08:03 PM.
Alright here it is
CLIENT
SERVERCode: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 rightawayCode:Look at new post
Last edited by bikr692002; 05-27-2006 at 09:43 PM.
>>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.
Alright here it is
ClientServerCode:#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; }What do you think so far?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; }
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:My 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);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().Code:char Command[128] = { 0 }; WSAStartup(MAKEWORD(1,1), &wsaData);
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'.
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.
=( 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)
Another problem is that I don't know how to deal with dropped packets and the such.Code:Check new post
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.
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.
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.
For one thing, does this line look awfully strange to you?
Code:if(BufferSize=>9,999&&<100,000)
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
> 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.