bug-gnubg
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Bug-gnubg] Winsock (Was: [Bug-gnubg] Multiprocessing)


From: Olivier Baur
Subject: Re: Bug-gnubg] Winsock (Was: [Bug-gnubg] Multiprocessing)
Date: Wed, 9 Jul 2003 14:47:00 +0200

Nardy, I had not read this message before posting the previous one about winsock... What follows are suggestions of #defines/#includes that could be added to non-compiling BSD-socket source code to make it "compatible" with winsock...


Le mercredi, 9 juil 2003, à 02:23 Europe/Paris, Nardy Pillards a écrit :

Example of sockets and Win32
(from http://web.presby.edu/~wasmith/courses/315/samples/sockets/ )

#include <iostream>
#include <string>
#include <fstream>
#ifdef WIN32
#include <windows.h> // this includes winsock2 (among many other things)
#include <io.h>          // needed for read, write
#else
#include <sys/socket.h> // basic socket definitions
#include <netinet/in.h> // sockaddr_in and other Internet defns
#include <arpa/inet.h> // inet(3) functions
#include <unistd.h> // needed for read, write
#include <strings.h> // for bzero
#include <stdlib.h>     // for atoi
#endif

You should modify the #include's as explained above.

The following should be added somewhere in gnubg.c: real_main() I think

real_main()
{
        /* declarations */
#ifdef WIN32
  WSADATA              wsaData;
  int                  Ret;
#endif

        /* instructions */
#ifdef WIN32
  if ((Ret = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0) {
    cerr << "winsock startup error" << endl;
    return -1;
  }
#endif

/* real_main() main loop here */

#ifdef WIN32
    WSACleanup();
#endif

        /* end of real_main() */
}



#ifdef WIN32
  servaddr.sin_family = AF_INET;
  servaddr.sin_port   = htons(portnum);
  servaddr.sin_addr.s_addr = inet_addr(ipaddress);
#else
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port   = htons(portnum);
#endif
#ifdef sun
  if (inet_pton(AF_INET, ipaddress, &servaddr.sin_addr) <= 0)
#endif
#ifdef __APPLE__
  if (inet_aton(ipaddress, &servaddr.sin_addr) <= 0)
#endif
#ifndef WIN32
  {
    cerr << "inet_pton error for %s" << ipaddress << endl;
    return -1;
  }
#endif

Maybe defining the following will be enough:

#if WIN32
#define inet_aton(ip,addr)  (addr)->s_addr = inet_addr(ip), 1
#define inet_pton(fam,ip,addr) (addr)->s_addr = inet_addr(ip), 1
#endif


#ifdef WIN32
    closesocket(sockfd);
    WSACleanup();
#else
    close(sockfd);
#endif
    return -1;
  }

In sources that use *net* sockets, define:
#if WIN32
#define close(s) closesocket(sockfd)
#endif



// send a file name to the server to copy here
  string msg = serverFileName;
  msg += "\n";
#ifdef WIN32
  // winsock apps typically use send (4th arg is flags)
  if (send(sockfd, msg.c_str(), msg.length(), 0) == SOCKET_ERROR)
#else
  if (write(sockfd, msg.c_str(), msg.length()) != msg.length())
#endif

In sources that use *net* sockets, define:
#if WIN32
#define write(s,m,len) (send((s),(m),(len),0) == SOCKET_ERROR ? -1 : (len))
#endif



/*
  Purpose: read a line of text from a socket
  Output: a string containing the line without a newline at end
    (newline is interpreted as "end of transmission")
 */
bool readLine(int sock, string & buf) {
  char cbuf;
  buf = ""; // initialize to the empty string
  bool newlineread = false;
#ifdef WIN32
  // winsock apps typically use recv (4th arg is flags)
  // (note: recv is available on unix side as well)
  while (!newlineread && recv(sock, &cbuf, 1, 0) > 0)
#else
  while (!newlineread && read(sock, &cbuf, 1) > 0)
#endif
  {
    if (cbuf == '\n') newlineread = true;  // quit if newline just read
    else buf = buf + cbuf;
  }
  // if we didn't get a newline, we failed
  return newlineread;
}


In sources that use *net* sockets, define:
#if WIN32
#define read(s,b,len) (rcv((s),(b),(len),0) == SOCKET_ERROR ? -1 : (len))
#endif



Hope this helps a little bit...

-- Olivier





reply via email to

[Prev in Thread] Current Thread [Next in Thread]