C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 27th, 2007, 09:32 AM
dead6re dead6re is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 72 dead6re User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 12 m 3 sec
Reputation Power: 6
Winsock Recv Problems

Okay, I'm stuck on how fix the bug I currently have with my recv.

The code I have is:

Code:
void sTheGame::RecieveData() {
	for (std::vector<sClient*>::iterator it = PlayerList.begin(); it != PlayerList.end(); ++it) {
		sClient *Client = (*it);
		if (FD_ISSET(Client->Socket, &Client->SocketData)) {
			char buffer[256];
			Client->AddressLen = recv(Client->Socket, buffer, 256, 0);
			if (Client->AddressLen == 0) {
				// Data Fail, Disconnect Client
			}
			else if (Client->AddressLen > 0) {
				sOutput(buffer);
			}
		}
	}
}


Code:
class sClient {
public:
	eConnectionState ConnectionState; // TODO: Enum
	char *PlayerName;
	int IncorrectPassword;
	int Room;
	SOCKET Socket;
	sockaddr_in Address;
	int AddressLen;
	fd_set SocketData;


	 sClient();
	 ~sClient();

	 void Init();

	// int Get_Socket() { return s; }
	 bool Connected;
	 bool IsPlaying;

};


sOutput is a class function that does: printf("%s\n", buffer);

As you can see from the screen dump, I get a load of jibberish, how do I correct this?
Attached Images
File Type: jpg winsock error.jpg (77.5 KB, 22 views)

Reply With Quote
  #2  
Old May 27th, 2007, 10:18 AM
salem's Avatar
salem salem is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2005
Posts: 1,674 salem User rank is Captain (20000 - 30000 Reputation Level)salem User rank is Captain (20000 - 30000 Reputation Level)salem User rank is Captain (20000 - 30000 Reputation Level)salem User rank is Captain (20000 - 30000 Reputation Level)salem User rank is Captain (20000 - 30000 Reputation Level)salem User rank is Captain (20000 - 30000 Reputation Level)salem User rank is Captain (20000 - 30000 Reputation Level)salem User rank is Captain (20000 - 30000 Reputation Level)salem User rank is Captain (20000 - 30000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 1 Month 4 Days 15 h 40 m 25 sec
Reputation Power: 296
> As you can see from the screen dump, I get a load of jibberish, how do I correct this?
By storing a \0 in the string before you print it. recv() doesn't add this for you.

Code:
	char buffer[256];
	Client->AddressLen = recv(Client->Socket, buffer, sizeof(buffer)-1, 0);
	if (Client->AddressLen == 0) {
		// Data Fail, Disconnect Client
	}
	else if (Client->AddressLen > 0) {
		buffer[Client->AddressLen] = '\0';
		sOutput(buffer);
	}

The -1 in the size allows room to append a \0 even if recv() fills the buffer.
Comments on this post
dead6re agrees: Thanks
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Winsock Recv Problems


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT