GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 30-Jun-2009, 12:04
Seraphis Seraphis is offline
Junior Member
 
Join Date: Jul 2006
Posts: 34
Seraphis is on a distinguished road

Winsock: Bind failing for client but not server


Hello,

The title pretty much explains what's happening here... When I run the program I just return my own error code of 102; which means the bind on the client portion of the engine has failed.

Files

networkEngine.h
CPP / C++ / C Code:
#ifndef NETWORKENGINE_H
#define NETWORKENGINE_H

#include <winsock2.h>
#include <windows.h>

class networkEngine
{
public:
	int initWinsock(void);
	int bindSocket(void);
	
protected:
	WSADATA wsaData;
	SOCKADDR_IN sockAddr;
	SOCKET aSocket;
};

class networkClient : public networkEngine
{
public:
	~networkClient();
	int Connect(char addr[], int port);
};

class networkServer : public networkEngine
{
public:
	~networkServer();
	int Start(int port);
};

#endif

networkEngine.cpp
CPP / C++ / C Code:
#include "networkEngine.h"

#include <iostream>
using namespace std;

int networkEngine::initWinsock(void)
{
	if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0)
		return 0;

	if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 0)
	{
		WSACleanup();
		return 0;
	}

	return 1;
}

int networkEngine::bindSocket(void)
{

	if (bind(aSocket, (SOCKADDR*)(&sockAddr), sizeof (sockAddr)) == SOCKET_ERROR)
		return 0;

	return 1;
}

networkClient::~networkClient()
{
	WSACleanup();

	if (aSocket != INVALID_SOCKET)
		closesocket(aSocket);
}

int networkClient::Connect(char addr[], int port)
{
	if (!initWinsock())
		return 101;

	sockAddr.sin_addr.S_un.S_addr = inet_addr(addr);
	sockAddr.sin_port = htons((u_short)port);
	sockAddr.sin_family = AF_INET;

	aSocket = socket(AF_INET, SOCK_STREAM, 0);
	
	if (!bindSocket())
		return 102;

	if (connect(aSocket, (SOCKADDR*)&sockAddr, sizeof(sockAddr)) == SOCKET_ERROR)
		return 103;
	
	return 1;
}

networkServer::~networkServer()
{
	WSACleanup();

	if (aSocket != INVALID_SOCKET)
		closesocket(aSocket);
}

int networkServer::Start(int port)
{
	if (!initWinsock())
		return 201;

	sockAddr.sin_addr.S_un.S_addr = INADDR_ANY;
	sockAddr.sin_family = AF_INET;
	sockAddr.sin_port = htons((u_short)port);

	aSocket = socket(AF_INET, SOCK_STREAM, 0);

	if (!bindSocket())
		return 202;

	listen(aSocket,SOMAXCONN);

	SOCKET newSocket = accept(aSocket, NULL, NULL);

	return 1;
}

main.cpp
CPP / C++ / C Code:
#include <iostream>
#include <conio.h>
#include "networkEngine.h"
using namespace std;

int main()
{
	int uin;

	cout << "1 - Server, 2 - Client" << endl;
	cin >> uin;

	if (uin == 1)
	{
		networkServer nServer;
		int errorCode = nServer.Start(2344);

		if (errorCode != 1)
			cout << "Error #" << errorCode << endl;
	}
	else
	{
		networkClient nClient;
		int errorCode = nClient.Connect("127.0.0.1", 2344);

		if (errorCode != 1)
			cout << "Error #" << errorCode << endl;
	}

	_getch();
	return 0;
}

I've been trying to figure this out... the server binds appropriately, which is odd.

Thanks in advance,
Seraphis.
  #2  
Old 30-Jun-2009, 13:31
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Winsock: Bind failing for client but not server


Quote:
Originally Posted by Seraphis
...the server binds appropriately...
Yes, you need to bind the server to a particular port so that clients can know how to connect.

However...

A client doesn't have to be bound to any particular port by the user. In your case, since the server and client are on the same machine, the client can not be bound to the same port that you have already committed to the server.

The kernel will choose a local port for the client when the connect() function is called for with server IP address and port number. In general, we don't care what the client's local port number is. See Footnote.

Bottom line: Don't call bind() for the client.

Regards,

Dave

Footnote: If you really want to know the client's local port number you can use getsockname():

CPP / C++ / C Code:
int networkClient::Connect(char addr[], int port)
{
    if (!initWinsock())
        return 101;

    sockAddr.sin_addr.S_un.S_addr = inet_addr(addr);
    sockAddr.sin_port = htons((u_short) port);
    sockAddr.sin_family = AF_INET;

    aSocket = socket(AF_INET, SOCK_STREAM, 0);

    // 
    // Do not bind the connection to any particluar port
    // davekw7c
    //if (!bindSocket())
        //return 102;

    if (connect(aSocket, (SOCKADDR *) & sockAddr, sizeof(sockAddr)) ==
        SOCKET_ERROR)
        return 103;

    int len = sizeof(sockAddr);
    if (getsockname(aSocket, (SOCKADDR *) &sockAddr, &len) < 0) {
        perror("getsockname");
        return 0;
    }
    else {
        cout << "Local port number after connect = "
             << htons(sockAddr.sin_port) << endl;
    }
    return 1;
}

But it's not really necessary to know this is it?
Last edited by davekw7x : 30-Jun-2009 at 14:45.
  #3  
Old 30-Jun-2009, 14:17
Seraphis Seraphis is offline
Junior Member
 
Join Date: Jul 2006
Posts: 34
Seraphis is on a distinguished road

Re: Winsock: Bind failing for client but not server


Haven't touched winsock in a while... that makes sense. haha... I feel dumb now.

Thanks man for the quick response!
  #4  
Old 30-Jun-2009, 18:46
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Winsock: Bind failing for client but not server


Quote:
Originally Posted by Seraphis
... that makes sense...
Sometimes it makes sense, but after all these years, I still think there's a little magic in the "secret sauce" that lets this stuff work.

"Any sufficiently advanced technology is indistinguishable from magic."
---Arthur C Clarke: The third law of prediction

"Any sufficiently advanced magic is indistinguishable from technology."
---davekw7x: Comments on network programming

Regards,

Dave
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
BarracudaDrive application server for Linksys NSLU2 BDG Computer Software Forum - Linux 0 24-Mar-2008 15:24
Named virtual host not working Johnnyrotton Apache Web Server Forum 4 04-Sep-2007 21:32
Develop a simple chat application consisting of a client and a server. HELP MEEEEEEE elcrazy C++ Forum 1 11-Mar-2007 22:42
Apache2 config issues monev Apache Web Server Forum 2 28-Jun-2004 07:19
Can't view pages from another machine on the Intranet aevans Apache Web Server Forum 9 14-May-2004 03:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:07.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.