Yin Ming wrote:
>Since not wiliing to learn WIN API totally, I want to port it via
>DEV-CPP, I mean the mingw32 libs it includes, with a little modification,
>But those libs seems not complete, I cannot find sys/socket in dev-cpp/includes.
>And none of my fellows wants to install huge of files, such as Cynwin...
>Can dev-cpp do this porting? Or I must rewrite in python or java?
>
>
It probably can.
For sockets you have to use winsock. But remember that there are many
incompatibilities. Some things to keep in mind:
* Windows sockets are *not* file descriptors. That means no read(),
no write(), and most important: *no close()*. You close sockets
with closesocket().
* Winsock needs to be initialized with WSAStartup and finalized with
WSACleanup.
* Windows sockets are *unsigned*, not signed. Expressions like
"sock<0" that are commong for detecting invalid file descriptors
won't work. An invalid socket has the value INVALID_SOCKET.
* The error codes (like EINVAL, ENETDOWN, etc) are present, but with
the "WSA" prefix (WSAEINVAL, WSAENETDOWN...).
* Instead of using fcntl for non-blocking sockets, use ioctlsocket.
* select() will accuse a non-blocking socket as readable/writable
most of the time, when it isn't. In this case, *every* send/recv
call should check for WSAEWOULDBLOCK.
* Because of this, you can't get high-speed transfers with
non-blocking sockets... you hit the wall at 2~5 MB/s.
* You check error codes with WSAGetLastError, not with errno. There
are no strings for winsock errors (i.e. strerror).
* inet_addr() will crash your program if you send it non-ASCII
chars, specially if you run it in a locale other than "C".
For network programs I usually have a header that looks like this:
---
#ifdef WIN32
#include <winsock2.h>
#define NET_STARTUP { WSADATA wsa; \
WSAStartup(MAKEWORD(2, 0), &wsa); \
atexit((void (*)())WSACleanup); }
#else
// *nix?
#define NET_STARTUP
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#define SOCKET int
#ifndef SOCKET_ERROR
#define SOCKET_ERROR -1
#endif
#define INVALID_SOCKET -1
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
#define closesocket(x) close(x)
#endif
---
I hope it helps. For mor info, look for the winsock documentation at
msdn.com.
Daniel K. O.
|