public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* posix style DGRAM sockets with cygwin.dll
@ 1999-08-23 23:39 Robert A. Mackie
  1999-08-24 18:27 ` Mumit Khan
  1999-08-31 23:49 ` Robert A. Mackie
  0 siblings, 2 replies; 6+ messages in thread
From: Robert A. Mackie @ 1999-08-23 23:39 UTC (permalink / raw)
  To: cygwin; +Cc: robert_mackie

I'm trying to get a very simple UDP example ported to cygwin as a
starting point for some real code I expect to develop.  Everything
compiles and links, but the sockets don't communicate.  When built for
unix the same code works fine.  (unless I've introduced some small bug
trying to get it to work under cygwin.)  I notice I'm not the first to
say something like this.  The last person was using the -no-cygwin
option and was told to call WSAStartup.  I read as much documentation as
I could find.  It looked to me like that was not necessary when using
the posix calls, rather than the winsock calls.  (Maybe I'm wrong ?)

If I do need to initialize winsock explicitly, could you help me with a
couple of details? The only place that I see WSAStartup and WSACleanup
defined is in <Windows32/Sockets.h>.  This file seems to be set up for
an MSC compiler, and includes a bunch of other similar headers.  When I
include any of them and the standard headers I'm used to, I get all
sorts of re-definition collisions between the headers.  When I include
them without the standard headers, things like strerror aren't defined.

The specific problem I'm having is that the datagrams never get through
to the receiver.  I have an alternate client and server that I tossed
together in Java to make sure my system was working (DNS et al.), and
while they work, they show me that neither the client nor the server
developed using cygwin appear to actually use the network.  For example,
I can bind the Java server to port 7000, and then launch the server
developed under cygwin on port 7000, and it doesn't complain.

Clearly, I'm missing something fundamental.  Thanks for any help you can
provide.

Rob Mackie.

Here is the code; it's a dirty adaption of code found in Stephen's book
on network programming:

server.cc:
#include "DataGramUtility.hh"

int main(int c, char* argv[])
{
 return DataGramUtility::server(c, argv);
}

client.cc:
#include "DataGramUtility.hh"

int main(int c, char* argv[])
{
 return DataGramUtility::client(c, argv);
}

DataGramUtility.hh:
#if( ! defined(DATA_GRAM_UTILITY) )
#define DATA_GRAM_UTILITY

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdio.h>

class DataGramUtility
{

public:
 static
 int client(int argc, char* argv[]);

 static
 int server(int argc, char* argv[]);


private:

 static
 int initialize();

 static
 int cleanup();


 static
 int serverLoop(
       int sockfd
  , struct sockaddr* pcli_addr
  , int maxclilen);

 static
 int clientLoop( FILE* fp
  , int sockfd
  , struct sockaddr* pserv_addr
  , int servlen);

};

DataGramUtility.cc:

#include "DataGramUtility.hh"

#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <errno.h>

#define MAXMESG 2048

#define LOCATION __FILE__, __LINE__
void printError(const char* file, int line)
{
 printf("%s:%d - %s\n"
  , file, line, strerror( errno ));
}

int
DataGramUtility::serverLoop(int sockfd
 , struct sockaddr* pcli_addr
 , int maxClientLength)
{
 int byteCount;
 int clientLength;
 char message[MAXMESG];

 while( true )
 {
  clientLength = maxClientLength;
  printf("waiting to recieve\n");
  byteCount
   = recvfrom(sockfd, message, MAXMESG, 0, pcli_addr, &clientLength);
  if( byteCount < 0 )
  {
   printf("DataGramUtility::serverLoop - recvfrom error: %d\n"
    , byteCount);
   printError(LOCATION);
  }
  message[byteCount] = '\0';
  printf("received: %s\n", message);
  if( sendto(
      sockfd
    , message
    , byteCount
    , 0, pcli_addr, clientLength) != byteCount)
  {
   printf("DataGramUtility::serverLoop - sendto error\n");
   printError(LOCATION);
  }
  printf("echo'd back to sender\n");
 }
 return 0;
}


#define MAXLINE 512

int
DataGramUtility::clientLoop(
   FILE* fp
 , int sockfd
 , struct sockaddr* pserv_addr
 , int serverLength)
{
 int byteCount;
 char sendline[MAXLINE];
 // char recvline[MAXLINE+1];

 printf("Ready to send\n");
 while( fgets(sendline, MAXLINE, fp) != NULL )
 {
  printf("read: %s\n", sendline);
  byteCount = strlen(sendline);
  if( sendto(
    sockfd
    , sendline
    , byteCount
    , 0, pserv_addr, serverLength) != byteCount)
  {
   printf("DataGramUtility::client - sendto error on socket\n");
   printError(LOCATION);
  }
  printf("sent\n");
#if 0
  byteCount = recvfrom(sockfd
   , recvline
   , MAXLINE
   , 0
   , (struct sockaddr*)0
   , (int*) 0 );

  if( byteCount < 0 )
  {
   printf("DataGramUtility::client - recvfrom error on socket\n");
   printError(LOCATION);
  }
  printf("received\n");
  recvline[byteCount] = 0;
  fputs(recvline, stdout);
#endif
 }
 return 0;
}


int DataGramUtility::client(int argc, char* argv[])
{
 if(DataGramUtility::initialize() == -1)
 {
  printf("Couldn't initialize winsock\n");
  return -1;
 }
 int sockfd;
 struct sockaddr_in client_address;
 struct sockaddr_in server_address;

 if( argc < 3 )
 {
  printf("Usage: %s hostip port\n", argv[0]);
  return -1;
 }

 const char* SERVER_HOST_ADDRESS = argv[1];
 const int SERVER_UDP_PORT = atoi(argv[2]);

 bzero((char*)&server_address, sizeof(server_address));
 server_address.sin_family      = AF_INET;
 server_address.sin_addr.s_addr = inet_addr(SERVER_HOST_ADDRESS);
 server_address.sin_port        = htonl(SERVER_UDP_PORT);

 if( server_address.sin_addr.s_addr == htonl(0x7F000001) )
 {
  printf("Address is local host.\n");
 }

 printf("IP: ");
 for( int i = 0; i<4; i++ )
 {
  printf( "%d.", ((unsigned char*)(&server_address.sin_addr.s_addr))[i]
);
 }

 if( (sockfd = socket(AF_INET, SOCK_DGRAM, 0 )) < 0 )
 {
  printf("DataGramUtility::client - can't open datagram socket\n");
  printError(LOCATION);
  return -1;
 }

 bzero((char*)&client_address, sizeof(client_address));
 client_address.sin_family      = AF_INET;
 client_address.sin_addr.s_addr = htonl(INADDR_ANY);
 client_address.sin_port        = htonl(0);

 if( bind(
    sockfd
  , (struct sockaddr*)&client_address
  , sizeof(client_address)) < 0 )
  {
   printf("DataGramUtility::client - can't bind local address\n");
   printError(LOCATION);
   return -1;
  }

 DataGramUtility::clientLoop(
    stdin
  , sockfd
  , (struct sockaddr*)&server_address
  , sizeof(server_address));

 close(sockfd);
 DataGramUtility::cleanup();
 return 0;
}

int DataGramUtility::server(int argc, char* argv[])
{
 if(DataGramUtility::initialize() == -1)
 {
  printf("Couldn't initialize winsock\n");
  return -1;
 }
 int sockfd;
 struct sockaddr_in server_address;
 struct sockaddr_in client_address;

 if( argc < 2 )
 {
  printf("Usage: %s port\n", argv[0]);
  return -1;
 }

 const int SERVER_UDP_PORT = atoi(argv[1]);

 if( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 )
 {
  printf("DataGramUtility::server - can't open datagram socket\n");
  printError(LOCATION);
  return -1;
 }

 bzero((char*)&server_address, sizeof(server_address));
 server_address.sin_family      = AF_INET;
 server_address.sin_addr.s_addr = htonl(INADDR_ANY);
 server_address.sin_port        = htonl(SERVER_UDP_PORT);

 if( bind(
       sockfd
  , (struct sockaddr*) &server_address
  , sizeof(server_address)) < 0 )
 {
  printf("DataGramUtility::server - can't bind local address\n");
  printError(LOCATION);
  return -1;
 }

 DataGramUtility::serverLoop(
    sockfd
  , (struct sockaddr*)&client_address
  , sizeof(client_address));

 DataGramUtility::cleanup();
 return 0;
}


int
DataGramUtility::initialize()
{
#if 0
 WSADATA WSAData;
 if (WSAStartup(0x0101, &WSAData)) {
  printf("WSAStartup failed (%d)\n",WSAGetLastError());
  return -1;
 }
#endif
 return 0;
}

int
DataGramUtility::cleanup()
{
#if 0
 WSACleanup();
#endif
}

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: posix style DGRAM sockets with cygwin.dll
  1999-08-23 23:39 posix style DGRAM sockets with cygwin.dll Robert A. Mackie
@ 1999-08-24 18:27 ` Mumit Khan
  1999-08-25 14:39   ` Robert A. Mackie
  1999-08-31 23:49   ` Mumit Khan
  1999-08-31 23:49 ` Robert A. Mackie
  1 sibling, 2 replies; 6+ messages in thread
From: Mumit Khan @ 1999-08-24 18:27 UTC (permalink / raw)
  To: Robert A. Mackie; +Cc: cygwin

"Robert A. Mackie" <robert_mackie@mail.com> writes:
> 
> I'm trying to get a very simple UDP example ported to cygwin as a
> starting point for some real code I expect to develop.  Everything
> compiles and links, but the sockets don't communicate.  When built for
> unix the same code works fine.  (unless I've introduced some small bug
> trying to get it to work under cygwin.)  I notice I'm not the first to
> say something like this.  The last person was using the -no-cygwin
> option and was told to call WSAStartup.  I read as much documentation as
> I could find.  It looked to me like that was not necessary when using
> the posix calls, rather than the winsock calls.  (Maybe I'm wrong ?)

If you're using -mno-cygwin, you're using Windows sockets (and you need
WSAStartup); if you're *not* using -mno-cygwin, you're using POSIX sockets
and obviously don't need the windows hacks, er features.

> If I do need to initialize winsock explicitly, could you help me with a
> couple of details? The only place that I see WSAStartup and WSACleanup
> defined is in <Windows32/Sockets.h>.  This file seems to be set up for
> an MSC compiler, and includes a bunch of other similar headers.  When I
> include any of them and the standard headers I'm used to, I get all
> sorts of re-definition collisions between the headers.  When I include
> them without the standard headers, things like strerror aren't defined.

You include winsock.h, not the internal headers, but *only* if you're using
Windows sockets. If you're using Cygwin runtime, use the POSIX specs and
forget about the windows headers (Cygwin runtime will do the right thing).

It's quite unclear from your message what runtime you're trying to use
and so on. If you could elaborate, we could try to help. 

As far as your code is concerned, there is at least one blatant bug I
see right away -- 

  server_address.sin_port        = htonl(SERVER_UDP_PORT);
                                   ^^^^^ (should be htons)

likewise in the other case. Just remember that sin_addr is htonl and
sin_port is htons and life would be a lot easier.

Perhaps you ought to check over your code a bit more and try again.

Regards,
Mumit


--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: posix style DGRAM sockets with cygwin.dll
  1999-08-24 18:27 ` Mumit Khan
@ 1999-08-25 14:39   ` Robert A. Mackie
  1999-08-31 23:49     ` Robert A. Mackie
  1999-08-31 23:49   ` Mumit Khan
  1 sibling, 1 reply; 6+ messages in thread
From: Robert A. Mackie @ 1999-08-25 14:39 UTC (permalink / raw)
  To: Mumit Khan; +Cc: cygwin

Mumit,

    Thank you for looking.  I'm sorry this I asked for your time on what turned
out to be a code inspection issue rather than a cygwin issue.  Since the unix
system on which it ran correctly uses network order for memory, the code worked
there, inspite of my error.  I should have been more vigilent.

Rob.

PS:  I'm very impressed with the value of cygwin.  I should free up from my
most urgent deadlines in the next month or so.  I plan to volunteer some effort
to help support cygwin at that point.  What a convenient set of tools and
library.

Mumit Khan wrote:

> "Robert A. Mackie" <robert_mackie@mail.com> writes:
> >
> > I'm trying to get a very simple UDP example ported to cygwin as a
> > starting point for some real code I expect to develop.  Everything
> > compiles and links, but the sockets don't communicate.  When built for
> > unix the same code works fine.  (unless I've introduced some small bug
> > trying to get it to work under cygwin.)  I notice I'm not the first to
> > say something like this.  The last person was using the -no-cygwin
> > option and was told to call WSAStartup.  I read as much documentation as
> > I could find.  It looked to me like that was not necessary when using
> > the posix calls, rather than the winsock calls.  (Maybe I'm wrong ?)
>
> If you're using -mno-cygwin, you're using Windows sockets (and you need
> WSAStartup); if you're *not* using -mno-cygwin, you're using POSIX sockets
> and obviously don't need the windows hacks, er features.
>
> > If I do need to initialize winsock explicitly, could you help me with a
> > couple of details? The only place that I see WSAStartup and WSACleanup
> > defined is in <Windows32/Sockets.h>.  This file seems to be set up for
> > an MSC compiler, and includes a bunch of other similar headers.  When I
> > include any of them and the standard headers I'm used to, I get all
> > sorts of re-definition collisions between the headers.  When I include
> > them without the standard headers, things like strerror aren't defined.
>
> You include winsock.h, not the internal headers, but *only* if you're using
> Windows sockets. If you're using Cygwin runtime, use the POSIX specs and
> forget about the windows headers (Cygwin runtime will do the right thing).
>
> It's quite unclear from your message what runtime you're trying to use
> and so on. If you could elaborate, we could try to help.
>
> As far as your code is concerned, there is at least one blatant bug I
> see right away --
>
>   server_address.sin_port        = htonl(SERVER_UDP_PORT);
>                                    ^^^^^ (should be htons)
>
> likewise in the other case. Just remember that sin_addr is htonl and
> sin_port is htons and life would be a lot easier.
>
> Perhaps you ought to check over your code a bit more and try again.
>
> Regards,
> Mumit

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: posix style DGRAM sockets with cygwin.dll
  1999-08-25 14:39   ` Robert A. Mackie
@ 1999-08-31 23:49     ` Robert A. Mackie
  0 siblings, 0 replies; 6+ messages in thread
From: Robert A. Mackie @ 1999-08-31 23:49 UTC (permalink / raw)
  To: Mumit Khan; +Cc: cygwin

Mumit,

    Thank you for looking.  I'm sorry this I asked for your time on what turned
out to be a code inspection issue rather than a cygwin issue.  Since the unix
system on which it ran correctly uses network order for memory, the code worked
there, inspite of my error.  I should have been more vigilent.

Rob.

PS:  I'm very impressed with the value of cygwin.  I should free up from my
most urgent deadlines in the next month or so.  I plan to volunteer some effort
to help support cygwin at that point.  What a convenient set of tools and
library.

Mumit Khan wrote:

> "Robert A. Mackie" <robert_mackie@mail.com> writes:
> >
> > I'm trying to get a very simple UDP example ported to cygwin as a
> > starting point for some real code I expect to develop.  Everything
> > compiles and links, but the sockets don't communicate.  When built for
> > unix the same code works fine.  (unless I've introduced some small bug
> > trying to get it to work under cygwin.)  I notice I'm not the first to
> > say something like this.  The last person was using the -no-cygwin
> > option and was told to call WSAStartup.  I read as much documentation as
> > I could find.  It looked to me like that was not necessary when using
> > the posix calls, rather than the winsock calls.  (Maybe I'm wrong ?)
>
> If you're using -mno-cygwin, you're using Windows sockets (and you need
> WSAStartup); if you're *not* using -mno-cygwin, you're using POSIX sockets
> and obviously don't need the windows hacks, er features.
>
> > If I do need to initialize winsock explicitly, could you help me with a
> > couple of details? The only place that I see WSAStartup and WSACleanup
> > defined is in <Windows32/Sockets.h>.  This file seems to be set up for
> > an MSC compiler, and includes a bunch of other similar headers.  When I
> > include any of them and the standard headers I'm used to, I get all
> > sorts of re-definition collisions between the headers.  When I include
> > them without the standard headers, things like strerror aren't defined.
>
> You include winsock.h, not the internal headers, but *only* if you're using
> Windows sockets. If you're using Cygwin runtime, use the POSIX specs and
> forget about the windows headers (Cygwin runtime will do the right thing).
>
> It's quite unclear from your message what runtime you're trying to use
> and so on. If you could elaborate, we could try to help.
>
> As far as your code is concerned, there is at least one blatant bug I
> see right away --
>
>   server_address.sin_port        = htonl(SERVER_UDP_PORT);
>                                    ^^^^^ (should be htons)
>
> likewise in the other case. Just remember that sin_addr is htonl and
> sin_port is htons and life would be a lot easier.
>
> Perhaps you ought to check over your code a bit more and try again.
>
> Regards,
> Mumit

^ permalink raw reply	[flat|nested] 6+ messages in thread

* posix style DGRAM sockets with cygwin.dll
  1999-08-23 23:39 posix style DGRAM sockets with cygwin.dll Robert A. Mackie
  1999-08-24 18:27 ` Mumit Khan
@ 1999-08-31 23:49 ` Robert A. Mackie
  1 sibling, 0 replies; 6+ messages in thread
From: Robert A. Mackie @ 1999-08-31 23:49 UTC (permalink / raw)
  To: cygwin; +Cc: robert_mackie

I'm trying to get a very simple UDP example ported to cygwin as a
starting point for some real code I expect to develop.  Everything
compiles and links, but the sockets don't communicate.  When built for
unix the same code works fine.  (unless I've introduced some small bug
trying to get it to work under cygwin.)  I notice I'm not the first to
say something like this.  The last person was using the -no-cygwin
option and was told to call WSAStartup.  I read as much documentation as
I could find.  It looked to me like that was not necessary when using
the posix calls, rather than the winsock calls.  (Maybe I'm wrong ?)

If I do need to initialize winsock explicitly, could you help me with a
couple of details? The only place that I see WSAStartup and WSACleanup
defined is in <Windows32/Sockets.h>.  This file seems to be set up for
an MSC compiler, and includes a bunch of other similar headers.  When I
include any of them and the standard headers I'm used to, I get all
sorts of re-definition collisions between the headers.  When I include
them without the standard headers, things like strerror aren't defined.

The specific problem I'm having is that the datagrams never get through
to the receiver.  I have an alternate client and server that I tossed
together in Java to make sure my system was working (DNS et al.), and
while they work, they show me that neither the client nor the server
developed using cygwin appear to actually use the network.  For example,
I can bind the Java server to port 7000, and then launch the server
developed under cygwin on port 7000, and it doesn't complain.

Clearly, I'm missing something fundamental.  Thanks for any help you can
provide.

Rob Mackie.

Here is the code; it's a dirty adaption of code found in Stephen's book
on network programming:

server.cc:
#include "DataGramUtility.hh"

int main(int c, char* argv[])
{
 return DataGramUtility::server(c, argv);
}

client.cc:
#include "DataGramUtility.hh"

int main(int c, char* argv[])
{
 return DataGramUtility::client(c, argv);
}

DataGramUtility.hh:
#if( ! defined(DATA_GRAM_UTILITY) )
#define DATA_GRAM_UTILITY

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdio.h>

class DataGramUtility
{

public:
 static
 int client(int argc, char* argv[]);

 static
 int server(int argc, char* argv[]);


private:

 static
 int initialize();

 static
 int cleanup();


 static
 int serverLoop(
       int sockfd
  , struct sockaddr* pcli_addr
  , int maxclilen);

 static
 int clientLoop( FILE* fp
  , int sockfd
  , struct sockaddr* pserv_addr
  , int servlen);

};

DataGramUtility.cc:

#include "DataGramUtility.hh"

#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <errno.h>

#define MAXMESG 2048

#define LOCATION __FILE__, __LINE__
void printError(const char* file, int line)
{
 printf("%s:%d - %s\n"
  , file, line, strerror( errno ));
}

int
DataGramUtility::serverLoop(int sockfd
 , struct sockaddr* pcli_addr
 , int maxClientLength)
{
 int byteCount;
 int clientLength;
 char message[MAXMESG];

 while( true )
 {
  clientLength = maxClientLength;
  printf("waiting to recieve\n");
  byteCount
   = recvfrom(sockfd, message, MAXMESG, 0, pcli_addr, &clientLength);
  if( byteCount < 0 )
  {
   printf("DataGramUtility::serverLoop - recvfrom error: %d\n"
    , byteCount);
   printError(LOCATION);
  }
  message[byteCount] = '\0';
  printf("received: %s\n", message);
  if( sendto(
      sockfd
    , message
    , byteCount
    , 0, pcli_addr, clientLength) != byteCount)
  {
   printf("DataGramUtility::serverLoop - sendto error\n");
   printError(LOCATION);
  }
  printf("echo'd back to sender\n");
 }
 return 0;
}


#define MAXLINE 512

int
DataGramUtility::clientLoop(
   FILE* fp
 , int sockfd
 , struct sockaddr* pserv_addr
 , int serverLength)
{
 int byteCount;
 char sendline[MAXLINE];
 // char recvline[MAXLINE+1];

 printf("Ready to send\n");
 while( fgets(sendline, MAXLINE, fp) != NULL )
 {
  printf("read: %s\n", sendline);
  byteCount = strlen(sendline);
  if( sendto(
    sockfd
    , sendline
    , byteCount
    , 0, pserv_addr, serverLength) != byteCount)
  {
   printf("DataGramUtility::client - sendto error on socket\n");
   printError(LOCATION);
  }
  printf("sent\n");
#if 0
  byteCount = recvfrom(sockfd
   , recvline
   , MAXLINE
   , 0
   , (struct sockaddr*)0
   , (int*) 0 );

  if( byteCount < 0 )
  {
   printf("DataGramUtility::client - recvfrom error on socket\n");
   printError(LOCATION);
  }
  printf("received\n");
  recvline[byteCount] = 0;
  fputs(recvline, stdout);
#endif
 }
 return 0;
}


int DataGramUtility::client(int argc, char* argv[])
{
 if(DataGramUtility::initialize() == -1)
 {
  printf("Couldn't initialize winsock\n");
  return -1;
 }
 int sockfd;
 struct sockaddr_in client_address;
 struct sockaddr_in server_address;

 if( argc < 3 )
 {
  printf("Usage: %s hostip port\n", argv[0]);
  return -1;
 }

 const char* SERVER_HOST_ADDRESS = argv[1];
 const int SERVER_UDP_PORT = atoi(argv[2]);

 bzero((char*)&server_address, sizeof(server_address));
 server_address.sin_family      = AF_INET;
 server_address.sin_addr.s_addr = inet_addr(SERVER_HOST_ADDRESS);
 server_address.sin_port        = htonl(SERVER_UDP_PORT);

 if( server_address.sin_addr.s_addr == htonl(0x7F000001) )
 {
  printf("Address is local host.\n");
 }

 printf("IP: ");
 for( int i = 0; i<4; i++ )
 {
  printf( "%d.", ((unsigned char*)(&server_address.sin_addr.s_addr))[i]
);
 }

 if( (sockfd = socket(AF_INET, SOCK_DGRAM, 0 )) < 0 )
 {
  printf("DataGramUtility::client - can't open datagram socket\n");
  printError(LOCATION);
  return -1;
 }

 bzero((char*)&client_address, sizeof(client_address));
 client_address.sin_family      = AF_INET;
 client_address.sin_addr.s_addr = htonl(INADDR_ANY);
 client_address.sin_port        = htonl(0);

 if( bind(
    sockfd
  , (struct sockaddr*)&client_address
  , sizeof(client_address)) < 0 )
  {
   printf("DataGramUtility::client - can't bind local address\n");
   printError(LOCATION);
   return -1;
  }

 DataGramUtility::clientLoop(
    stdin
  , sockfd
  , (struct sockaddr*)&server_address
  , sizeof(server_address));

 close(sockfd);
 DataGramUtility::cleanup();
 return 0;
}

int DataGramUtility::server(int argc, char* argv[])
{
 if(DataGramUtility::initialize() == -1)
 {
  printf("Couldn't initialize winsock\n");
  return -1;
 }
 int sockfd;
 struct sockaddr_in server_address;
 struct sockaddr_in client_address;

 if( argc < 2 )
 {
  printf("Usage: %s port\n", argv[0]);
  return -1;
 }

 const int SERVER_UDP_PORT = atoi(argv[1]);

 if( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 )
 {
  printf("DataGramUtility::server - can't open datagram socket\n");
  printError(LOCATION);
  return -1;
 }

 bzero((char*)&server_address, sizeof(server_address));
 server_address.sin_family      = AF_INET;
 server_address.sin_addr.s_addr = htonl(INADDR_ANY);
 server_address.sin_port        = htonl(SERVER_UDP_PORT);

 if( bind(
       sockfd
  , (struct sockaddr*) &server_address
  , sizeof(server_address)) < 0 )
 {
  printf("DataGramUtility::server - can't bind local address\n");
  printError(LOCATION);
  return -1;
 }

 DataGramUtility::serverLoop(
    sockfd
  , (struct sockaddr*)&client_address
  , sizeof(client_address));

 DataGramUtility::cleanup();
 return 0;
}


int
DataGramUtility::initialize()
{
#if 0
 WSADATA WSAData;
 if (WSAStartup(0x0101, &WSAData)) {
  printf("WSAStartup failed (%d)\n",WSAGetLastError());
  return -1;
 }
#endif
 return 0;
}

int
DataGramUtility::cleanup()
{
#if 0
 WSACleanup();
#endif
}

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: posix style DGRAM sockets with cygwin.dll
  1999-08-24 18:27 ` Mumit Khan
  1999-08-25 14:39   ` Robert A. Mackie
@ 1999-08-31 23:49   ` Mumit Khan
  1 sibling, 0 replies; 6+ messages in thread
From: Mumit Khan @ 1999-08-31 23:49 UTC (permalink / raw)
  To: Robert A. Mackie; +Cc: cygwin

"Robert A. Mackie" <robert_mackie@mail.com> writes:
> 
> I'm trying to get a very simple UDP example ported to cygwin as a
> starting point for some real code I expect to develop.  Everything
> compiles and links, but the sockets don't communicate.  When built for
> unix the same code works fine.  (unless I've introduced some small bug
> trying to get it to work under cygwin.)  I notice I'm not the first to
> say something like this.  The last person was using the -no-cygwin
> option and was told to call WSAStartup.  I read as much documentation as
> I could find.  It looked to me like that was not necessary when using
> the posix calls, rather than the winsock calls.  (Maybe I'm wrong ?)

If you're using -mno-cygwin, you're using Windows sockets (and you need
WSAStartup); if you're *not* using -mno-cygwin, you're using POSIX sockets
and obviously don't need the windows hacks, er features.

> If I do need to initialize winsock explicitly, could you help me with a
> couple of details? The only place that I see WSAStartup and WSACleanup
> defined is in <Windows32/Sockets.h>.  This file seems to be set up for
> an MSC compiler, and includes a bunch of other similar headers.  When I
> include any of them and the standard headers I'm used to, I get all
> sorts of re-definition collisions between the headers.  When I include
> them without the standard headers, things like strerror aren't defined.

You include winsock.h, not the internal headers, but *only* if you're using
Windows sockets. If you're using Cygwin runtime, use the POSIX specs and
forget about the windows headers (Cygwin runtime will do the right thing).

It's quite unclear from your message what runtime you're trying to use
and so on. If you could elaborate, we could try to help. 

As far as your code is concerned, there is at least one blatant bug I
see right away -- 

  server_address.sin_port        = htonl(SERVER_UDP_PORT);
                                   ^^^^^ (should be htons)

likewise in the other case. Just remember that sin_addr is htonl and
sin_port is htons and life would be a lot easier.

Perhaps you ought to check over your code a bit more and try again.

Regards,
Mumit


--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~1999-08-31 23:49 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-08-23 23:39 posix style DGRAM sockets with cygwin.dll Robert A. Mackie
1999-08-24 18:27 ` Mumit Khan
1999-08-25 14:39   ` Robert A. Mackie
1999-08-31 23:49     ` Robert A. Mackie
1999-08-31 23:49   ` Mumit Khan
1999-08-31 23:49 ` Robert A. Mackie

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).