| | |
client server communication problem in CSocket program
![]() |
•
•
Join Date: Sep 2005
Posts: 17
Reputation:
Solved Threads: 0
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.
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
C Syntax (Toggle Plain Text)
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
is wrong.
It should be Refer the Specifications of the bind, listen, connect, etc. functions and correct the code accordingly.
listen and bind and maybe the other functions also, return 0 on correct operation. So the code
C Syntax (Toggle Plain Text)
if ( bind( parameters...) ) { //COrrect op } else { // wrong }
It should be
C Syntax (Toggle Plain Text)
if ( bind( parameters ) == 0 ) { //Correct Operation } else { //Error }
![]() |
Similar Threads
- Simple Client Server Instant Messaging Java code using UDP datagrams (Java)
- little more about client-server (VB.NET)
- about Client Server (VB.NET)
- php/mysql communication problem (PHP)
- client and server communication (Java)
- Problem with Client-Server Socket Connection (Java)
- java client server quiz. interesting!!! (Java)
Other Threads in the C Forum
- Previous Thread: Program help
- Next Thread: reading a file...please help
| Thread Tools | Search this Thread |
* ansi api array arrays binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o inches include infiniteloop input interest intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






