CAsyncSocket::Bind problem

Hi:
  I'm just playing around with CAsyncSocket in my network consisting of two computers.  Here's my code:

CAsyncSocket m_Socket;
CString      m_SockName;
UINT         m_PortNumber;
BOOL         success;

success = m_Socket.Create(); //success=1
success = m_Socket.GetSockName(m_SockName,m_PortNumber);
  //success=1,m_SockName="0.0.0.0" and m_PortNumber=0x0408

success = m_Socket.Bind(m_PortNumber,"10.0.0.1");
  //Fails here, call GetLastError

DWORD error = ::GetLastError();
  //error=WSAEINVAL, i.e. socket already bound

   My question is - I'm obviously doing things out of sequence.  I've tried calling Create with "10.0.0.1" but it fails.

   Other info that might be relevant: The program is run on Windows 95 and the other computer in the network has Windows 98.

   Thanxs for any help,
         Glenn

LVL 3
GlennDeanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

manloreCommented:
Yes you are doing errors.

It's a bat metod use the Bind metod.

if you want the CAsyncSocket for a client connection you'd not us Bind,  only call Create an then Connect. The class found a port for you.

if you want the CAsyncSocket for a server socket put the IP address (usualy "127.0.0.1" for all local network targets) and the port than you want to use in the Create function (don't call Bind) and wait for the OnAccept call to call Accept.

Remember CAsyncSocket is not UNIX SOCKET. It's full asyncronous and not easy to program. May be you found more convenient the class CSocket.
0
GlennDeanAuthor Commented:
manlore:
   Thanxs for the info.  I tried, after the call to GetSockName, a call to Connect, as in

success=m_Socket.Connect(m_SockName,m_PortNumber);

and it failed.  GetLastError returned
WSAEADDRNOTAVAIL, which makes sense since address 0.0.0.0 can't be a valid one?
         Glenn
0
GlennDeanAuthor Commented:
Some more info on the above call to Connect.  Although it appears the Connect failed, BUT when I shut down my Windows 98 computer it warns me that I have a user connected.
     Glenn
0
GlennDeanAuthor Commented:
manlore:
  I am not rejecting your answer because it is wrong, but with the info you gave me I still can't "connect" properly.
    Glenn
0
manloreCommented:
Excuse mua.
At weekend I can't see your coments.

Code example:

Server:
class Chocket: public CAsyncSocket
{
public:
      Chocket *m_Socketent;

      void Begin()
      {
            if(AfxSocketInit())
            {
                  // We are listen on Port 10002.
                  if(Create(10002, SOCK_STREAM,FD_ACCEPT | FD_READ | FD_CLOSE))
                  {
                        Listen();                        
                  }
                  else
                        printf("Create Socket Error\n");
            }
            else
                  printf("Init Socket Error\n");
      };

      void OnAccept( int nErrorCode )
      {
            m_Socketent = new Chocket;

            if(nErrorCode == 0)
            {
                  if(!Accept(*m_Socketent))
                        printf("Accept Error\n");
            }
            else
                  printf("OnAccept Error: %i, %i\n", GetLastError(), nErrorCode);
      };

      void OnReceive( int nErrorCode)
      {
            if(nErrorCode == 0)
            {
                  char buff[255];
                  int n = Receive(buff, 255);
                  buff[n] = 0;
                  printf("Recive: %s\n", buff);
            }
            else
                  printf("OnReceive Error: %i, %i\n", GetLastError(), nErrorCode);
      };

      void OnClose( int nErrorCode)
      {
            if(nErrorCode == 0)
                  printf("Client close the Socket\n");
            else
                  printf("OnClose Error: %i, %i\n", GetLastError(), nErrorCode);
      }
};

You only need to create a Object of type Choket and call Begin.

Client:
class ChocketCli: public CAsyncSocket
{
public:
      void Begin()
      {
            if(AfxSocketInit())
            {
                  if(Create(0, SOCK_STREAM,FD_CONNECT))
                  {
                        // We are connect to Port 10002 on local machine
                        Connect("127.0.0.1", 10002);
                  }
                  else
                        printf("Create Socket Error\n");
            }
            else
                  printf("Init Socket Error\n");
      };

      void OnConnect( int nErrorCode )
      {
            m_Socketent = new Chocket;

            if(nErrorCode == 0)
            {
                  char sHello = "Hi World!";
                  Send(sHello, strlen(sHello));
                  Close();
            }
            else
                  printf("OnConnect Error: %i, %i\n", GetLastError(), nErrorCode);
      };
};

You only need to create a Object of type ChoketCli and call Begin.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.