Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

changing c socket calls to compatible with windowsNT calls

Posted on 2000-04-14
1
Medium Priority
?
181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 2

Accepted Solution

by:
munsie earned 90 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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 recursion 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.

721 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