AF_INET connection refused help pls

Haksun Li
Thu Dec 6 03:21:00 GMT 2001

Dear all,

This is my first time sending to the mailing list. I hope I am doing the
right thing

I am having problem writing a little program using cygwin. It is about a
client sending message to a server.
I run into the exact same problem described in:

I read the follow up to this post but it doesnt seem to answer the
question (if it does for others, there is something I dont understand. So
please pardon me).

My call to accept always fails, regardless of whether I use localhost or
the actual IP address. I have also tried a few different ports to no

By the way, if both the client and server are on Solaris, then both work.
If one of them is on Cygwin and the other is on Solaris, then the message
sending doesnt work out.
The Solaris client cannot send to the Cygwin server (connection refused).
The Cygwin client cannot send to the Solaris sever (connection refused).

I am working on the same machine which runs Win2K server. My version of
Cygwin is pretty new (havent figured out how to check the version yet) coz
I just d/l it 2 weeks ago.

Can anyone please shed some light on what goes wrong here.

My codes are the below, followed by the output.
Thanks very much in advance!!!

Administrator@MOBILEHERO ~/Circa/Utils
$ ./clientMessage.exe
Sending message to server with ip mobilehero, port 4000
Error: cannot connect!
errno = 111
This means Connection refused

Administrator@MOBILEHERO ~/Circa/Utils


#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <strings.h>
#include <unistd.h>
#include <pthread.h>

// fake server

main ()
  struct sockaddr_in server_addr, *client_addr;
  struct hostent *hp;
  int listenfd, client_len;
  int *iptr;

  char ip_address[] = "localhost";
  int port = 9998;

  if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    printf("Error: server cannot create socket\n");
  printf("server socket at %d\n", listenfd);

  if ((hp = gethostbyname(ip_address)) == NULL) {
    printf("Error: server cannot get the host information\n");

  server_addr.sin_family = hp->h_addrtype;
  bcopy((char*)hp->h_addr, (char*)&server_addr.sin_addr, hp->h_length);
  server_addr.sin_port = port;

  if (bind(listenfd, (sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
    printf("Error: server binding error\n");

  listen(listenfd, 5);

  client_len = sizeof(sockaddr_in);
  client_addr = (sockaddr_in*)malloc(client_len);

  printf("Listening at host %s, port %d\n", hp->h_name, port);

  while (1) {
    printf("waiting for the messages\n");
    iptr = (int*)malloc(sizeof(int));
    if ((*iptr = accept(listenfd, (sockaddr*)&client_addr, &client_len))
== -1) {
      printf("Error: server cannot accept!\n");
    printf("server connection establised at %d\n", *iptr);


  return 1;


#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <strings.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <sys/ioctl.h>

const int MAX_NUMBER_OF_BODIES = 10;
const int FIELD_LENGTH = 50;

struct AgentMessage {
  char sender[FIELD_LENGTH];
  char receiver[FIELD_LENGTH];
  char subject[FIELD_LENGTH * 2];
  int  nBody;

main ()
  struct sockaddr_in agent_addr;
  struct hostent *hp;
  int agent_id;
  struct AgentMessage msg;
  int i;

  char sender_ip[] = "localhost";
  char dest_ip[] = "localhost";
  int port = 9998;

  // make message
  bzero((char*)(&msg), sizeof(AgentMessage));
  strcpy(msg.sender, sender_ip);
  strcpy(msg.receiver, dest_ip);
  strcpy(msg.subject, "READY");

  if ((agent_id = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    printf("Error: cannot create socket!\n");

  if ((hp = gethostbyname(dest_ip)) == NULL) {
    printf("Error: cannot get the host information about ip
  printf("Sending message to server with ip %s, port %d\n", hp->h_name,

  memset(&agent_addr, 0, sizeof(agent_addr));
  agent_addr.sin_family = hp->h_addrtype;
  bcopy((char*)hp->h_addr, (char*)&agent_addr.sin_addr, hp->h_length);

  agent_addr.sin_port = htons(port);

  if (connect(agent_id, (sockaddr*)&agent_addr, sizeof(agent_addr)) != 0)
    printf("Error: cannot connect!\n");
    printf("errno = %d\n", errno);
    printf("This means %s\n", strerror(errno));

  if (write(agent_id, (void*)&msg, sizeof(AgentMessage)) <= 0) {
    printf("Error: cannot send message to server\n");

  printf("message sent\n");

Haksun Li
Advanced Technology Laboratory 133
Electrical Engineering and Computer Science Dept.
University of Michigan
Ann Arbor MI 48109


Unsubscribe info:
Bug reporting:

More information about the Cygwin mailing list