Excerpts from client program:
#include <errno.h> /* for declaration of errno /
#include <netdb.h> / for gethostbyname() /
#include <sys/types.h> / needed by <netinet/in.h> /
#include <netinet/in.h> / for htons(), &c, /
#include <sys/select.h> / for fd_set /
#include <sys/socket.h> / for socket(), &c. /
#include <unistd.h> / for getopt(), &c /
…
typedef struct sockaddr SA; / for convenience, à la UNP by WRS */
char const Hostname = “127.0.0.1”; / localhost /
unsigned short Port = 7777;
int SocketFd = -1; / socket for receiving data */
…
int
connectToMtp()
/=============================================================================
| connectToMtp – Connect to the MTP. Return 0 if successful, errno if not.
|=============================================================================/
{
struct sockaddr_in address;
struct hostent *host;
int errorCode;
/* create socket. if unsuccessful, exit */
SocketFd = socket( AF_INET, SOCK_STREAM, 0 );
if( SocketFd == -1 )
{
perror( “socket” );
exit( 1 );
}
/* set up structure for addressing host & port via internet protocol */
address.sin_family = AF_INET;
address.sin_port = htons( Port );
if( host = gethostbyname( Hostname ) )
memcpy( &address.sin_addr, host->h_addr_list[0], host->h_length );
else
{
#ifdef HAVE_INET_ATON
if( !inet_aton( Hostname, &address.sin_addr ) )
#else
if( -1 == ( address.sin_addr.s_addr = inet_addr( Hostname ) ) )
#endif
{
fprintf( stderr, “invalid host name or address\n” );
exit( 1 );
}
}
/* now connect */
if( -1 == connect( SocketFd, ( SA * ) &address, sizeof( address ) ) )
{
errorCode = errno;
close( SocketFd );
SocketFd = -1;
return errorCode;
}
return 0;
}
Excerpts from server program:
#include <errno.h>
#include <sys/types.h> /* needed by <netinet/in.h> /
#include <arpa/inet.h> / for inet_aton() /
#include <netinet/in.h> / for htons() /
#include <signal.h>
#include <stdio.h>
#include <sys/socket.h> / for socket(), &c. /
#include <sys/wait.h> / for waitpid() /
#include <unistd.h> / for getopt(), &c. /
…
typedef struct sockaddr SA; / for convenience, à la UNP by WRS */
…
struct sockaddr_in address, cliAddr;
int cliLen;
…
listenFd = socket( AF_INET, SOCK_STREAM, 0 );
if( listenFd == -1 )
{
perror( “socket” );
exit( 1 );
}
setsockopt( listenFd, SOL_SOCKET, SO_REUSEADDR, &reuseAddr, sizeof( int ) );
address.sin_family = AF_INET;
address.sin_port = htons( port );
address.sin_addr.s_addr = htonl( INADDR_ANY );
/* bind to it */
if( -1 == bind( listenFd, ( SA * ) &address, sizeof( address ) ) )
{
perror( “bind” );
exit( 1 );
}
/* convert it to a passive socket */
if( -1 == listen( listenFd, 5 ) )
{
perror( “listen” );
exit( 1 );
}
/* accept connexions on socket */
while( 1 )
{
ConnFd = accept( listenFd, ( SA * ) &cliAddr, &cliLen );
fprintf( stderr, “host at %s connected\n”,
inet_ntoa( cliAddr.sin_addr ) );
if( -1 == ConnFd )
{
if( errno == EINTR )
continue;
perror( “accept” );
exit( 1 );
}
/* fork child process to handle connexion /
switch( fork() )
{
case -1:
perror( “fork” );
break;
case 0:
/ child process */
close( listenFd );
handleConnexion( cycleTimeMs );
}
/* parent closes connected socket */
close( ConnFd );
}
}
Chris Foran <cforan@qnx.com> wrote:
Can you post your code? Or part of your code?
“Will Parsons” <> wbp@Ysnet.net> > wrote in message
news:> slrn96knmt.e9.wbp@aigialos.thalatta> …
I am developing a rather simple client/server datalink using TCP/IP,
with the server end of the datalink to run under QNX4. The same source
code for both client and server compiles and runs correctly under the
following conditions:
Server Client
QNX4 QNX4 (same or different host)
QNX4 WinNT/Cygwin
QNX4 Sun/Solaris
GNU/Linux GNU/Linux (same host)
When I compile and run run the client and server code under QNXRTP
and try to run both on the same machine, the server bombs on the
accept() call with the error “Bad address”. (The client is specifying
IP address “127.0.0.1”.) The address returned from the accept() call
is 83.12.50.176. (Since accept() is returning -1, I realize this address
may be meaningless, but I am including it in case it has some
significance…)