client server communication problem in CSocket program

Reply

Join Date: Sep 2005
Posts: 17
Reputation: perlsu is an unknown quantity at this point 
Solved Threads: 0
perlsu perlsu is offline Offline
Newbie Poster

client server communication problem in CSocket program

 
0
  #1
Feb 13th, 2006
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");
}



}
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: client server communication problem in CSocket program

 
0
  #2
Feb 13th, 2006
Use this before using the Winsock Functions

  1. WSADATA wsaData;
  2. int Ret;
  3. if ((Ret = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0)
  4. {
  5. printf("WSAStartup failed with error %d\n", Ret);
  6. return;
  7. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: client server communication problem in CSocket program

 
0
  #3
Feb 13th, 2006
Some other things that I saw later..
listen and bind and maybe the other functions also, return 0 on correct operation. So the code
  1. if ( bind( parameters...) )
  2. {
  3. //COrrect op
  4. }
  5. else
  6. {
  7. // wrong
  8. }
is wrong.
It should be
  1. if ( bind( parameters ) == 0 )
  2. {
  3. //Correct Operation
  4. }
  5. else
  6. {
  7. //Error
  8. }
Refer the Specifications of the bind, listen, connect, etc. functions and correct the code accordingly.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC