Solved

changing c socket calls to compatible with windowsNT calls

Posted on 2000-04-14
1
175 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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

771 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

10 Experts available now in Live!

Get 1:1 Help Now