This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

AF_INET connection refused help pls


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:

http://sources.redhat.com/ml/cygwin/2001-05/msg01085.html

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
avail.

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



Server:

#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

int
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");
    exit(1);
  }
  printf("server socket at %d\n", listenfd);

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

  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");
    exit(1);
  }

  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");
      //exit(1);
    }
    printf("server connection establised at %d\n", *iptr);

  }

  return 1;
}



client:


#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;
  char body[MAX_NUMBER_OF_BODIES][FIELD_LENGTH];
};

void
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");
    exit(1);
  }

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

  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));
    exit(1);
  }

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

  close(agent_id);
  printf("message sent\n");
}




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

haksunli@engin.umich.edu

Quote:


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]