Vignaesh
asked on
CAsyncsocket bind function urgent plz
Hi all,
I am developing a server application using the CAsynsocket class. I have used a windows32 console application and had managed to create a socket. But the problem is to bind it to the local port. I have a query whether its necessary to bind the server socket. My code is as follows
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <afxsock.h>
#include <winsock.h>
//using namespace std;
//TCHAR szError[100];
int main(int argc, char* argv[])
{
// TODO: Place code here.
int nRetCode =0 ;
UINT nPort = 0;
char* Socketaddr = "localhost";
// SOCKADDR_IN sockaddr;
// hostent *he;
//************************ ********** ********** ********** ********** ********** ********** //
//test address structure
// sockaddr.sin_addr. =
//************************ ********** ********** ********** ********** ********** ********** *//
//initialising MFC and print error on failure
if(!AfxWinInit(::GetModule Handle(NUL L),NULL,:: GetCommand Line(),0))
{
printf( _T("Fatal Error: MFC initialization failed"));//<< endl;
return 1;
}
//************************ ********** ********** ********** ********** ********** ********** //
//Establish a connection to the server socket
if(!AfxSocketInit())
{
AfxMessageBox("AfxSocketIn it() Failed");
}
CAsyncSocket* pSocket = new CAsyncSocket;
printf("Creating Socket Object\n");
//************************ ********** ********** ********** ********** ********** ********** *//
// creation
int test = 0;
test = pSocket->Create(nPort,SOCK _STREAM);
if(test !=0)
{
printf("Succesfully Created Socket on %d\n",nPort,GetLastError() );
}
else
{
printf("Cannot initialise socket\n",pSocket->GetLast Error());
}
//************************ ********** ********** ********** ********** ********** ********** **//
//bind operation
int bindtest = 0;
bindtest = pSocket->Bind(nPort,NULL);
if(bindtest == 0)
{
printf("test value for bind printed %d",pSocket->GetLastError( ));
}
//test line printf("The value of create is ",test );
return 0;
}
The bind function always returns zero...hope some one could help me on this as i m stuck with this point
I am developing a server application using the CAsynsocket class. I have used a windows32 console application and had managed to create a socket. But the problem is to bind it to the local port. I have a query whether its necessary to bind the server socket. My code is as follows
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <afxsock.h>
#include <winsock.h>
//using namespace std;
//TCHAR szError[100];
int main(int argc, char* argv[])
{
// TODO: Place code here.
int nRetCode =0 ;
UINT nPort = 0;
char* Socketaddr = "localhost";
// SOCKADDR_IN sockaddr;
// hostent *he;
//************************
//test address structure
// sockaddr.sin_addr. =
//************************
//initialising MFC and print error on failure
if(!AfxWinInit(::GetModule
{
printf( _T("Fatal Error: MFC initialization failed"));//<< endl;
return 1;
}
//************************
//Establish a connection to the server socket
if(!AfxSocketInit())
{
AfxMessageBox("AfxSocketIn
}
CAsyncSocket* pSocket = new CAsyncSocket;
printf("Creating Socket Object\n");
//************************
// creation
int test = 0;
test = pSocket->Create(nPort,SOCK
if(test !=0)
{
printf("Succesfully Created Socket on %d\n",nPort,GetLastError()
}
else
{
printf("Cannot initialise socket\n",pSocket->GetLast
}
//************************
//bind operation
int bindtest = 0;
bindtest = pSocket->Bind(nPort,NULL);
if(bindtest == 0)
{
printf("test value for bind printed %d",pSocket->GetLastError(
}
//test line printf("The value of create is ",test );
return 0;
}
The bind function always returns zero...hope some one could help me on this as i m stuck with this point
It is not legal to bind to port 0 (value of nPort at call to Bind). Try another port.
Also, why are you binding to a port after Create? Create binds to a port. If you pass 0 to create, Windows picks a port. For client-side, this is usually correct. The client usually does not have to use a specific port.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It's the second comment that is the problem (socket already bound). I looked at the implementation of Bind and it would select a port for you if you specify port 0 and TCP.