Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 183
  • Last Modified:

changing c socket calls to compatible with windowsNT calls

I have written a simple client server program in c using read and write calls.
There connect,bing,socket.... or standard calls.

How to convert these calls when run this on WinNT.I am using bc to compile the code.(the editor is visual studio 6.0).If possible can any body send the sample program for that.

It is urgent please do it.
0
adinarayanak
Asked:
adinarayanak
1 Solution
 
munsieCommented:
first, convert your read() calls to recv() calls... the recv() call is available on both Unix and NT.  Convert the write() to send()... same as above.

second, place some #ifdefs at the top of your code or in a header somewhere:

#if defined _WIN32
typedef long ssize_t;

#else
typedef int SOCKET;

#define INVALID_SOCKET  -1
#define closesocket(s)  close(s)
#endif

third, you'll need a function to call to initialize WinSock... I stole this one from the VisualC++ docs somewhere:

#if defined _WIN32
static bool InitializeWinsock(void)
{
      WORD    wVersionRequested;
      WSADATA wsaData;
      int     err;

      wVersionRequested = MAKEWORD(2, 0);
      if ((err = WSAStartup(wVersionRequested, &wsaData)) != 0)
      {
            fprintf(stderr, "InitializeWinsock: Cannot find suitable WinSock version.");
          return false;
}

      // Confirm that the WinSock DLL supports 2.0
      // Note that if the DLL supports versions greater
      // than 2.0 in addition to 2.0, it will still return
      // 2.0 in wVersion since that is the version we
      // requested.

      if ((LOBYTE(wsaData.wVersion) != 2) ||
            (HIBYTE(wsaData.wVersion) != 0))
      {
WSACleanup();
      fprintf(stderr, "InitializeWinsock: Cannot find suitable WinSock version.");
            return false;
      }

      return true;

} // of InitializeWinsock()

#else
#define InitializeWinSock() true
#endif

fourth, error checking... use WSAGetLastError() to get the last err code for socket operations... under Unix, you'd normally use errno.

fifth, replace your close(socket) calls with closesocket(socket)

that should cover most of your issues

good luck,
dennis
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now