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 18 m 19 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, 24 views)

Reply With Quote
  #2  
Old May 27th, 2007, 10:18 AM
salem's Avatar
salem salem is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jun 2005
Posts: 2,026 salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 1 Month 2 Weeks 2 h 57 m 12 sec
Reputation Power: 839
> 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.
UK Voter? Please send a message to Incapability Brown and the rest of Zanu-Labour

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



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

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





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