Jump to content
  • Advertisement
Sign in to follow this  
Afr0m@n

Cout problems

This topic is 4606 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Why doesn't this cout? :
#include <winsock.h>;
#include <iostream.h>;
	
void InitWinSock()
{
	// Must be done at the beginning of every WinSock program
	WSADATA w;    // used to store information about WinSock version
	int error = WSAStartup (0x0202, &w);   // Fill in w
	
	if (error)
	{ // there was an error
		MessageBox(NULL, "Couldn't initialize WinSock!", "Fatal Error!", MB_OK);
		return;
	}
		
	if (w.wVersion != 0x0202)
	{ // wrong WinSock version!
		MessageBox(NULL, "Couldn't initialize WinSock!", "Fatal Error!", MB_OK);
		WSACleanup (); // unload ws2_32.dll
		return;
	}
}

int main()
{
	SOCKET s[3];
	sockaddr_in me;
	sockaddr you[2];
	int addr_size = sizeof (sockaddr);
	int num_players = 0;

	InitWinSock();

	cout << "WinSock successfully initialized!";

	s[0] = socket (AF_INET,SOCK_STREAM,0);
	me.sin_family = AF_INET; // AF_INET = Adress Family Internet
	me.sin_port = htons (5555); // Use port 5555
	me.sin_addr.s_addr = htonl (INADDR_ANY);
	
	if (bind(s[0],(LPSOCKADDR)&me,sizeof(me))==SOCKET_ERROR)
	{
		cout << "Error:  Unable to bind socket!\n";
		WSACleanup ();
		return 0;
	}
	
	if (listen(s[0],1)==SOCKET_ERROR)
	{
		cout << "Error:  Unable to listen!\n";
		WSACleanup ();
		return 0;
	}

	cout << "Listening for connections...\n";
	while (num_players<2)
	{
		s[num_players+1] = accept(s[0],&you[num_players],&addr_size);

		if (s[num_players+1]==INVALID_SOCKET)
		{
			cout << "Error:  Unable to accept connection!\n";
			WSACleanup ();
			return 0;
		}
		else
		{
			cout << "Player joined!\n";
			MessageBox(NULL, "Player connected!\n", "Success!", MB_OK);
			char buffer[2];
			//sprintf (buffer,"%c%d",RPSS_NUMOFUSERS,num_players+1);
			//send (s[num_players+1],buffer,2,0);
			num_players++;
		}
	}

	return 0;
}


Share this post


Link to post
Share on other sites
Advertisement
#include <winsock.h>;
#include <iostream.h>;
Remove the semi-colons, they shouldn't (hehe) be there

I'm not sure if this will make a difference but try using
#include <iostream>
then doing
std::cout << ...;
. I know the way you're doing it works in Visual C++ 6 but I'm not sure that it works in newer compilers.

Share this post


Link to post
Share on other sites
using namespace std; is also a possible way to make sure the std objects are used, but use cautiously if you're using other namespaces (which at this point I doubt you will be).

Share this post


Link to post
Share on other sites

#include <winsock.h>
#include <iostream>

using namespace std; //this is for cout

void initWinSock ()
{
//....code
}

int main () {

//....code

cout << "WinSock successfully initialized!" << endl;

//....code

return 0;

}


you might want to make this function a bool instead of a void.

bool InitWinSock()
{
// Must be done at the beginning of every WinSock program
WSADATA w; // used to store information about WinSock version
int error = WSAStartup (0x0202, &w); // Fill in w

if (error)
{ // there was an error
MessageBox(NULL, "Couldn't initialize WinSock!", "Fatal Error!", MB_OK);
return false;
}

if (w.wVersion != 0x0202)
{ // wrong WinSock version!
MessageBox(NULL, "Couldn't initialize WinSock!", "Fatal Error!", MB_OK);
WSACleanup (); // unload ws2_32.dll
return false;
}

return true;

}


then in your code you can really test if it is true, for example:
//.... code

int main ()
{

//...some more code

if (initWinSock() )
cout << "initWinSock successfully initialized" << endl;

//...more code

return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by ciroknight
using namespace std; is also a possible way to make sure the std objects are used, but use cautiously if you're using other namespaces (which at this point I doubt you will be).


I second that.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!