Hi all,

My csocket program is as follow. The program run properly but the server don't wait the message from client and the client also don't send. Can anyone help me what's the problem in client server communciation API. Thanks a lot in advance.

#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions
#include <afxsock.h>     // MFC socket extensions

#include <winsock.h>
//#include <windows.h>

//CSocket gensoc;

void server( int port)
{
    printf("\n Server \n\n");

    CSocket gensoc;
    gensoc.Create ( port );

    SOCKADDR_IN addr;

    memset( &addr, 0, sizeof( SOCKADDR_IN ) );
    addr.sin_family = AF_INET;
    addr.sin_port = htons (port);

    addr.sin_addr.s_addr = INADDR_ANY;


    if (bind( gensoc, (SOCKADDR *)&addr , sizeof(addr)))
    {
        printf ("\n Bind success");
    }
    else
    {
        printf ("\n Bind fail");
    }

    //gensoc.Listen();


    if (listen( gensoc,0) )
    {
        printf ("\n listening" );

    }
    else
    {
        printf ("\n socket error" );
    }


    CSocket newsoc;
    int nlen = sizeof(addr);

    //gensoc.Accept(newsoc);

    if( accept (gensoc,(SOCKADDR *)&addr , &nlen))

    {
        printf ("\n Accept() function success" );

    }
    else
    {
        printf ("\n Accept() function failed" );
    }

    int nBytesRead;
    char cbBuffer[2000];

    nBytesRead = gensoc.Receive ( cbBuffer, sizeof ( cbBuffer ));
gensoc.close();
newsoc.close();

}

void client( char* remote_mac, int port)
{
    printf("\n Client \n\n");

    CSocket gensoc;
    gensoc.Create ( port );

    SOCKADDR_IN addr;

    memset( &addr, 0, sizeof( SOCKADDR_IN ) );
    addr.sin_family = AF_INET;
    addr.sin_port = htons (port);

    addr.sin_addr.s_addr = INADDR_ANY;


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

    addr.sin_addr.s_addr = inet_addr( remote_mac );

    /*
    if (addr.sin_addr.s_addr == INADDR_NONE)
    {
        HOSTENT *lpHostEnt;
        lpHostEnt = gethostbyname( remote_mac );
        if (!lpHostEnt)
        {
            char* lpAddr = ( char* ) inet_ntoa(*(LPIN_ADDR)*(lpHostEnt ->h_addr_list) );
            addr.sin_addr.s_addr = **(int**)(lpHostEnt->h_addr_list);
        }
    }*/

//if (gensoc.Connect( (SOCKADDR*) &addr, sizeof( SOCKADDR_IN ) ) )
    if (connect(gensoc, (SOCKADDR*) &addr, sizeof( SOCKADDR_IN ) ) )
    {
        printf( "\n connect was successful" );
    }
    else
    {
        printf( "\nconnect was not successful" );
    }


    int nBytesWritten;
    static char msg[] = "Hello socket";

    nBytesWritten = gensoc.Send ( msg, sizeof ( msg ));
gensoc.close();

}

void main( int argc, char* argv[] )
{

    switch( argc )
    {
        case 2:
            server( atoi( argv[1] ) );
            break;
        case 3:
            client( argv[1], atoi( argv[2]) );
            break;
        default:
            printf("Usage: main[RemoteMachien] port#\n");
            printf("If remote machine is not provided, application will run in server mode listening to specified port number \n");
            printf("If remote machine is provided, application will run in client trying to reach specified server \n");
    }



}

Use this before using the Winsock Functions

WSADATA wsaData;
	int Ret;
    if ((Ret = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0)
   {
      printf("WSAStartup failed with error %d\n", Ret);
      return;
   }

Some other things that I saw later..
listen and bind and maybe the other functions also, return 0 on correct operation. So the code

if ( bind( parameters...) )
{
      //COrrect op
}
else
{
        // wrong
}

is wrong.
It should be

if ( bind( parameters ) == 0 )
{
       //Correct Operation
}
else
{
       //Error
}

Refer the Specifications of the bind, listen, connect, etc. functions and correct the code accordingly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.