Solved

changing c socket calls to compatible with windowsNT calls

Posted on 2000-04-14
1
178 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

Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
memory mapped I/O query 6 147
How do I avoid pointer to integer casting errors in C programming? 4 243
Acrinis True image 2 93
nested if statement in excel help 4 37
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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

830 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