This is the mail archive of the
cygwin@cygwin.com
mailing list for the Cygwin project.
Connection Refused message from example AF_INET socket code.
- To: cygwin at cygwin dot com
- Subject: Connection Refused message from example AF_INET socket code.
- From: "Smith, Randall L" <randall dot l dot smith at intel dot com>
- Date: Sat, 19 May 2001 19:17:36 -0700
Cygwin;
Could someone please help understand why I'm getting "connection refused"
from the following code. I have searched the cygwin email archives and read
the users guide and have not found an answer. I'm new to Cygwin, but have
many years of Unix experience and little NT experience. The "connection
refused" error occurs regardless effective user id or whether I use
"localhost" or the actual hostname. I'm using the latest version of cygwin
1.3.1 and Win2K.
All assistance will be greatly appreciated.
Randy Smith
--------------------Code
Follows-------------------------------------------------
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define PORT 5726
#define MESSAGE "Yow!!! Are we having fun yet?!?"
//#define SERVERHOST "EDM2"
#define SERVERHOST "localhost"
void
write_to_server (int filedes)
{
int nbytes;
nbytes = write (filedes, MESSAGE, strlen (MESSAGE) + 1);
if (nbytes < 0)
{
perror ("write");
exit (EXIT_FAILURE);
}
}
void
init_sockaddr (struct sockaddr_in *name,
const char *hostname,
unsigned short int port)
{
struct hostent *hostinfo;
unsigned int oct;
char *h_addr_listp;
char *bufp;
int i;
/* allocate buf area in netbuf (16 char long)
* *bufp+0 = 0 ProtoFamily
* *bufp+1 = 2 ProtoFamily
* *bufp+2 = Lower end of port number
* *bufp+3 = Upper end of port number;
* *bufp+4 = internet number;
* *bufp+5 = internet number;
* *bufp+6 = internet number;
* *bufp+7 = internet number;
* *bufp+8-15 = unused and set to 0;
*/
bufp = (char *) malloc ((size_t) 16);
/* zero out the buffer */
memset (bufp, (char) 0, 16);
name->sin_family = AF_INET;
name->sin_port = htons (port);
hostinfo = gethostbyname (hostname);
if (hostinfo == NULL)
{
fprintf (stderr, "Unknown host %s.\n", hostname);
exit (EXIT_FAILURE);
}
name->sin_addr = *(struct in_addr *) hostinfo->h_addr;
#ifdef DEBUG
h_addr_listp = *hostinfo->h_addr_list;
for (i = 0; i < hostinfo->h_length; i++, h_addr_listp++)
{
*(bufp+4+i) = *h_addr_listp;
oct = (*h_addr_listp);
printf("Octet %u is %u %d\n",i, oct, *(bufp+4+i));
}
#endif
}
/////////////////////////////////////////////////
int
make_socket (const char *hname, unsigned short int port)
{
int sock;
struct sockaddr_in name;
/* Create the socket. */
sock = socket (PF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
perror ("socket");
exit (EXIT_FAILURE);
}
/* Give the socket a name. */
init_sockaddr(&name, hname, port);
if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0)
{
perror ("bind");
exit (EXIT_FAILURE);
}
return sock;
}
----------------------------------------------------------------------------
-------
--
Want to unsubscribe from this list?
Check out: http://cygwin.com/ml/#unsubscribe-simple