Jump to content
  • Advertisement
Sign in to follow this  
fruni

What am i doing wrong?

This topic is 3891 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

Well I am new on the winsock scene and I am trying to get my client to connect to the server... Here is the program: MAIN.CPP
#include "library.h"

int main()
{
	// init winsock...
    initwinsock();
	// init sockets
	cout << "Loading sockets... ";
		SOCKET s = socket (AF_INET, SOCK_STREAM, 0); // Create socket
		sockaddr_in addr; // the address structure for a TCP socket
		addr.sin_family = AF_INET;      // Address family Internet
		addr.sin_port = htons (7000);   // Assign port 5001 to this socket
		addr.sin_addr.s_addr = htonl (INADDR_ANY);   // No destination
		if (bind(s, (LPSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR)
		{ // error
			cout << "\nerror initializing sockets.\n";
			system ("pause");
			WSACleanup ();  // unload WinSock
			return 1;         // quit
		}
	cout << "success" << endl;
	
	//Starting to Listen...
	cout << "Initializing listen server... ";
		// WSAStartup () has been called
		// SOCKET s is valid
		// s has been bound to a port using sockaddr_in sock
		if (listen(s,5)==SOCKET_ERROR)
		{ // error!  unable to listen
			cout << "Unable to listen..." << endl;
			WSACleanup ();
			return 1;
		}
	cout << "Success" << endl;
	
	//:D Everything works fine now.
	cout << "Server has started up successfuly." << endl; 
	system ("pause");
	WSACleanup();
}
library.h
#ifndef LIBRARY_H
#define LIBRARY_H

#include <iostream>
#include <fstream>
#include <winsock2.h>
using namespace std;

int initwinsock();
helper files.cpp
#include "library.h"

int initwinsock()
{
	cout << "Setting up winsock... ";
    WSADATA wsaData; 
	if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) 
	{
        cout << "\nWSAStartup failed.\n";
		system ("pause");
        return 1;
	}
	cout << "Success" << endl;
}
and here is the client: (if it helps it is another project)
#include <iostream>
#include <fstream>
#include <winsock2.h>
using namespace std;

int main()
{
	// Initialising winsock
	cout << "Setting up winsock... ";
	WSADATA wsaData; 
	if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) 
	{
		cout << "\nWSAStartup failed.\n";
		system ("pause");
		return 1;
	}
	// Trying to connect to server...
	cout << "Connecting to server... ";
    SOCKET s = socket (AF_INET, SOCK_STREAM, 0); // Create socket
	sockaddr_in target;
	target.sin_family = AF_INET;           // address family Internet
	target.sin_port = htons (7000);        // set server’s port number
	target.sin_addr.s_addr = inet_addr ("127.0.0.1");  // set server’s IP
	if (connect(s, target, sizeof(target)) == SOCKET_ERROR)
	{ // an error connecting has occurred!
	  WSACleanup ();
	  return 1;
	}

	cout << "omg :D it works" << endl;
	system ("pause");
	return 0;
}
this is the error i get: ------ Build started: Project: test_client, Configuration: Debug Win32 ------ Compiling... Test Client.cpp c:\documents and settings\hrd\desktop\pie\project_piston\test_client\test client.cpp(24) : error C2664: 'connect' : cannot convert parameter 2 from 'sockaddr_in' to 'const sockaddr *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called Build log was saved at "file://c:\Documents and Settings\HRD\Desktop\pie\PROJECT_PISTON\test_client\Debug\BuildLog.htm" test_client - 1 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ========== :) I am new to this all and am not sure what I am doing wrong, The tut says it should be right. Also I am using visual C++ express edition 2008 on windows XP

Share this post


Link to post
Share on other sites
Advertisement
Would I be wrong in assuming that you are new to C++/programming all-together? It seems that the error you have is very well described by the compiler.

This line is throwing the error:

if (connect(s, target, sizeof(target)) == SOCKET_ERROR)

It says parameter two (which in this case is 'target') is of type 'sockaddr_in', when it should be a 'const sockaddr *'. Now, I'm no C++ guru, but I'm sure that that means you need a constant pointer to a different type of object that the one you've given it. You are giving the connect function the wrong information.

I know very little about C++, yet I think I've found the error in your code. I would suggest learning how to properly debug and actually know your language before you start a topic as heinously complex as sockets.

Honestly, start small!

(I apologise if this is misdirected or if it seems overly negative).

Share this post


Link to post
Share on other sites
Thanks NickHigh i agree with your post and I am new to C++ But We all got to start somewhere. But yeh I sort of figured most of that out but I am not sure how to fix it as the tut says it should be working... Anyway thanks :) (also if anyone can give a good tutorial for newbs on winsock it would be nice :) )

Share this post


Link to post
Share on other sites
That's not the point. Sockets are an intense topic, and not something for beginners. Yes, everyone has to start somewhere, so start small. If you can't fix that error, then you probably shouldn't be doing sockets anyway. Copying tutorials verbatim teaches you nothing.

Someone help me out here...

Share this post


Link to post
Share on other sites
Sockets generally aren't *that* complicated, but you do need to know what your doing and how to use them; theres not really that much to the winsock library to get things going. (but I do support NickHighIQ comments somewhat)

I think the problems come when you start considering how your program may handle many clients at once, and then you enter multi-threaded territory.

generally speaking, first you use the accept function to create a socket for your client requests;

SOCKET client = accept(listening_socket,NULL,NULL);

then you spawn a new thread to handle that client. which is simpler said then done ;-) If you are just experimenting you could probably ignore this bit.

to recv data you do;

static int length = 4095;
char buffer[length]; // any old length
int result;

result = recv(client,buffer,length,0);

and sending is simular;

send(client,buffer,length,0);

You can use windows Hyperterminal to test your server, by connecting into it using the local address 127.0.0.1 and the port number.

good luck.

Share this post


Link to post
Share on other sites
Quote:
Original post by moosedude
I think the problems come when you start considering how your program may handle many clients at once, and then you enter multi-threaded territory.


That's what I was talking about, you've articulated it a bit clearer though. Sockets aren't all that complex in themselves, but using them to create something like a server/client interface for a LAN game is one of the much harder topics out there. And debugging threads will drive you completely bat-shit, trust me - if you can't debug this, good luck.

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!