Solved

changing c socket calls to compatible with windowsNT calls

Posted on 2000-04-14
1
176 Views
Last Modified: 2010-04-15
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
Comment
Question by:adinarayanak
1 Comment
 
LVL 2

Accepted Solution

by:
munsie earned 30 total points
ID: 2717310
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now