Solved

CAsyncsocket bind function urgent plz

Posted on 2004-09-08
4
342 Views
Last Modified: 2013-11-20
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(::GetModuleHandle(NULL),NULL,::GetCommandLine(),0))
      {
            printf( _T("Fatal Error: MFC initialization failed"));//<< endl;
            return 1;
      }
//************************************************************************************//
      //Establish a connection to the server socket
      if(!AfxSocketInit())
      {
            AfxMessageBox("AfxSocketInit() 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->GetLastError());
      }

//**************************************************************************************//


    //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
0
Comment
Question by:Vignaesh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
4 Comments
 
LVL 19

Expert Comment

by:drichards
ID: 12007331
It is not legal to bind to port 0 (value of nPort at call to Bind).  Try another port.
0
 
LVL 19

Expert Comment

by:drichards
ID: 12007380
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.
0
 
LVL 19

Accepted Solution

by:
drichards earned 500 total points
ID: 12007399
Here are a couple of lines from the Create docs:

"Parameters
nSocketPort
  A well-known port to be used with the socket, or 0 if you want Windows Sockets to select a port. "

"Create then calls Bind to bind the socket to the specified address. The following socket types are supported"
0
 
LVL 19

Expert Comment

by:drichards
ID: 12008462
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mixString challenge 36 138
method notes when mouse over in eclipse 5 97
while loop over for loop 7 119
sumHeights2  challenge 7 140
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question