Thread: string not a member of std???

  1. #16
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    I know, I'm sorry. I was just fixing it for what you guys said right now. Also, I'm going to go through it a couple times before I post it again. I wasn't trying to be rude so sorry for that also.

  2. #17
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Alright it compiles now.
    But it just shows on the screen on and off and just loops through that >.< Now why would that happen
    Client.cpp
    Code:
    #include "header\ClientHeader.h"
    
    int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmd,int nShow)
    {
    	bool LookForConnections=true;
    	int nret,Connected;
    	WSADATA wsaData;
    	WSAStartup(MAKEWORD(1,1),&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);
    	SOCKADDR_IN serverInfo;
    	serverInfo.sin_family=AF_INET;
    	serverInfo.sin_addr=*((LPIN_ADDR)*hostEntry->h_addr_list);
    	serverInfo.sin_port=htons(48245);
    	while(LookForConnections==true)
    	{
    		SOCKET theSocket;
    		theSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    		Connected=nret;
    		Connected=connect(theSocket,(LPSOCKADDR)&serverInfo,sizeof(struct sockaddr));
    		if(Connected!=nret)
    		{
    			char Command[128];
    			std::string CommandString;
    			recv(theSocket,Command,128,0);
    			CommandString=Command;
    			if(CommandString=="ExitClient"||CommandString=="QuitClient")
    			{
    				LookForConnections=false;
    				closesocket(theSocket);
    			}
    			if(CommandString=="Disconnect")
    			{
    				closesocket(theSocket);
    			}
    			if(CommandString=="FileTransfer")
    			{
    				int FileSize,BufferLength;
    				char* FileName;
    				char* Buffer;
    				recv(theSocket,Buffer,3,0);/*Gets the size of needed buffer this
    			comes as a int say 25... but it's as a char* so I have to figure out
    			how to get it to read as a int value of 25...maybe I can get it as a
    			typecast of a char**/
    				BufferLength=(int)Buffer;
    				if(BufferLength>128)
    				{
    					send(theSocket,"25",2,0);
    					send(theSocket,"The buffer is too large.",24,0);
    					closesocket(theSocket);
    				}
    				recv(theSocket,FileName,BufferLength,0);//Gets the Files name
    				FileSize=GetFileSize(FileName);
    				send(theSocket,(const char*)FileSize,
    				(strlen((const char*)FileSize)),0);
    				/*Sends size of buffer needed*/
    				char FileContents[FileSize];//=GetFileContents(FileName)
    											//notcreatedyet
    				/*Stores file contents in buffer*/
    				send(theSocket,(const char*)FileContents,FileSize,0);
    				/*send file contents*/
    			}
    			if(CommandString=="Help")
    			{
    				//The server will reply so we will just ignore it
    			}
    			else
    			{
    				system(Command);//I'm not going to worry about
    								//Security yet
    				if(/*Invalid Command*/1==1/*just to compile*/)
    				{
    					char Reply[17]="Invalid Command.";
    					int ReplyLength=17;
    					send(theSocket,(const char*)ReplyLength,2,0);
    					/*Sends the int length of the reply so the recipient will
    					know how large to make the buffer*/
    					send(theSocket,Reply,ReplyLength,0);
    				}
    			}
    		}
    	}
    	WSACleanup();
    	return 0;
    }
    ClientHeader.h
    Code:
    #include <windows.h>
    #include <winsock.h>
    #include <fstream>
    #include <stdio.h>
    #include <string>
    
    int GetFileSize(char*);
    Clientdefinitions.cpp
    Code:
    #include "..\header\ClientHeader.h"
    
    int GetFileSize(char*FileName)
    {
    	long end,begin;
    	int BufferSize;
    	std::ifstream a_file(FileName);
    	begin=a_file.tellg();
    	a_file.seekg(0,std::ios::end);
    	end=a_file.tellg();
    	BufferSize=((end-begin)+1);
    	return BufferSize;
    }
    Last edited by bikr692002; 05-29-2006 at 01:32 PM.

  3. #18
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    You still haven't corrected some of the fundamental issues others have pointed out in your code. Personally, I think you need to perhaps step away from sockets and look at/work on some basics.

    As was previously mentioned, the lack of vertical whitespace (the occasional blank line) makes your code extremely hard to read. Separate sections of code with a blank line; it not only aids reading, but implies that this code does something different from the code following. It's like a new paragraph in a book - just as you separate ideas with paragraphs, you separate code with blank lines.

    Now, this issue:
    Code:
    			int FileSize,BufferLength;
    				char* FileName;
    				char* Buffer;
    				recv(theSocket,Buffer,3,0);/*Gets the size of needed buffer this
    			comes as a int say 25... but it's as a char* so I have to figure out
    			how to get it to read as a int value of 25...maybe I can get it as a
    			typecast of a char**/
    				BufferLength=(int)Buffer;
    				if(BufferLength>128)
    				{
    					send(theSocket,"25",2,0);
    					send(theSocket,"The buffer is too large.",24,0);
    					closesocket(theSocket);
    				}
    First, if you'd initialize your variables, you'd spot errors a lot quicker. The biggest issue: Buffer is a pointer to what? It's not - rather, it's undefined. It's a pointer to garbage, and you're telling recv() to store the result to some random location in memory. Happy things will not follow.

    The next issue is your typecasting of BufferLength. You're casting the entirely wrong thing. BufferLength is going to be the location that Buffer points to in memory, which, as above, is unknown.

    Finally, why do you send "25" ? Is this your "buffer length"? (I suspect so) If you think you can cast this to an int, think again: This is ASCII "25", not binary. If you want to send an integer, you can cast a 25 somewhere in memory to a char *, and send the size of an integer, but this is far from an elegant solution. Basically, sending "25" does send "25", but rather 0x 3235 ... equal to 13618 (on some systems).

    It seems to me you don't full grasp pointers. Read up on them, and the difference between 2 and '2'. Also, note that all calls to send()/recv() don't guarentee to send or recv the amount you tell them to. It's your job to call them again if need be.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  4. #19
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Alright Cactus, I'll read up on more of the basics. The lack of verticle spaces though is because it makes it easier for me to read imo but I'll try it w/ spacers next time. Oh yeah good news, it connects =D I just had a friend run the client code off his computer and it went straight through =) I'm happy with that now that I have the overall thing dont I will work on correcting the horrible mistakes I made.

    EDIT: Yeah, I'm not that good with pointers, I am not shure exactly what they do.

    Now that I think about it, I seem to get them a bit more. The reason why they help with strings is because we can pinpoint the location in memory, right?
    Last edited by bikr692002; 05-29-2006 at 03:36 PM.

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