Winsock2 for Games Tutorial Errors

Started by
4 comments, last by Limitz 17 years, 8 months ago
Hey, I have been following this tutorial: Here But I have a question as to once I get it listening, which everything works so far, when I try to connect, I get 3 errors: error C2664: 'connect' : cannot convert parameter 2 from 'struct sockaddr_in' to 'const struct sockaddr *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called error C2664: 'accept' : cannot convert parameter 2 from 'struct sockaddr' to 'struct sockaddr *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called error C2065: 'startThread' : undeclared identifier Error executing cl.exe. My code is as follows


WSADATA wsaData;
		int numClients = 0;

		// Initialize Winsock
		iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
			
		if (iResult != 0) 
		{		
			printf("WSAStartup failed: %d\n", iResult);
			return 1;
		}
		else
		{	
			printf("WSAStartup success!\n");

			SOCKET s = socket (AF_INET, SOCK_STREAM, 0); // Create socket
			
			sockaddr_in addr; // the address structure for a TCP socket
			int addr_size = sizeof (sockaddr);

			addr.sin_family = AF_INET;      // Address family Internet
			addr.sin_port = htons (PORT);   // Assign port to this socket
			addr.sin_addr.s_addr = htonl (INADDR_ANY);   // No destination

			if (bind(s, (LPSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR)
			{ // error
				 WSACleanup ();  // unload WinSock
				 cout << "ERROR" << endl;
				 return 0;         // quit
			}
			else
			{
				cout << "Socket now bound." << endl;

				if (listen(s,MAXPLAYERS)==SOCKET_ERROR)
				{ // error!  unable to listen
					WSACleanup ();
					cout << "ERROR" << endl;
					return 0;
				}
				else
				{
					cout << "Server is listening..." << endl;
					
					sockaddr_in target;
					target.sin_family = AF_INET;           // address family Internet
					target.sin_port = htons (PORT);        // set server’s port number
					target.sin_addr.s_addr = inet_addr ("69.169.48.222");  // set server’s IP

					if (connect(s, target, sizeof(target)) == SOCKET_ERROR)
					{ // an error connecting has occurred!
						WSACleanup ();
						return 0;
					}


					SOCKET client[1000];        // socket handles to clients
					sockaddr client_sock[1000]; // info on client sockets

					while (numClients < MAXPLAYERS) // let MAX_CLIENTS connect
					{
						client[numClients] =  // accept a connection
						accept (s, client_sock[numClients], &addr_size); 
							if (client[numClients] == INVALID_SOCKET)
							{ // error accepting connection
								WSACleanup ();
								return 0;
							}
							else
							{	// client connected successfully
								// start a thread that will communicate with client
								startThread(client[numClients]);
								numClients++;
							}
					}
				}
			}
		}
Advertisement
Hi, for the connect you should cast it and make a pointer out of it like this:
struct sockaddr_in sock_addr;connect(SOCK,(struct sockaddr*) &sock_addr, sizeof( sock_addr ));


As for the accept, i use the same idea:
accept(SOCK,(SOCKADDR*) &sock_addr, &sz)


And the startThread method can't be found, make sure you included it before you call it.

Greetings.
Thank you, but I got even more errors;

I tried replacing my connect with:

connect(SOCK,(struct sockaddr*) &sock_addr, sizeof( sock_addr ))


But replacing SOCK with s, my socket object.



Edit: including the startThread? Where can I find it.. on the page there is nothing on ther except for the use of it, so I thought it was defined in winsock.
Quote:Original post by Crazyfool
Thank you, but I got even more errors;

I tried replacing my connect with:

connect(SOCK,(struct sockaddr*) &sock_addr, sizeof( sock_addr ))


But replacing SOCK with s, my socket object.


And replacing &sock_addr with &target off course, and sizeof( sock_addr) with sizeof( target) ... (it was my own code, sorry)


EDIT: startThread: this would be the function that handles the connection with the client and has some protocol you define. This is where you communicate with the client. You need to make that yourself.

EDIT: accept in your code would be:
accept (s, (SOCKADDR*) &(client_sock[numClients]), &addr_size);

or: accept (s, (SOCKADDR*) client_sock + numClients, &addr_size);
;)
Ahhhh, I absolutely love you! I figured the start thread was my own thing, sorry!
Quote:Original post by Crazyfool
Ahhhh, I absolutely love you! I figured the start thread was my own thing, sorry!


Hehe, ok... good luck

This topic is closed to new replies.

Advertisement